Sort by

recency

|

468 Discussions

|

  • + 0 comments

    Can someone explain what's wrong in this code? I am adding the difference between max and min(in rounds of 1,2,5) to each element except the max element and updating the array to perform above operation again until i get diff=0. I am able to solve in less operations compared to output of test cases, which is weird. Here's the code:

    import math import os import random import re import sys

    #

    Complete the 'equal' function below.

    #

    The function is expected to return an INTEGER.

    The function accepts INTEGER_ARRAY arr as parameter.

    #

    def split_diff(diff, splitrounds): if diff == 1: splitrounds = splitrounds + 1 elif diff < 5 and diff >= 2: quotient = diff // 2 # print("quo:", quotient) remainder = diff % 2 splitrounds = splitrounds + quotient + remainder elif diff >= 5: quotient = diff // 5 remainder = diff % 5 if remainder != 0: splitrounds = splitrounds + quotient + split_diff(remainder, 0) else: splitrounds = splitrounds + quotient

    # print("split:", splitrounds)
    return splitrounds
    

    def equal(arr, rounds):

    max_ = max(arr)
    min_ = min(arr)
    diff = max_ - min_
    print(min_, max_, diff)
    if diff == 0:
        # print(rounds)
        return rounds
    elif diff in [1,2,5]:
        rounds += 1
    else:
    
        rounds += split_diff(diff, 0)
        # print(rounds)
    
    for i in range(len(arr)):
        if arr[i] != max_:
            arr[i] = arr[i] + diff
    
    # print(arr)
    
    return equal(arr, rounds)
    
    
    
    # Write your code here
    

    if name == 'main': fptr = open(os.environ['OUTPUT_PATH'], 'w')

    t = int(input().strip())
    
    for t_itr in range(t):
        n = int(input().strip())
    
        arr = list(map(int, input().rstrip().split()))
        rounds = 0
        result = equal(arr, rounds)
    
        fptr.write(str(result) + '\n')
    
    fptr.close()
    
  • + 0 comments

    Can someone explain what's wrong in this code? I am adding the difference between max and min(in rounds of 1,2,5) to each element except the max element and updating the array to perform above operation again until i get diff=0. I am able to solve in less operations compared to output of test cases, which is weird. Here's the code:

    !/bin/python3

    import math import os import random import re import sys

    def split_diff(diff, splitrounds): if diff == 1: splitrounds = splitrounds + 1 elif diff < 5 and diff >= 2: quotient = diff // 2 remainder = diff % 2 splitrounds = splitrounds + quotient + remainder elif diff >= 5: quotient = diff // 5 remainder = diff % 5 if remainder != 0: splitrounds = splitrounds + quotient + split_diff(remainder, 0) else: splitrounds = splitrounds + quotient

    return splitrounds
    

    def equal(arr, rounds):

    max_ = max(arr)
    min_ = min(arr)
    diff = max_ - min_
    print(min_, max_, diff)
    if diff == 0:
        return rounds
    elif diff in [1,2,5]:
        rounds += 1
    else:
    
        rounds += split_diff(diff, 0)
    
    
    for i in range(len(arr)):
        if arr[i] != max_:
            arr[i] = arr[i] + diff
    
    
    return equal(arr, rounds)
    
    
    
    # Write your code here
    

    if name == 'main': fptr = open(os.environ['OUTPUT_PATH'], 'w')

    t = int(input().strip())
    
    for t_itr in range(t):
        n = int(input().strip())
    
        arr = list(map(int, input().rstrip().split()))
        rounds = 0
        result = equal(arr, rounds)
    
        fptr.write(str(result) + '\n')
    
    fptr.close()
    
  • + 0 comments

    The key perspective is that adding candies to all but one colleague is mathematically the same as substracting from that one colleague the amount added to everyone else.

  • + 0 comments

    Well, after reinspecting my solution a number of times, looks like I managed to solve the equalization with quite few rounds than the tests are pointing... :D

    I can't see where it could be going wrong, it reduces the gap by 5 preferrably, then 2, and 1 given the maximum difference between elements in the list to get as fewer steps as possible, never touching the greater value in the list, then reduces the list to make it distinct.

    Checking the log, it goes down to a single element meaning all item gaps reduced to 0. Here's the funny part, with way lesser rounds than the tests expected output.

    Here it is in Java 8 (comments are welcome):

    `

    class Result {

    static int equalizeStep(List<Integer> A, int step, int rounds) {
    
        List<Integer> Qs = A.stream()
                .distinct()
                .sorted()
                .collect(Collectors.toList());
    
        if (step>0) {
            System.out.println(">>>LOG Qs:"+Qs+", step:"+step+", rounds:"+rounds);
        } else {
            System.out.println(">>>STARTING");
        }
    
        if (Qs.size() < 2) return rounds;
    
        if (step>0) {
            for (int i=0; i<Qs.size()-1; i++) {
                Qs.set(i, Qs.get(i)+step);
            }
            rounds++;
        }
    
        int diffQ = (Qs.get(Qs.size()-1)-Qs.get(0));
        if (diffQ >= 5) return equalizeStep(Qs, 5, rounds);
        if (diffQ >= 2) return equalizeStep(Qs, 2, rounds);
        return equalizeStep(Qs, 1, rounds);
    }
    
    public static int equal(List<Integer> arr) {
        return equalizeStep(arr, 0, 0);
    }
    

    }

    `

  • + 0 comments

    Powerful ERP for SMEs in India - Chirix by JM-Origin

    Transform your SME with Chirix ERP from JM-Origin — a cloud-based, scalable system built for Indian businesses to boost efficiency, cut costs & drive growth.