Skip to content
Related Articles

Related Articles

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

Improve Article
Save Article
Like Article
  • Last Updated : 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 = 4
Output: 4
Explanation: 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 = 1
Output: 3
Explanation: 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++




// C++ Program to implement
// the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to calculate minimum sets
// required with given conditions
int 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 Code
int main()
{
    int R = 4, G = 2, B = 4;
    cout << minimumSetBalls(R, G, B);
}

Java




// Java program for the above approach
import java.io.*;
import java.util.*;
 
public class GFG{
 
// Function to calculate minimum sets
// required with given conditions
static 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 Code
public 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




# Python3 Program to implement the above approach
 
# Function to calculate minimum sets
# required with given conditions
def 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 Code
if __name__ == "__main__" :
 
    R = 4; G = 2; B = 4;
    print(minimumSetBalls(R, G, B));
     
    # This code is contributed by AnkThon

C#




// C# program for the above approach
using System;
using System.Collections.Generic;
 
class GFG{
 
// Function to calculate minimum sets
// required with given conditions
static 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 Code
public static void Main()
{
    int R = 4, G = 2, B = 4;
    Console.WriteLine( minimumSetBalls(R, G, B));
}
}
 
// This code is contributed by sanjoy_62.

Javascript




<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>
Output: 
4

 

Time Complexity: O(1) 
Auxiliary Space: O(1)


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!