Skip to content
Programmingoneonone
Programmingoneonone
  • CS Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
    • Cybersecurity
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programmingoneonone
Programmingoneonone

HackerRank Equal problem solution

YASH PAL, 31 July 202425 January 2026

In this 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.

HackerRank Equal problem solution

HackerRank Equal problem solution in Python.

def g(diff):
    ans = {0:0, 1:1, 2:1, 3:2, 4:2}
    return diff // 5 + ans[diff % 5]

def f(chocolates, goal):
    return sum(g(chocolate-goal) for chocolate in chocolates)

def get_ans(chocolates):
    min_chocolate = min(chocolates)
    return min(f(chocolates, min_chocolate - dc) for dc in range(4))

T = int(input(""))
for i in range(T):
    N = int(input(""))
    inp = input("").split()
    chocolates = [int(chocolate) for chocolate in inp]
    print (get_ans(chocolates))

Equal problem solution in Java.

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.StringTokenizer;


public class Equal {
public static ArrayList<Integer> chocoDist ;
public static int steps ;


public static void main(String[] args) throws NumberFormatException, IOException
{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
int testCases = Integer.parseInt( in.readLine() );
//System.out.println("testCases "+testCases);
int testCount = 0;

while( testCount < testCases)
{
chocoDist = new ArrayList<Integer>();
int coInterns = Integer.parseInt(in.readLine()) ;
StringTokenizer st = new StringTokenizer(in.readLine());
while( st.hasMoreTokens()) chocoDist.add(Integer.parseInt(st.nextToken()));
//System.out.println("coInterns "+coInterns+" chocoDist "+chocoDist.toString());
Collections.sort(chocoDist);
int sum = 0;
int sum1 = 1;
int sum2 = 1;
for(int i = 1 ; i < chocoDist.size() ; i++) {
int diff = chocoDist.get(i) - chocoDist.get(0);
sum += diff/5 + (diff%5)/2 + (diff%5)%2/1;
diff+=1 ;
sum1 += diff/5 + (diff%5)/2 + (diff%5)%2/1;
diff+=1 ;
sum2 += diff/5 + (diff%5)/2 + (diff%5)%2/1;
}
System.out.println(Math.min(Math.min(sum, sum1), sum2));
}
}

}

Problem solution in C++.

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>

#define MAX_N 10005
#define INF 1000000000

using namespace std;

int tests,n,answer,s;
int h[MAX_N],minn;

int find_min(int x) {
    int res = INF;
    int tmp = x / 5;
    if ((x - 5 * tmp) % 2 == 0) res = min(res, tmp + (x - 5 * tmp) / 2);
    else res = min(res, tmp + (x - 5 * tmp) / 2 + 1);
    if (tmp >= 1) {
        if ((x - 5 * tmp) % 2 == 1) res = min(res, tmp - 1 + (x + 5 - 5 * tmp) / 2);
        else res = min(res, tmp - 1 + (x + 5 - 5 * tmp) / 2 + 1);
    }
    return res;
}

int main() {
    scanf("%d", &tests);
    for (int test = 0 ; test < tests ; test ++) {
        scanf("%d", &n);
        minn = INF;
        for (int i = 0 ; i < n ; i ++) {
            scanf("%d", &h[i]);
            minn = min(minn, h[i]);
        }
        answer = INF;
        for (int tmp = 0 ; tmp < 20 ; tmp ++) {
            s = 0;
            for (int i = 0 ; i < n ; i ++) {
                s += find_min(h[i] - minn + tmp);
            }
            answer = min(answer, s);
        }
        printf("%dn", answer);
    }
    return 0;
}

Problem solution in C.

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#define min(a,b) ((a < b) ? (a) : (b) )

//const int INF = (int)1e9;
int main() {

    /* Enter your code here. Read input from STDIN. Print output to STDOUT */    
    
int testcase,n,array[10000];
    scanf("%d", &testcase);
    
      while(testcase--)
     {
         scanf("%d",&n);
        int sum, min1 = 10000000, sum1 = 100000000;
         
         for(int i=0;i<n;i++)
         {
              scanf("%d",&array[i]);
              min1 = min(min1, array[i]);
         } 

       for(int k = min1; k >= (min1-5);k--)
       {
           sum = 0;
         for(int i=0;i<n;i++)
          {
             int d = array[i]-k;
              sum += d/5;
              d %= 5;
              sum += d/2;
              d %= 2;
              sum += d;
          }
         sum1 = min(sum1,sum);
     }    
        printf("%dn",sum1);
     
     }
    
    return 0;
}

Algorithms coding problems solutions AlgorithmsHackerRank

Post navigation

Previous post
Next post

Are you a student and stuck with your career or worried about real-time things, and don't know how to manage your learning phase? Which profession to choose? and how to learn new things according to your goal, and land a dream job. Then this might help to you.

Hi My name is YASH PAL, founder of this Blog and a Senior Software engineer with 5+ years of Industry experience. I personally helped 40+ students to make a clear goal in their professional lives. Just book a one-on-one personal call with me for 30 minutes for 300 Rupees. Ask all your doubts and questions related to your career to set a clear roadmap for your professional life.

Book session - https://wa.me/qr/JQ2LAS7AASE2M1

Pages

  • About US
  • Contact US
  • Privacy Policy

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes