VOOZH about

URL: https://www.geeksforgeeks.org/dsa/ways-to-arrange-balls-such-that-adjacent-balls-are-of-different-types/

⇱ Ways to arrange Balls such that adjacent balls are of different types - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Ways to arrange Balls such that adjacent balls are of different types

Last Updated : 23 Jul, 2025

There are 'p' balls of type P, 'q' balls of type Q and 'r' balls of type R. Using the balls we want to create a straight line such that no two balls of the same type are adjacent.
Examples : 

Input: p = 1, q = 1, r = 0
Output: 2
Explanation: There are only two arrangements PQ and QP

Input: p = 1, q = 1, r = 1
Output: 6
Explanation: There are only six arrangements PQR, QPR, QRP, RQP, PRQ and RPQ

Input: p = 2, q = 1, r = 1
Output: 12
Explanation: There are twelve arrangements PQRP, PRQP, QPRP, RPQP, PQPR, PRPQ, QPQR, RQRP, PQRQ, QRQP, RPQR, and QRPR

Using Recursion – O(3^n) Time and O(n) Space

To solve this problem, we use a recursive approach, placing the last ball as one of three types. Each ball type is represented by a digit in the recurrence relation: 0 for P, 1 for Q, and 2 for R. If the last ball is of type P (0), the next ball must be of type Q or R, and similarly for the last balls of type Q (1) and R (2).

Recurrence Relation:

  • if the last ball is of type P: countWays(p, q, r, last) = countWays(p-1, q, r, 1) + countWays(p-1, q, r, 2)
  • if the last ball is of type Q: countWays(p, q, r, last) = countWays(p, q-1, r, 0) + countWays(p, q-1, r, 2)
  • if the last ball is of type R: countWays(p, q, r, last) = countWays(p, q, r-1, 0) + countWays(p, q-1, r-1, 1)

Base Cases:

if (p < 0), (q < 0) or (r < 0) then countWays(p, q, r, last) = 0. if only one ball ramains of specific type, then:

  • countWays(p, q, r, 0) = 1 if p=1 and q=0 and r=0
  • countWays(p, q, r, 1) = 1 if p=0 and q=1 and r=0
  • countWays(p, q, r, 2) = 1 if p=0 and q=0 and r=1

Output
6

Using Top-Down DP (Memoization) - O(p*q*r) Time and O(p*q*r) Space

If we notice carefully, we can observe that the above recursive solution holds the following two properties of Dynamic Programming.

1. Optimal Substructure:

The solution to the problem of arranging balls with no two adjacent balls of the same type can be derived from the optimal solutions of smaller subproblems.

2. Overlapping Subproblems:

In the recursive solution, we notice that many subproblems are solved multiple times. For example, when calculating countWays(p, q, r, last), the same values of p, q, r are computed multiple times.

  • The recursive solution involves changing four parameters: the number of remaining balls of each type (p, q, r) and the last placed ball type (last). We need to track all these parameters, so we create a 4D array of size (p+1) x (q+1) x (r+1) x 3. This is because the values of p, q, and r will be in the range [0, p], [0, q], and [0, r], respectively, and the value of last will range from 0 to 2 (representing the three ball types: P, Q, and R).
  • We initialize this 4D array with -1 to indicate that no subproblems have been computed yet.
  • Before computing the number of arrangements for any subproblem, we check if the value at memo[p][q][r][last] is -1. If it is, we proceed to compute the result using the recursive relations. If it is, we proceed to compute the result. otherwise, we return the stored result.

Output
6

Using Bottom-Up DP (Tabulation) – O(p*q*r) Time and O(p*q*r) Space

The approach is similar to the previous one. just instead of breaking down the problem recursively, we iteratively build up the solution by calculating in bottom-up manner.

So we will create 4D array of size (p+1)*(q+1)*(r+1)*last. and the state dp[i][j][k][l] will represent the number of ways to arrange the balls such that:

  • i is the count of balls of type P placed so far.
  • j is the count of balls of type Q placed so far.
  • k is the count of balls of type R placed so far.
  • l is the type of the last ball placed (l = 0 for P, l = 1 for Q, and l = 2 for R).

1. For the case where the last ball placed is of type P (i.e., l=0): if i>0, then dp[i][j][k][0] = dp[i-1][j][k][1] + dp[i-1][j][k][2]

2. For the case where the last ball placed is of type Q (i.e. ,l=1): if j>0, then dp[i][j][k][1]=dp[i][j-1][k][0]+dp[i][j-1][k][2]

3. For the case where the last ball placed is of type R (i.e., l=2): if k>0, then dp[i][j][k][2]=dp[i][j][k-1][0]+dp[i][j][k-1][1]

Base Cases

  • dp[1][0][0][0]=1 (Only one P ball left, and the last placed ball is P)
  • dp[0][1][0][1]=1 (Only one Q ball left, and the last placed ball is Q)
  • dp[0][0][1][2]=1 (Only one R ball left, and the last placed ball is R)

Output
6
Comment
Article Tags: