Count of arrangements of RGB balls with no duplicates in a set
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++
#include <bits/stdc++.h>
using namespace std;
int minimumSetBalls( int R, int G, int B)
{
vector< int > balls;
balls.push_back(R);
balls.push_back(G);
balls.push_back(B);
sort(balls.begin(), balls.end());
int ans = 0;
ans += balls[0];
balls[1] -= balls[0];
balls[2] -= balls[0];
balls[0] = 0;
ans += balls[1] / 3;
balls[1] %= 3;
ans += balls[2] / 3;
balls[2] %= 3;
if (balls[1] == 0
&& balls[2] == 0) {
}
else if (balls[1] == 1
&& balls[2] == 1) {
ans++;
}
else if ((balls[1] == 2
&& balls[2] == 0)
|| (balls[1] == 0
&& balls[2] == 2)) {
ans++;
}
else {
ans += 2;
}
return ans;
}
int main()
{
int R = 4, G = 2, B = 4;
cout << minimumSetBalls(R, G, B);
}
|
Java
import java.io.*;
import java.util.*;
public class GFG{
static int minimumSetBalls( int R, int G, int B)
{
ArrayList<Integer> balls = new ArrayList<Integer>();
balls.add(R);
balls.add(G);
balls.add(B);
Collections.sort(balls); ;
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 );
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 )
{
}
else if (balls.get( 1 ) == 1
&& balls.get( 2 ) == 1 ) {
ans++;
}
else if ((balls.get( 1 ) == 2
&& balls.get( 2 ) == 0 )
|| (balls.get( 1 ) == 0
&& balls.get( 2 ) == 2 )) {
ans++;
}
else {
ans += 2 ;
}
return ans;
}
public static void main(String []args)
{
int R = 4 , G = 2 , B = 4 ;
System.out.println( minimumSetBalls(R, G, B));
}
}
|
Python3
def minimumSetBalls(R, G, B) :
balls = [];
balls.append(R);
balls.append(G);
balls.append(B);
balls.sort();
ans = 0 ;
ans + = balls[ 0 ];
balls[ 1 ] - = balls[ 0 ];
balls[ 2 ] - = balls[ 0 ];
balls[ 0 ] = 0 ;
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 ;
elif ((balls[ 1 ] = = 2 and balls[ 2 ] = = 0 ) or (balls[ 1 ] = = 0 and balls[ 2 ] = = 2 )) :
ans + = 1 ;
else :
ans + = 2 ;
return ans;
if __name__ = = "__main__" :
R = 4 ; G = 2 ; B = 4 ;
print (minimumSetBalls(R, G, B));
|
C#
using System;
using System.Collections.Generic;
class GFG{
static int minimumSetBalls( int R, int G, int B)
{
List< int > balls = new List< int >();
balls.Add(R);
balls.Add(G);
balls.Add(B);
balls.Sort();
int ans = 0;
ans += balls[0];
balls[1] -= balls[0];
balls[2] -= balls[0];
balls[0] = 0;
ans += balls[1] / 3;
balls[1] %= 3;
ans += balls[2] / 3;
balls[2] %= 3;
if (balls[1] == 0
&& balls[2] == 0)
{
}
else if (balls[1] == 1
&& balls[2] == 1) {
ans++;
}
else if ((balls[1] == 2
&& balls[2] == 0)
|| (balls[1] == 0
&& balls[2] == 2)) {
ans++;
}
else {
ans += 2;
}
return ans;
}
public static void Main()
{
int R = 4, G = 2, B = 4;
Console.WriteLine( minimumSetBalls(R, G, B));
}
}
|
Javascript
<script>
function minimumSetBalls(R, G, B)
{
let balls = [];
balls.push(R);
balls.push(G);
balls.push(B);
balls.sort( function (a, b) { return a - b })
let ans = 0;
ans += balls[0];
balls[1] -= balls[0];
balls[2] -= balls[0];
balls[0] = 0;
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) {
}
else if (balls[1] == 1
&& balls[2] == 1) {
ans++;
}
else if ((balls[1] == 2
&& balls[2] == 0)
|| (balls[1] == 0
&& balls[2] == 2)) {
ans++;
}
else {
ans += 2;
}
return ans;
}
let R = 4, G = 2, B = 4;
document.write(minimumSetBalls(R, G, B));
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Last Updated :
17 Jan, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...