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

Median Updates

The median M of numbers is defined as the middle number after sorting them in order if M is odd. Or it is the average of the middle two numbers if M is even. You start with an empty number list. Then, you can add numbers to the list, or remove existing numbers from it. After each add or remove operation, output the median. Input: The first line is an integer, N , that indicates the number o

View Solution →

Maximum Element

You have an empty sequence, and you will be given N queries. Each query is one of these three types: 1 x -Push the element x into the stack. 2 -Delete the element present at the top of the stack. 3 -Print the maximum element in the stack. Input Format The first line of input contains an integer, N . The next N lines each contain an above mentioned query. (It is guaranteed that each

View Solution →

Balanced Brackets

A bracket is considered to be any one of the following characters: (, ), {, }, [, or ]. Two brackets are considered to be a matched pair if the an opening bracket (i.e., (, [, or {) occurs to the left of a closing bracket (i.e., ), ], or }) of the exact same type. There are three types of matched pairs of brackets: [], {}, and (). A matching pair of brackets is not balanced if the set of bra

View Solution →

Equal Stacks

ou have three stacks of cylinders where each cylinder has the same diameter, but they may vary in height. You can change the height of a stack by removing and discarding its topmost cylinder any number of times. Find the maximum possible height of the stacks such that all of the stacks are exactly the same height. This means you must remove zero or more cylinders from the top of zero or more of

View Solution →

Game of Two Stacks

Alexa has two stacks of non-negative integers, stack A = [a0, a1, . . . , an-1 ] and stack B = [b0, b1, . . . , b m-1] where index 0 denotes the top of the stack. Alexa challenges Nick to play the following game: In each move, Nick can remove one integer from the top of either stack A or stack B. Nick keeps a running sum of the integers he removes from the two stacks. Nick is disqualified f

View Solution →

Largest Rectangle

Skyline Real Estate Developers is planning to demolish a number of old, unoccupied buildings and construct a shopping mall in their place. Your task is to find the largest solid area in which the mall can be constructed. There are a number of buildings in a certain two-dimensional landscape. Each building has a height, given by . If you join adjacent buildings, they will form a solid rectangle

View Solution →