Hackerrank - Equal Solution

Hackerrank - Equal Solution

Christy is interning at HackerRank. One day she has to distribute some chocolates to her colleagues. She is biased towards her friends and plans to give them more than the others. One of the program managers hears of this and tells her to make sure everyone gets the same number.

To make things difficult, she must equalize the number of chocolates in a series of operations. For each operation, she can give  chocolates to all but one colleague. Everyone who gets chocolate in a round receives the same number of pieces.

For example, assume the starting distribution is . She can give  bars to the first two and the distribution will be . On the next round, she gives the same two  bars each, and everyone has the same number: .

Given a starting distribution, calculate the minimum number of operations needed so that every colleague has the same number of chocolates.

Function Description

Complete the equal function in the editor below. It should return an integer that reperesents the minimum number of operations required.

equal has the following parameter(s):

  • arr: an array of integers to equalize

Input Format

The first line contains an integer , the number of test cases.

Each test case has  lines.
- The first line contains an integer , the number of colleagues.
- The second line contains  space-separated integers denoting the number of chocolates each colleague has.

Constraints



Number of initial chocolates each colleague has <

Output Format

Print the minimum number of operations needed for each test case, one to a line.

Sample Input

1
4
2 2 3 7

Sample Output

2

Explanation

Start with
Add  to all but the 3rd element
Add  to all but the 4th element

Two operations were required.

Sample Input 1

1
3
10 7 12

Sample Output 1

3

Explanation 1

Start with
Add  to the first two elements
Add  to the last two elements
Add  to the last two elements

Three operations were required.

Solution in Python

def equal(arr):
    m = min(arr)
    t = [0]*4
    for i in arr:
        for j in range(4):
            x = i-(m-j)
            x = x//5 +(x%5)//2 + (x%5)%2 
            t[j]+=x
    return min(t)

for _ in range(int(input())):
    n = int(input())
    arr = list(map(int,input().split()))
    print(equal(arr))

Answer explanation

Christy has to equalize the number of chocolates for all the coworkers. The only action she can make at every operation is to increase the count of every others' chocolate by 1,2 or 5 except one of them. This is equivalent to saying, christy can take away the chocolates of one coworker by 1, 2 or 5 while keeping others' chocolate untouched.

Let's consider decreasing a coworker's chocolate as an operation. To minimize the number of operations, we should try to make the number of chocolates of every coworker equal to the minimum one in the group(min). We have to decrease the number of chocolates the ith person A[i] by (A[i] - min). Let this value be x.

We now follow a greedy algorithm so number of operations required is minimum. This can be done in k operations.

k = x//5 +(x%5)//2 + (x%5)%2 

Example

Given array [4,5,7,9], minimum value of our array is 4

To convert 9 to 4 we need 2 operations. 9-5-1

To convert 7 to 4 we need 2 operations. 7-2-1

To convert 5 to 4 we need 1 operation. 5-1

To convert 4 to 4 we need 0 operation.

Our answer is 2+2+1+0 = 5

Let f(min) be sum of operations performed over all coworkers to reduce each of their chocolates to min.

However, sometimes f(min) might not always give the correct answer. It can also be a case when

f(min) > f(min-1)

It may possible that it takes a total of fewer operations to convert all values to 3 or 2 or 1 or 0 instead of 4. Therefore we check all possibilities

It is safe to assume that

f(min) < f(min-5)

as f(min-5) takes N operations more than f(min) where N is the number of coworkers.

Therefore, if

A = {min,min-1,min-2,min-3,min-4} 

then,

f(A) <= f(min) < f(min-5)

Compute f(y) ∀ y ∈ A and print the minimum as the answer.

Subscribe to The Poor Coder | Algorithm Solutions

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
[email protected]
Subscribe