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 Minimum Operations problem solution

YASH PAL, 31 July 202426 January 2026

In this HackerRank Minimum Operations problem solution, There are n boxes in front of you. For each i, box i contains r[i] red balls, g[i] green balls, and b[i] blue balls.

You want to separate the balls by their color. In each operation, you can pick a single ball from some box and put it into another box. The balls are separated if no box contains balls of more than one color.

Debug the given function min_operations and compute the minimal number of operations required to separate the balls.

Note: In this problem you can modify at most six lines of code and you cannot add any new lines.

HackerRank Minimum Operations problem solution in C++.

#include <cstdio> 
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
#include <iostream>
#include <map>
using namespace std;

int dp[110][1<<3];

int min_operations(vector <int> red, vector <int> green, vector <int> blue) {

int n = (int)red.size(), i, j;
for (i = 0; i <= n; i++) {
for (j = 0; j < 8; j++) {
dp[i][j] = 1<<30;
}
}

dp[0][0] = 0;
for (i = 0; i < n; i++){
for (j = 0; j < 8; j++){
dp[i + 1][j | 1] = min(dp[i + 1][j | 1], dp[i][j] + green[i] + blue[i]);
dp[i + 1][j | 2] = min(dp[i + 1][j | 2], dp[i][j] + red[i] + blue[i]);
dp[i + 1][j | 4] = min(dp[i + 1][j | 4], dp[i][j] + red[i] + green[i]);
}
}
j = 0;
for (i = 0; i < n; i++){
if (green[i]) j |= 2;
if (red[i]) j |= 1;
if (blue[i]) j |= 4;
}

if (dp[n][j] >= (1<<30))
dp[n][j] = -1;

return dp[n][j];
}

int main() {

int n, r, g, b;
cin >> n;
vector<int> red, blue, green;

for(int i = 0; i < n; i++){

cin >> r >> g >> b;
red.push_back(r);
green.push_back(g);
blue.push_back(b);
}

cout << min_operations(red, green, blue) << "n";
return 0;
}

Minimum Operations problem solution in Java

import java.util.*;

class MinimumOperations {
    private static final Scanner scan = new Scanner(System.in);
    int n, r ,g, b;
    int[][] dp = new int[110][1<<3];

    Vector<Integer> red = new Vector();
    Vector<Integer> green = new Vector();
    Vector<Integer> blue = new Vector();

    public void get() {
         n = scan.nextInt();

        for (int i = 0; i < n; i++) {
            r = scan.nextInt();
            g = scan.nextInt();
            b = scan.nextInt();
            red.add(r);
            green.add(g);
            blue.add(b);
        }
    }

    public void minOperations() {
        int i, j;
        for (i = 0; i <= n; i++) {
            for (j = 0; j < 8; j++) {
                dp[i][j] = (1<<30);
            }
        }
        dp[0][0] = 0;
        for (i = 0; i < n; i++){
            for (j = 0; j < 8; j++){
                dp[i + 1][j | 1] = Math.min(dp[i + 1][j | 1], dp[i][j] + green.get(i) + blue.get(i));
                dp[i + 1][j | 2] = Math.min(dp[i + 1][j | 2], dp[i][j] + red.get(i) + blue.get(i));
                dp[i + 1][j | 4] = Math.min(dp[i + 1][j | 4], dp[i][j] + red.get(i) + green.get(i));
            }
        }
        j = 0;
        for (i = 0; i < n; i++){
            if (green.get(i) != 0) j |= 2;
            if (red.get(i) != 0) j |= 1;
            if (blue.get(i) != 0) j |= 4;
        }
        if (dp[n][j] >= (1<<30)) dp[n][j] = -1;
            System.out.println(dp[n][j]);
    }
}
class Solution {
    public static void main(String[] args) {
        MinimumOperations obj = new MinimumOperations();
        obj.get();
        obj.minOperations();
    }
}


Algorithms coding problems solutions AlgorithmsHackerRank

Post navigation

Previous post
Next post
CLOSE ADS
CLOSE ADS

Pages

  • About US
  • Contact US
  • Privacy Policy

Follow US

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