Christy is interning at HackerRank. One day she has to distribute some chocolates to her colleagues. She is biased towards her friends and may have distributed the chocolates unequally. One of the program managers gets to know this and orders Christy to make sure everyone gets equal number of chocolates.
But to make things difficult for the intern, she is ordered to equalize the number of chocolates for every colleague in the following manner,
For every operation, she can choose one of her colleagues and can do one of the three things.
- She can give one chocolate to every colleague other than chosen one.
- She can give two chocolates to every colleague other than chosen one.
- She can give five chocolates to every colleague other than chosen one.
Calculate minimum number of such operations needed to ensure that every colleague has the same number of chocolates.
Input Format
First line contains an integer denoting the number of testcases. testcases follow.
Each testcase has lines. First line of each testcase contains an integer denoting the number of colleagues. Second line contains N space separated integers denoting the current number of chocolates each colleague has.
Each testcase has lines. First line of each testcase contains an integer denoting the number of colleagues. Second line contains N space separated integers denoting the current number of chocolates each colleague has.
Constraints
Number of initial chocolates each colleague has <
Output Format
lines, each containing the minimum number of operations needed to make sure all colleagues have the same number of chocolates.
Sample Input
1
4
2 2 3 7
Sample Output
2
Explanation
1st operation: Christy increases all elements by 1 except 3rd one
2 2 3 7 -> 3 3 3 8
2nd operation: Christy increases all element by 5 except last one
3 3 3 8 -> 8 8 8 8
2 2 3 7 -> 3 3 3 8
2nd operation: Christy increases all element by 5 except last one
3 3 3 8 -> 8 8 8 8
import java.util.Scanner;
/**
* Created by vdokku on 7/29/2017.
*/
public class Equal {
static boolean DBG = false;
// Min number in the Array.
static int min;
//Find the minimised action counts
public static int MinRound(int[] counts) {
int[][] results = new int[counts.length][3];
for (int i = 0; i < counts.length; i++) {
for (int j = 0; j < 3; j++) {
int delta = counts[i] - min + j; // calculating the DELTA is important.
results[i][j] = 0;
while (true) {
// Greedy approach
if (delta >= 5) {
delta -= 5;
results[i][j]++;
} else if (delta >= 2) {
delta -= 2;
results[i][j]++;
} else if (delta >= 1) {
delta -= 1;
results[i][j]++;
} else {
break;
}
}
}
}
int finalResult = -1;
// Compare results from different baseline cases (keep min, take 1, 2 ).
for (int i = 0; i < 3; i++) {
int subFinal = 0;
for (int j = 0; j < counts.length; j++) {
subFinal += results[j][i];
if (DBG) System.out.format("results[%d][%d] = %d \n", j, i, results[j][i]);
}
if (DBG) System.out.println(subFinal);
if (finalResult < 0 || finalResult > subFinal) {
finalResult = subFinal;
}
}
return finalResult;
}
public static void main(String[] args) {
int casesCount = 0;
Scanner s = new Scanner(System.in);
if (s.hasNextInt()) {
casesCount = s.nextInt();
}
s.nextLine(); // throw away the newline.
int[] outputArrary = new int[casesCount];
for (int i = 0; i < casesCount; i++) {
int count = 0;
if (s.hasNextInt()) {
count = s.nextInt();
}
s.nextLine();
int[] numbers = new int[count];
min = -1;
for (int j = 0; j < count; j++) {
if (s.hasNextInt()) {
numbers[j] = s.nextInt();
// get min value from input array
if (numbers[j] < min || min < 0) {
min = numbers[j];
}
} else {
System.out.println("You didn't provide enough numbers");
break;
}
}
//SortCounts(numbers);
outputArrary[i] = MinRound(numbers);
}
for (int i = 0; i < casesCount; i++) {
System.out.println(outputArrary[i]);
}
}
}
0 comments:
Post a Comment