Given r red, b blue, and g green balls, find the total number of arrangements in a row such that no two balls of the same color end up together.

For example,

Input:  r = 1, b = 2, g = 1
 
Output: Total number of arrangements are 6
 
The arrangements are [bgbr, bgrb, brbg, brgb, gbrb, rbgb]
 
 
Input:  r = 2, b = 3, g = 1
 
Output: Total number of arrangements are 10
 
The arrangements are [bgbrbr, bgrbrb, brbgbr, brbgrb, brbrbg, brbrgb, brgbrb, gbrbrb, rbgbrb, rbrbgb]

Practice this problem

Let f(r, b, g, c) denote the total number of the arrangements where r red, b blue, and g green balls are left, and the color of the last ball was c. Now, there are three possible cases:

  1. If the last ball taken was red, then the total number of ways is
     
    f(r, b, g, red) = f(r, b - 1, g, blue) + f(r, b, g - 1, green)
  2. If the last ball taken was blue, then the total number of ways is
     
    f(r, b, g, blue) = f(r - 1, b, g, red) + f(r, b, g - 1, green)
  3. If the last ball taken was green, then the total number of ways is
     
    f(r, b, g, green) = f(r - 1, b, g, red) + f(r, b - 1, g, blue)

Following is the recursive implementation in C, Java, and Python to find the total number of arrangements using the above recurrence:

C


Download  Run Code

Output:

The total number of distinct arrangements are 10

Java


Download  Run Code

Output:

The total number of distinct arrangements are 10

Python


Download  Run Code

Output:

The total number of distinct arrangements are 10

We can also devise a recurrence relation by assuming the next ball’s color instead of tracking the last ball’s color.

Let f(r, b, g, c) denote the total number of the arrangements where r red, b blue, and g green balls are left, and the color of the next ball will be c. Now, there are three possible cases:

  1. If the next ball is red, then the total number of ways is
     
    f(r, b, g, red) = f(r - 1, b, g, blue) + f(r - 1, b, g, green)
  2. If the next ball is blue, then the total number of ways is
     
    f(r, b, g, blue) = f(r, b - 1, g, red) + f(r, b - 1, g, green)
  3. If the next ball is green, then the total number of ways is
     
    f(r, b, g, green) = f(r, b, g - 1, red) + f(r, b, g - 1, blue)

Following is the implementation of the above recurrence in C, Java, and Python:

C


Download  Run Code

Output:

The total number of distinct arrangements are 10

Java


Download  Run Code

Output:

The total number of distinct arrangements are 10

Python


Download  Run Code

Output:

The total number of distinct arrangements are 10

The time complexity of the above solution is exponential, as many subproblems are recalculated repeatedly. The repeated subproblems can be easily seen by drawing a recursion tree.

The problems having an optimal substructure and overlapping subproblems can be solved using dynamic programming. The dynamic programming solution is left as an exercise to readers. The expected time complexity of the dynamic programming solution is O(r.b.g).