# Minimum Operations 4 HackerRank Solution

Minimum Operations 4 HackerRank Solution

In this Minimum Operations 4 HackerRank solution, the task is to debug the existing code to successfully execute all provided test files.

There are  boxes in front of you. For each , box  contains  red balls,  green balls, and  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.

To restore the original code, click on the icon to the right of the language selector.

## Minimum Operations 4 HackerRank solution

I will Provide solution in Multiple programming languages for you. If you are not able to find the code in required language then please share in comments so that our team can help you.

### 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 |= 1;
if (red[i]) j |= 2;
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;
}
Here for this problem our task is to debug code so the solution works only for some languages, we do not need to write new code just to debug the old code.
Solve original Problem on HackerRank here. Checkout more HackerRank Problems