Count of arrangements of RGB balls with no duplicates in a set - GeeksforGeeks

17 Jan, 2022
Given R red balls, G green balls, B blue balls. A set can have 1, 2, or 3 balls. Also, a set could have all balls of the same color or all balls of different colors. All other possible sets are not considered valid. The task is to calculate the minimum possible sets required to place all balls.
Examples: 
Input: R = 4, G = 2, B = 4Output: 4Explanation: There can be 4 sets which satisfies all condition mentioned above {R, R, R}, {B, B, B}, {G, R}, {G, B}.Therefore, the answer is 4. 
Input: R = 1, G = 7, B = 1Output: 3Explanation: There are 3 valid sets {R, G, B}, {G, G, G}, {G, G, G}
Approach: This problem is analysis-based. Follow the steps below to solve the given problem. 
The problem statement can be solved with careful case analysis.
Without loss of generality assume that R<=G<=B.
So there will be at least R sets formed. Subtract R from G and B. All the sets formed from this step will have different balls in them.
The remaining balls will be 0, G – R, B – R. For the remaining balls form sets of the same balls.
After the above step remaining balls will be 0, (G – R)%3, (B – R)%3.
Now there can be 3 cases:
2nd term is 0 and 3rd term is zero. No extra set will be required.
(2nd term is 1 and 3rd term is 1) or (2nd term is 0 and 3rd term is 2 or vice versa). 1 extra set will be required.
In all other cases, 2 more sets will be required.
Check all the above conditions carefully and print the answer.
Below is the implementation of the above approach:
C++
Java
Python3
C#
Javascript
// C++ Program to implement// the above approach#include <bits/stdc++.h>using namespace std; // Function to calculate minimum sets// required with given conditionsint minimumSetBalls(int R, int G, int B){    // Push values R, G, B    // in a vector and sort them    vector<int> balls;    balls.push_back(R);    balls.push_back(G);    balls.push_back(B);     // Sort the vector    sort(balls.begin(), balls.end());     // Store the answer    int ans = 0;    ans += balls[0];    balls[1] -= balls[0];    balls[2] -= balls[0];    balls[0] = 0;     // Check all mentioned conditions    ans += balls[1] / 3;    balls[1] %= 3;    ans += balls[2] / 3;    balls[2] %= 3;     if (balls[1] == 0        && balls[2] == 0) {        // No extra set required    }    else if (balls[1] == 1             && balls[2] == 1) {        ans++;        // 1 extra set is required    }    else if ((balls[1] == 2              && balls[2] == 0)             || (balls[1] == 0                 && balls[2] == 2)) {        ans++;        // 1 extra set is required    }    else {        // 2 extra sets will be required        ans += 2;    }    return ans;} // Driver Codeint main(){    int R = 4, G = 2, B = 4;    cout << minimumSetBalls(R, G, B);}
// Java program for the above approachimport java.io.*;import java.util.*; public class GFG{ // Function to calculate minimum sets// required with given conditionsstatic int minimumSetBalls(int R, int G, int B){       // Push values R, G, B    // in a vector and sort them    ArrayList<Integer> balls = new ArrayList<Integer>();    balls.add(R);    balls.add(G);    balls.add(B);       // Sort the vector    Collections.sort(balls);   ;       // Store the answer    int ans = 0;    ans += balls.get(0);    balls.set(1,balls.get(1) - balls.get(0));    balls.set(2,balls.get(2) - balls.get(0));    balls.set(0,0);       // Check all mentioned conditions    ans += balls.get(1) / 3;    balls.set(1, balls.get(1) % 3);    ans += balls.get(2) / 3;    balls.set(2,balls.get(2) % 3);       if (balls.get(1) == 0        && balls.get(2) == 0)    {               // No extra set required    }    else if (balls.get(1) == 1             && balls.get(2) == 1) {        ans++;        // 1 extra set is required    }    else if ((balls.get(1) == 2              && balls.get(2) == 0)             || (balls.get(1) == 0                 && balls.get(2) == 2)) {        ans++;        // 1 extra set is required    }    else {        // 2 extra sets will be required        ans += 2;    }    return ans;}  // Driver Codepublic static void main(String []args){    int R = 4, G = 2, B = 4;    System.out.println( minimumSetBalls(R, G, B));}} // This code is contributed by AnkThon
# Python3 Program to implement the above approach # Function to calculate minimum sets# required with given conditionsdef minimumSetBalls(R, G, B) :     # Push values R, G, B    # in a vector and sort them    balls = [];    balls.append(R);    balls.append(G);    balls.append(B);     # Sort the vector    balls.sort();     # Store the answer    ans = 0;    ans += balls[0];    balls[1] -= balls[0];    balls[2] -= balls[0];    balls[0] = 0;     # Check all mentioned conditions    ans += balls[1] // 3;    balls[1] %= 3;    ans += balls[2] // 3;    balls[2] %= 3;     if (balls[1] == 0 and balls[2] == 0) :        pass         elif (balls[1] == 1 and balls[2] == 1) :        ans += 1;        # 1 extra set is required     elif ((balls[1] == 2 and balls[2] == 0) or (balls[1] == 0 and balls[2] == 2)) :        ans += 1;        # 1 extra set is required    else :        # 2 extra sets will be required        ans += 2;     return ans; # Driver Codeif __name__ == "__main__" :     R = 4; G = 2; B = 4;    print(minimumSetBalls(R, G, B));         # This code is contributed by AnkThon
// C# program for the above approachusing System;using System.Collections.Generic; class GFG{ // Function to calculate minimum sets// required with given conditionsstatic int minimumSetBalls(int R, int G, int B){       // Push values R, G, B    // in a vector and sort them    List<int> balls = new List<int>();    balls.Add(R);    balls.Add(G);    balls.Add(B);       // Sort the vector    balls.Sort();       // Store the answer    int ans = 0;    ans += balls[0];    balls[1] -= balls[0];    balls[2] -= balls[0];    balls[0] = 0;       // Check all mentioned conditions    ans += balls[1] / 3;    balls[1] %= 3;    ans += balls[2] / 3;    balls[2] %= 3;       if (balls[1] == 0        && balls[2] == 0)    {               // No extra set required    }    else if (balls[1] == 1             && balls[2] == 1) {        ans++;        // 1 extra set is required    }    else if ((balls[1] == 2              && balls[2] == 0)             || (balls[1] == 0                 && balls[2] == 2)) {        ans++;        // 1 extra set is required    }    else {        // 2 extra sets will be required        ans += 2;    }    return ans;}  // Driver Codepublic static void Main(){    int R = 4, G = 2, B = 4;    Console.WriteLine( minimumSetBalls(R, G, B));}} // This code is contributed by sanjoy_62.
<script>         // JavaScript Program to implement        // the above approach         // Function to calculate minimum sets        // required with given conditions        function minimumSetBalls(R, G, B)        {                     // Push values R, G, B            // in a vector and sort them            let balls = [];            balls.push(R);            balls.push(G);            balls.push(B);             // Sort the vector            balls.sort(function (a, b) { return a - b })             // Store the answer            let ans = 0;            ans += balls[0];            balls[1] -= balls[0];            balls[2] -= balls[0];            balls[0] = 0;             // Check all mentioned conditions            ans += Math.floor(balls[1] / 3);            balls[1] %= 3;            ans += Math.floor(balls[2] / 3);            balls[2] %= 3;             if (balls[1] == 0                && balls[2] == 0) {                // No extra set required            }            else if (balls[1] == 1                && balls[2] == 1) {                ans++;                // 1 extra set is required            }            else if ((balls[1] == 2                && balls[2] == 0)                || (balls[1] == 0                    && balls[2] == 2)) {                ans++;                // 1 extra set is required            }            else {                // 2 extra sets will be required                ans += 2;            }            return ans;        }         // Driver Code        let R = 4, G = 2, B = 4;        document.write(minimumSetBalls(R, G, B));     // This code is contributed by Potta Lokesh    </script>
4
 
Time Complexity: O(1) Auxiliary Space: O(1)
sanjoy_62
ankthon
lokeshpotta20
simranarora5sos
Combinatorial
Greedy
Greedy
Combinatorial
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Ways to sum to N using Natural Numbers up to K with repetitions allowed
Combinations with repetitions
Generate all possible combinations of K numbers that sums to N
Largest substring with same Characters
Generate all possible combinations of at most X characters from a given array
Dijkstra's shortest path algorithm | Greedy Algo-7
Prim’s Minimum Spanning Tree (MST) | Greedy Algo-5
Kruskal’s Minimum Spanning Tree Algorithm | Greedy Algo-2
Program for array rotation
Huffman Coding | Greedy Algo-3