We are given N items which are of total K different colors. Items of the same color are indistinguishable and colors can be numbered from 1 to K and count of items of each color is also given as k1, k2 and so on. Now we need to arrange these items one by one under a constraint that the last item of color i comes before the last item of color (i + 1) for all possible colors. Our goal is to find out how many ways this can be achieved.
Input : N = 3 k1 = 1 k2 = 2 Output : 2 Explanation : Possible ways to arrange are, k1, k2, k2 k2, k1, k2 Input : N = 4 k1 = 2 k2 = 2 Output : 3 Explanation : Possible ways to arrange are, k1, k2, k1, k2 k1, k1, k2, k2 k2, k1, k1, k2
We can solve this problem using dynamic programming. Let dp[i] stores the number of ways to arrange first i colored items. For one colored item answer will be one because there is only one way. Now Let’s assume all items are in a sequence. Now, to go from dp[i] to dp[i + 1], we need to put at least one item of color (i + 1) at the very end, but the other items of color (i + 1) can go anywhere in the sequence. The number of ways to arrange the item of color (i + 1) is combination of (k1 + k2 .. + ki + k(i + 1) – 1) over (k(i + 1) – 1) which can be represented as (k1 + k2 .. + ki + k(i + 1) – 1)C(k(i + 1) – 1). In this expression we subtracted one because we need to put one item at the very end.
In below code, first we have calculated the combination values, you can read more about that from here. After that we looped over all different color and calculated the final value using above relation.
This article is contributed by Utkarsh Trivedi. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
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.
- Number of ways to arrange N numbers which are in a range from 1 to K under given constraints.
- Count ways to build street under given constraints
- Count of strings that can be formed using a, b and c under given constraints
- Calculate Stirling numbers which represents the number of ways to arrange r objects around n different circles
- Count number of ways to arrange first N numbers
- Ways to form a group from three groups with given constraints
- Ways to arrange Balls such that adjacent balls are of different types
- Count digit groupings of a number with given constraints
- Find the longest path in a matrix with given constraints
- Maximum path sum for each position with jumps under divisibility condition
- Maximum items that can be filled in K Knapsacks of given Capacity
- Number of ways to get a given sum with n number of m-faced dices
- Unbounded Knapsack (Repetition of items allowed)
- Printing Items in 0/1 Knapsack
- Count of Ways to obtain given Sum from the given Array elements
- Count number of ways to reach a given score in a game
- Count number of ways to reach a given score in a Matrix
- Count of ways to split a given number into prime segments
- Number of ways to split N as sum of K numbers from the given range
- Number of ways to color N-K blocks using given operation