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

Self-Driving Bus

Treeland is a country with n cities and n - 1 roads. There is exactly one path between any two cities. The ruler of Treeland wants to implement a self-driving bus system and asks tree-loving Alex to plan the bus routes. Alex decides that each route must contain a subset of connected cities; a subset of cities is connected if the following two conditions are true: There is a path between ever

View Solution →

Unique Colors

You are given an unrooted tree of n nodes numbered from 1 to n . Each node i has a color, ci. Let d( i , j ) be the number of different colors in the path between node i and node j. For each node i, calculate the value of sum, defined as follows: Your task is to print the value of sumi for each node 1 <= i <= n. Input Format The first line contains a single integer, n, denoti

View Solution →

Fibonacci Numbers Tree

Shashank loves trees and math. He has a rooted tree, T , consisting of N nodes uniquely labeled with integers in the inclusive range [1 , N ]. The node labeled as 1 is the root node of tree , and each node in is associated with some positive integer value (all values are initially ). Let's define Fk as the Kth Fibonacci number. Shashank wants to perform 22 types of operations over his tree, T

View Solution →

Pair Sums

Given an array, we define its value to be the value obtained by following these instructions: Write down all pairs of numbers from this array. Compute the product of each pair. Find the sum of all the products. For example, for a given array, for a given array [7,2 ,-1 ,2 ] Note that ( 7 , 2 ) is listed twice, one for each occurrence of 2. Given an array of integers, find the largest v

View Solution →

Lazy White Falcon

White Falcon just solved the data structure problem below using heavy-light decomposition. Can you help her find a new solution that doesn't require implementing any fancy techniques? There are 2 types of query operations that can be performed on a tree: 1 u x: Assign x as the value of node u. 2 u v: Print the sum of the node values in the unique path from node u to node v. Given a tree wi

View Solution →

Ticket to Ride

Simon received the board game Ticket to Ride as a birthday present. After playing it with his friends, he decides to come up with a strategy for the game. There are n cities on the map and n - 1 road plans. Each road plan consists of the following: Two cities which can be directly connected by a road. The length of the proposed road. The entire road plan is designed in such a way that if o

View Solution →