Given two integers N and K. There are N balls placed in a row. K of them are green and N – K of them are black. The task is to find the number of ways to arrange N balls such that one will need exactly i ( 1 ≤ i ≤ K ) moves to collect all the green balls. In one move, we can collect any group of consecutive green balls. Note that the answer can be very large. So, output answer modulo 109 + 7.
Input: N = 5, K = 3
Output: 3 6 1
There are three ways to arrange the balls so that
one will need exactly one move:
(G, G, G, B, B), (B, G, G, G, B), and (B, B, G, G, G).
There are six ways to arrange the balls so that
one will need exactly two moves:
(G, G, B, G, B), (G, G, B, B, G), (B, G, G, B, G), (B, G, B, G, G),
(G, B, G, G, B), and (G, B, B, G, G).
There is only one way to arrange the balls so that
one will need exactly three moves: (G, B, G, B, G).
Input: N = 100, K = 5
Output: 96 18240 857280 13287840 61124064
Approach: Only i moves have to be performed to collect K green balls, which means that K green balls are separated into i places by balck balls. Therefore, let’s consider the combination as follows.
- First, arrange the N – K balck balls in a row.
- In between these black balls, select i places from the left end to the right end and consider placing K green balls there. There are N – K + 1C i ways to choose these.
- For each choice, consider how many green balls will be assigned to each gap. Since it is necessary to assign one or more to each, there are K – 1C i – 1 ways to determine this.
Therefore, for each i, the answer is N – K + 1C i * K – 1C i – 1 . Finding n C r is disscused here.
Below is the implementation of the above approach:
3 6 1
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
- Number of ways to arrange a word such that all vowels occur together
- Number of ways to arrange a word such that no vowels occur together
- Ways to choose balls such that at least one ball is chosen
- Count ways to arrange N distinct objects if all clockwise arrangements are considered the same
- Total position where king can reach on a chessboard in exactly M moves
- Total position where king can reach on a chessboard in exactly M moves | Set 2
- Arrange first N natural numbers such that absolute difference between all adjacent elements > 1
- Calculate Stirling numbers which represents the number of ways to arrange r objects around n different circles
- Number of ways to arrange K different objects taking N objects at a time
- Number of ways to arrange 2*N persons on the two sides of a table with X and Y persons on opposite sides
- Count number of ways to arrange first N numbers
- Number of ways to arrange N numbers which are in a range from 1 to K under given constraints.
- Number of ways of scoring R runs in B balls with at most W wickets
- Ways to divide a binary array into sub-arrays such that each sub-array contains exactly one 1
- Count ways to distribute m items among n people
- Number of ways to distribute N Paper Set among M students
- Count of ways to distribute N items among 3 people with one person receiving maximum
- Find the distance covered to collect items at equal distances
- Find minimum moves to bring all elements in one cell of a matrix
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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : AnkitRai01