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 :



title-img


                            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

Tree: Height of a Binary Tree

The height of a binary tree is the number of edges between the tree's root and its furthest leaf. For example, the following binary tree is of height : image Function Description Complete the getHeight or height function in the editor. It must return the height of a binary tree as an integer. getHeight or height has the following parameter(s): root: a reference to the root of a binary

View Solution →

Tree : Top View

Given a pointer to the root of a binary tree, print the top view of the binary tree. The tree as seen from the top the nodes, is called the top view of the tree. For example : 1 \ 2 \ 5 / \ 3 6 \ 4 Top View : 1 -> 2 -> 5 -> 6 Complete the function topView and print the resulting values on a single line separated by space.

View Solution →

Tree: Level Order Traversal

Given a pointer to the root of a binary tree, you need to print the level order traversal of this tree. In level-order traversal, nodes are visited level by level from left to right. Complete the function levelOrder and print the values in a single line separated by a space. For example: 1 \ 2 \ 5 / \ 3 6 \ 4 F

View Solution →

Binary Search Tree : Insertion

You are given a pointer to the root of a binary search tree and values to be inserted into the tree. Insert the values into their appropriate position in the binary search tree and return the root of the updated binary tree. You just have to complete the function. Input Format You are given a function, Node * insert (Node * root ,int data) { } Constraints No. of nodes in the tree <

View Solution →

Tree: Huffman Decoding

Huffman coding assigns variable length codewords to fixed length input characters based on their frequencies. More frequent characters are assigned shorter codewords and less frequent characters are assigned longer codewords. All edges along the path to a character contain a code digit. If they are on the left side of the tree, they will be a 0 (zero). If on the right, they'll be a 1 (one). Only t

View Solution →

Binary Search Tree : Lowest Common Ancestor

You are given pointer to the root of the binary search tree and two values v1 and v2. You need to return the lowest common ancestor (LCA) of v1 and v2 in the binary search tree. In the diagram above, the lowest common ancestor of the nodes 4 and 6 is the node 3. Node 3 is the lowest node which has nodes and as descendants. Function Description Complete the function lca in the editor b

View Solution →