# HackerRank Equal problem solution

In this HackerRank Equal Problem solution you have given a starting distribution, calculate the minimum number of operations needed so that every colleague has the same number of pieces.

## Problem solution in Python.

```def g(diff):
ans = {0:0, 1:1, 2:1, 3:2, 4:2}
return diff // 5 + ans[diff % 5]

def f(chocolates, goal):
return sum(g(chocolate-goal) for chocolate in chocolates)

def get_ans(chocolates):
min_chocolate = min(chocolates)
return min(f(chocolates, min_chocolate - dc) for dc in range(4))

T = int(input(""))
for i in range(T):
N = int(input(""))
inp = input("").split()
chocolates = [int(chocolate) for chocolate in inp]
print (get_ans(chocolates))
```

{"mode":"full","isActive":false}

## Problem solution in Java.

```import java.io.BufferedReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.StringTokenizer;

public class Equal {
public static ArrayList<Integer> chocoDist ;
public static int steps ;

public static void main(String[] args) throws NumberFormatException, IOException
{
int testCases = Integer.parseInt( in.readLine() );
//System.out.println("testCases "+testCases);
int testCount = 0;

while( testCount < testCases)
{
chocoDist = new ArrayList<Integer>();
//System.out.println("coInterns "+coInterns+" chocoDist "+chocoDist.toString());
Collections.sort(chocoDist);
int sum = 0;
int sum1 = 1;
int sum2 = 1;
for(int i = 1 ; i < chocoDist.size() ; i++)	{
int diff = chocoDist.get(i) - chocoDist.get(0);
sum += diff/5 + (diff%5)/2 + (diff%5)%2/1;
diff+=1 ;
sum1 += diff/5 + (diff%5)/2 + (diff%5)%2/1;
diff+=1 ;
sum2 += diff/5 + (diff%5)/2 + (diff%5)%2/1;
}
System.out.println(Math.min(Math.min(sum, sum1), sum2));
}
}

}```

{"mode":"full","isActive":false}

## Problem solution in C++.

```#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>

#define MAX_N 10005
#define INF 1000000000

using namespace std;

int h[MAX_N],minn;

int find_min(int x) {
int res = INF;
int tmp = x / 5;
if ((x - 5 * tmp) % 2 == 0) res = min(res, tmp + (x - 5 * tmp) / 2);
else res = min(res, tmp + (x - 5 * tmp) / 2 + 1);
if (tmp >= 1) {
if ((x - 5 * tmp) % 2 == 1) res = min(res, tmp - 1 + (x + 5 - 5 * tmp) / 2);
else res = min(res, tmp - 1 + (x + 5 - 5 * tmp) / 2 + 1);
}
return res;
}

int main() {
scanf("%d", &tests);
for (int test = 0 ; test < tests ; test ++) {
scanf("%d", &n);
minn = INF;
for (int i = 0 ; i < n ; i ++) {
scanf("%d", &h[i]);
minn = min(minn, h[i]);
}
for (int tmp = 0 ; tmp < 20 ; tmp ++) {
s = 0;
for (int i = 0 ; i < n ; i ++) {
s += find_min(h[i] - minn + tmp);
}
}
}
return 0;
}
```

{"mode":"full","isActive":false}

## Problem solution in C.

```#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#define min(a,b) ((a < b) ? (a) : (b) )

//const int INF = (int)1e9;
int main() {

/* Enter your code here. Read input from STDIN. Print output to STDOUT */

int testcase,n,array[10000];
scanf("%d", &testcase);

while(testcase--)
{
scanf("%d",&n);
int sum, min1 = 10000000, sum1 = 100000000;

for(int i=0;i<n;i++)
{
scanf("%d",&array[i]);
min1 = min(min1, array[i]);
}

for(int k = min1; k >= (min1-5);k--)
{
sum = 0;
for(int i=0;i<n;i++)
{
int d = array[i]-k;
sum += d/5;
d %= 5;
sum += d/2;
d %= 2;
sum += d;
}
sum1 = min(sum1,sum);
}
printf("%d\n",sum1);

}

return 0;
}
```

{"mode":"full","isActive":false}