Given three integers N, M and K. The task is to find the number of ways to fill N positions using M colors such that there are exactly K pairs of adjacent different colors.
Input: N = 3, M = 2, K = 1
Let the colors be 1 and 2, so the ways are:
1, 1, 2
1, 2, 2
2, 2, 1
2, 1, 1
Above 4 ways have exactly one pair of adjacent elements with different color.
Input: N = 3, M = 3, K = 2
Approach: We can use Dynamic Programming with memoisation to solve the above problem. There are N positions to fill, hence the recursive function will compose of two calls, one if the next position is filled with the same color and the other if it is filled with a different color. Hence the recursive calls will be:
- countWays(index + 1, cnt), if the next index is filled with same color.
- (m – 1) * countWays(index + 1, cnt + 1), if the next index is filled with a different color. The number of ways get multiplied with (m – 1).
The bases cases will be:
- If index = n, then a check for the value of cnt is done. If cnt = K then it is a possible way hence return 1, else return 0.
- To avoid repetitive calls, memoize the returned value in a 2-D array and return this value if the recursive call with same parameters is done again.
Below is the implementation of the above approach:
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Ways to arrange Balls such that adjacent balls are of different types
- Flood fill Algorithm - how to implement fill() in paint?
- Color N boxes using M colors such that K boxes have different color from the box on its left
- Count number of ways to fill a "n x 4" grid using "1 x 4" tiles
- Maximum sum in an array such that every element has exactly one adjacent element to it
- Maximum sum such that exactly half of the elements are selected and no two adjacent
- Minimize the count of adjacent pairs with different parity
- Balanced expressions such that given positions have opening brackets
- Balanced expressions such that given positions have opening brackets | Set 2
- Number of binary strings such that there is no substring of length ≥ 3
- Minimum labelled node to be removed from undirected Graph such that there is no cycle
- Count ways to reach Nth Stairs by taking 1 and 2 steps with exactly one 3 step
- Number of ways to make exactly C components in a 2*N matrix
- Number of ways to select exactly K even numbers from given Array
- Different ways to sum n using numbers greater than or equal to m
- Minimum colors required such that edges forming cycle do not have same color
- Number of pairs such that path between pairs has the two vertices A and B
- Longest subsequence with different adjacent characters
- Count of all subsequences having adjacent elements with different parity
- Find the number of distinct pairs of vertices which have a distance of exactly k in a tree
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.