**Equal**

### Problem Statement :

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 1, 2 or 5 pieces to all but one colleague. Everyone who gets a piece in a round receives the same number of pieces. Given a starting distribution, calculate the minimum number of operations needed so that every colleague has the same number of pieces. Example arr = [1, 1, 5] arr represents the starting numbers of pieces for each colleague. She can give 2 pieces to the first two and the distribution is then [3, 3, 5]. On the next round, she gives the same two 2 pieces each, and everyone has the same number: [5, 5, 5]. Return the number of rounds, 2. Function Description Complete the equal function in the editor below. equal has the following parameter(s): int arr[n]: the integers to equalize Returns int: the minimum number of operations required Input Format The first line contains an integer t, the number of test cases. Each test case has 2 lines. - The first line contains an integer n, the number of colleagues and the size of arr. - The second line contains n space-separated integers, arr[i], the numbers of pieces of chocolate each colleague has at the start. Constraints 1 <= t <= 100 1 <= n <= 10000 The number of chocolates each colleague has initially < 1000.

### Solution :

` ````
Solution in C :
In C++ :
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <map>
#include <utility>
#define MOD 1000000007
typedef long long LLD;
using namespace std;
int ar[20000];
int dp[2000];
int main()
{
int t, n;
cin >> t;
while(t--)
{
int mn = 1999;
cin >> n;
for(int i = 0; i < n; i ++)
{
cin >> ar[i];
mn = min(mn, ar[i]);
}
for(int i = 0; i < 2000; i ++)
dp[i] = MOD;
dp[0] = 0;
for(int i = 1; i < 1200; i ++)
{
dp[i] = min(dp[i], dp[i-1] + 1);
if(i - 2 >= 0)
dp[i] = min(dp[i], dp[i-2] + 1);
if(i - 5 >= 0)
dp[i] = min(dp[i], dp[i-5] + 1);
}
int steps = MOD;
for(int s = 0; s <= mn; s ++)
{
int ans = 0;
for(int i = 0; i < n; i ++)
ans += dp[ar[i] - s];
steps = min(steps, ans);
}
cout << steps << endl;
}
return 0;
}
In Java :
import java.io.BufferedInputStream;
import java.util.Scanner;
public class Solution {
static int offset = 100;
static Integer f[][] = new Integer[1111][1111];
static int a[] = new int[11111];
// From i down to j
static Integer F(int i, int j) {
if (i < j) return Integer.MAX_VALUE / 2;
if (f[i + offset][j + offset] != null) return f[i + offset][j + offset];
if (i == j) return 0;
int ans = Integer.MAX_VALUE / 2;
ans = Math.min(ans, F(i - 1, j) + 1);
ans = Math.min(ans, F(i - 2, j) + 1);
ans = Math.min(ans, F(i - 5, j) + 1);
return f[i + offset][j + offset] = ans;
}
public static void main(String[] args) {
Scanner cin = new Scanner(new BufferedInputStream(System.in));
for (int T = cin.nextInt(); T!=0; T--) {
int n = cin.nextInt();
int min = Integer.MAX_VALUE;
for (int i=0; i<n; i++) {
a[i] = cin.nextInt();
min = Math.min(min, a[i]);
}
int ans = Integer.MAX_VALUE;
for (int i=min; i>=min-30; i--) {
int tmp = 0;
for (int j=0; j<n; j++) {
tmp += F(a[j], i);
}
ans = Math.min(ans, tmp);
}
System.out.println(ans);
}
}
}
In C :
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int T;
scanf("%d\n", &T);
for(int t=0; t<T; t++) {
int N;
scanf("%d\n", &N);
int a[20000];
int min = 0x7fffffff;
for (int i=0; i<N; i++) {
scanf("%d", &a[i]);
if (a[i] < min) min = a[i];
}
int min_r = 0x7fffffff;
for( int t=-5; t <= 0; t++) {
int r = 0;
for(int i=0; i<N; i++) {
int diff = a[i] - (min+t);
r += diff / 5;
diff = diff % 5;
r += diff / 2;
diff = diff % 2;
r += diff;
}
if (r < min_r) min_r = r;
}
printf("%d\n", min_r);
}
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
return 0;
}
In Python3 :
def steps(number):
remainder = [0, 1, 1, 2, 2]
return number // 5 + remainder[number % 5]
T=int(input())
for loop in range(T):
N = int(input())
if N==0:
input()
print(0)
continue
distribution = list(map(int, input().split()))
distribution.sort()
minimum = distribution[0]
totalsteps = [0,1,1,2,2]
for i in range(5):
for element in distribution[1:]:
totalsteps[i] += steps(element-minimum+i)
print(min(totalsteps))
```

## View More Similar Problems

## Find Merge Point of Two Lists

This challenge is part of a tutorial track by MyCodeSchool Given pointers to the head nodes of 2 linked lists that merge together at some point, find the node where the two lists merge. The merge point is where both lists point to the same node, i.e. they reference the same memory location. It is guaranteed that the two head nodes will be different, and neither will be NULL. If the lists share

View Solution →## Inserting a Node Into a Sorted Doubly Linked List

Given a reference to the head of a doubly-linked list and an integer ,data , create a new DoublyLinkedListNode object having data value data and insert it at the proper location to maintain the sort. Example head refers to the list 1 <-> 2 <-> 4 - > NULL. data = 3 Return a reference to the new list: 1 <-> 2 <-> 4 - > NULL , Function Description Complete the sortedInsert function

View Solution →## Reverse a doubly linked list

This challenge is part of a tutorial track by MyCodeSchool Given the pointer to the head node of a doubly linked list, reverse the order of the nodes in place. That is, change the next and prev pointers of the nodes so that the direction of the list is reversed. Return a reference to the head node of the reversed list. Note: The head node might be NULL to indicate that the list is empty.

View Solution →## Tree: Preorder Traversal

Complete the preorder function in the editor below, which has 1 parameter: a pointer to the root of a binary tree. It must print the values in the tree's preorder traversal as a single line of space-separated values. Input Format Our test code passes the root node of a binary tree to the preOrder function. Constraints 1 <= Nodes in the tree <= 500 Output Format Print the tree's

View Solution →## Tree: Postorder Traversal

Complete the postorder function in the editor below. It received 1 parameter: a pointer to the root of a binary tree. It must print the values in the tree's postorder traversal as a single line of space-separated values. Input Format Our test code passes the root node of a binary tree to the postorder function. Constraints 1 <= Nodes in the tree <= 500 Output Format Print the

View Solution →## Tree: Inorder Traversal

In this challenge, you are required to implement inorder traversal of a tree. Complete the inorder function in your editor below, which has 1 parameter: a pointer to the root of a binary tree. It must print the values in the tree's inorder traversal as a single line of space-separated values. Input Format Our hidden tester code passes the root node of a binary tree to your $inOrder* func

View Solution →