**Minimum Operations**

### Problem Statement :

In this challenge, the task is to debug the existing code to successfully execute all provided test files. 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. To restore the original code, click on the icon to the right of the language selector. Input Format The first line contains a single integer n. The next n lines i contain three space-separated integers, r[i], g[i], and b[i], respectively. Constraints 1 <= n <= 100 0 <= r[i], g[i], b[i] <= 105 Output Format Print the minimal number of operations required to separate the balls. If this is impossible, return -1.

### Solution :

` ````
Solution in C :
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 <= 7; j++) {
dp[i][j] = 1<<30;
}
}
dp[0][0] = 0;
for (i = 0; i < n; i++){
for (j = 0; j <= 7; 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 (red[i]) j |= 1;
if (green[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;
}
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 <= 7; j++) {
dp[i][j] = (1<<30);
}
}
dp[0][0] = 0;
for (i = 0; i < n; i++){
for (j = 0; j <= 7; 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 |= 1;
if (red.get(i) != 0) j |= 2;
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();
}
}
In Python3 :
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;
}
```

## View More Similar Problems

## Balanced Brackets

A bracket is considered to be any one of the following characters: (, ), {, }, [, or ]. Two brackets are considered to be a matched pair if the an opening bracket (i.e., (, [, or {) occurs to the left of a closing bracket (i.e., ), ], or }) of the exact same type. There are three types of matched pairs of brackets: [], {}, and (). A matching pair of brackets is not balanced if the set of bra

View Solution →## Equal Stacks

ou have three stacks of cylinders where each cylinder has the same diameter, but they may vary in height. You can change the height of a stack by removing and discarding its topmost cylinder any number of times. Find the maximum possible height of the stacks such that all of the stacks are exactly the same height. This means you must remove zero or more cylinders from the top of zero or more of

View Solution →## Game of Two Stacks

Alexa has two stacks of non-negative integers, stack A = [a0, a1, . . . , an-1 ] and stack B = [b0, b1, . . . , b m-1] where index 0 denotes the top of the stack. Alexa challenges Nick to play the following game: In each move, Nick can remove one integer from the top of either stack A or stack B. Nick keeps a running sum of the integers he removes from the two stacks. Nick is disqualified f

View Solution →## Largest Rectangle

Skyline Real Estate Developers is planning to demolish a number of old, unoccupied buildings and construct a shopping mall in their place. Your task is to find the largest solid area in which the mall can be constructed. There are a number of buildings in a certain two-dimensional landscape. Each building has a height, given by . If you join adjacent buildings, they will form a solid rectangle

View Solution →## Simple Text Editor

In this challenge, you must implement a simple text editor. Initially, your editor contains an empty string, S. You must perform Q operations of the following 4 types: 1. append(W) - Append W string to the end of S. 2 . delete( k ) - Delete the last k characters of S. 3 .print( k ) - Print the kth character of S. 4 . undo( ) - Undo the last (not previously undone) operation of type 1 or 2,

View Solution →## Poisonous Plants

There are a number of plants in a garden. Each of the plants has been treated with some amount of pesticide. After each day, if any plant has more pesticide than the plant on its left, being weaker than the left one, it dies. You are given the initial values of the pesticide in each of the plants. Determine the number of days after which no plant dies, i.e. the time after which there is no plan

View Solution →