HackerRank Equal Problem Solution
In this post, we will solve HackerRank Equal Problem Solution.
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
Sample Input
STDIN Function
----- --------
1 t = 1
4 arr[] size n = 4
2 2 3 7 arr =[2, 2, 3, 7]
Sample Output
2
Explanation
Start with [2, 2, 3, 7]
Add 1 to all but the 3rd element→ [3, 3, 3, 8]
Add 5 to all but the 4th element → [8, 8, 8, 8]
Two operations were required.
Sample Input 1
1
3
10 7 12
Sample Output 1
3
Explanation 1
Start with [10, 7, 12]
Add 5 to the first two elements [15, 12, 12]
Add 2 to the last two elements [15, 14, 14]
Add 1 to the last two elements → [15, 15, 15]
Three operations were required.
Equal C Solution
#include<stdio.h>
int main()
{
int T, j, i, N, V;
scanf("%d",&T);
while(T>0)
{
scanf("%d",&N);
int A[N];
for(i=0;i<N;++i)
{
scanf("%d",&A[i]);
}
int minVal = A[0];
for(i = 1; i < N; ++i)
{
if (A[i] < minVal)
{
minVal = A[i];
}
}
int minCount = 9999999;
for(i = 0; i <= 5; ++i)
{
int count = 0;
for(j = 0; j < N; ++j)
{
V = (A[j] - (minVal - i));
count += V/5 + (V %= 5)/2 + (V & 1);
}
if (count < minCount)
{
minCount = count;
}
}
printf("%d\n",minCount);
T--;
}
return 0;
}
Equal C++ Solution
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int func(int n){
return((n/5) + ((n%5)/2) + ((n%5)%2) );
}
int main() {
int arr[10005],N,T,min;
cin>>T;
for(int tst=0;tst<T;tst++){
cin>>N;
min=99999;
for(int i=0;i<N;i++){
cin>>arr[i];
if(arr[i]<min)min=arr[i];
}
int res=99999999;
for(int k=0;k<=2;k++){
int x=0;
for(int i=0;i<N;i++){
x+=func(arr[i]-min+k);
}
if(x<res) res=x;
}
cout<<res<<endl;
}
return 0;
}
Equal C Sharp Solution
using System;
/*
Sample Input
1
4
2 2 3 7
Sample Output
2
Sample Input#2
1
3
1 5 5
Sample Output#2
*/
class Solution
{
static void Main(String[] args)
{
(new Solution()).solve();
}
public void solve()
{
int T = int.Parse(Console.ReadLine());
for (int t = 0; t < T; t++)
{
int N = int.Parse(Console.ReadLine());
string[] splitted = Console.ReadLine().Split();
int[] a = new int[N];
int i, minV = int.MaxValue;
long answer = long.MaxValue;
for (i = 0; i < N; i++)
{
int v = int.Parse(splitted[i]);
a[i] = v;
if (v < minV)
minV = v;
}
for (int adj = 0; adj <= 2; adj++)
{
long nOps = 0;
for (i = 0; i < N; i++)
{
int diff = a[i] - (minV - adj);
if (diff > 0)
{
nOps += diff / 5;
diff %= 5;
nOps += diff / 2;
diff %= 2;
nOps += diff;
}
}
answer = Math.Min(answer, nOps);
}
Console.WriteLine(answer);
}
Console.ReadLine();
}
}
Equal Java Solution
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.function.*;
import java.util.regex.*;
import java.util.stream.*;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;
class Result {
/*
* Complete the 'equal' function below.
*
* The function is expected to return an INTEGER.
* The function accepts INTEGER_ARRAY arr as parameter.
*/
public static int equal(List<Integer> arr) {
// Write your code here
int[] p=new int[5];
int min=Collections.min(arr);
for(int i=0;i<5;i++)
{
p[i]=0;
for(int vl : arr)
{
int v=vl-min;
int po=v/5+v%5/2+(v%5)%2;
p[i]+=po;
}
--min;
}
return Arrays.stream(p).min().getAsInt();
}
}
public class Solution {
public static void main(String[] args) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));
int t = Integer.parseInt(bufferedReader.readLine().trim());
IntStream.range(0, t).forEach(tItr -> {
try {
int n = Integer.parseInt(bufferedReader.readLine().trim());
List<Integer> arr = Stream.of(bufferedReader.readLine().replaceAll("\\s+$", "").split(" "))
.map(Integer::parseInt)
.collect(toList());
int result = Result.equal(arr);
bufferedWriter.write(String.valueOf(result));
bufferedWriter.newLine();
} catch (IOException ex) {
throw new RuntimeException(ex);
}
});
bufferedReader.close();
bufferedWriter.close();
}
}
Equal JavaScript Solution
process.stdin.resume();
process.stdin.setEncoding("ascii");
var input = "";
process.stdin.on("data", function (chunk) {
input += chunk;
});
process.stdin.on("end", function () {
// now we can read/parse input
input_array = input.split('\n');
var numTests = parseInt(input_array[0]);
var currLine = 1;
for(var i = 0; i < numTests; i++) {
var n = parseInt(input_array[currLine]);
var chocolates = input_array[currLine+1].split(' ').map(Number);
//console.log(n);
minNumOps(n, chocolates);
currLine += 2;
}
});
function minNumOps(n, chocolates) {
var numOps = 0;
var minElement = Math.min.apply(null, chocolates);
var chocoRems = [0,0,0,0,0];
for(i = 0; i < n; i++) {
var d = (chocolates[i] - minElement);
numOps += Math.floor(d / 5);
chocoRems[d % 5] += 1;
}
numOps += Math.min(chocoRems[1] + chocoRems[2] + 2 * chocoRems[3] + 2 * chocoRems[4],
chocoRems[0] + chocoRems[1] + 2 * chocoRems[2] + 2 * chocoRems[3] + chocoRems[4],
chocoRems[0] + 2 * chocoRems[1] + 2 * chocoRems[2] + chocoRems[3] + 2 * chocoRems[4])
console.log(numOps);
}
function numOpsForDiff(d) {
var numOps = 0;
var d_rem = d % 5;
numOps = (d - d_rem) / 5;
return numOps
}
Equal Python Solution
sols = [0, 1, 1, 2, 2, 1]
cases = int(input())
def getAnswer(num):
return sols[num]
def getMinDistributions(minIntern, cointerns):
# minIntern = min(cointerns)
total = 0
for i in cointerns:
i -= minIntern
total += int(i / 5)
total += getAnswer(i % 5)
return total
for case in range(cases):
interns = int(input())
cointerns = [int(i) for i in input().split()]
minIntern = min(cointerns)
print(min(getMinDistributions(minIntern - i, cointerns) for i in range(5)))