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
- Ways to arrange Balls such that adjacent balls are of different types
- Ways to choose balls such that at least one ball is chosen
- Number of ways of scoring R runs in B balls with at most W wickets
- Program to check if water tank overflows when n solid balls are dipped in the water tank
- Count number of ways to arrange first N numbers
- 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
- 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
- Find the distance covered to collect items at equal distances
- Number of ways to arrange 2*N persons on the two sides of a table with X and Y persons on opposite sides
- Find minimum number of Log value needed to calculate Log upto N
- Find minimum operations needed to make an Array beautiful
- Find the index of the left pointer after possible moves in the array
- Find minimum moves to reach target on an infinite line
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.
Improved By : AnkitRai01