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 **10 ^{9} + 7**.

**Examples:**

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 areways to choose these.^{N – K + 1}C_{i} - 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
ways to determine this.^{K – 1}C_{i – 1}

Therefore, for each **i**, the answer is ** ^{N – K + 1}C _{i} * ^{K – 1}C _{i – 1}** . Finding

**is disscused here.**

^{n }C_{r}Below is the implementation of the above approach:

## C++

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` `#define N 100005 ` `#define mod (int)(1e9 + 7) ` ` ` `// To store the factorial and the ` `// factorial mod inverse of a number ` `int` `factorial[N], modinverse[N]; ` ` ` `// Function to find (a ^ m1) % mod ` `int` `power(` `int` `a, ` `int` `m1) ` `{ ` ` ` `if` `(m1 == 0) ` ` ` `return` `1; ` ` ` `else` `if` `(m1 == 1) ` ` ` `return` `a; ` ` ` `else` `if` `(m1 == 2) ` ` ` `return` `(1LL * a * a) % mod; ` ` ` `else` `if` `(m1 & 1) ` ` ` `return` `(1LL * a ` ` ` `* power(power(a, m1 / 2), 2)) ` ` ` `% mod; ` ` ` `else` ` ` `return` `power(power(a, m1 / 2), 2) % mod; ` `} ` ` ` `// Function to find factorial ` `// of all the numbers ` `void` `factorialfun() ` `{ ` ` ` `factorial[0] = 1; ` ` ` `for` `(` `int` `i = 1; i < N; i++) ` ` ` `factorial[i] = (1LL ` ` ` `* factorial[i - 1] * i) ` ` ` `% mod; ` `} ` ` ` `// Function to find the factorial ` `// mod inverse of all the numbers ` `void` `modinversefun() ` `{ ` ` ` `modinverse[N - 1] ` ` ` `= power(factorial[N - 1], mod - 2) % mod; ` ` ` ` ` `for` `(` `int` `i = N - 2; i >= 0; i--) ` ` ` `modinverse[i] = (1LL * modinverse[i + 1] ` ` ` `* (i + 1)) ` ` ` `% mod; ` `} ` ` ` `// Function to return nCr ` `int` `binomial(` `int` `n, ` `int` `r) ` `{ ` ` ` `if` `(r > n) ` ` ` `return` `0; ` ` ` ` ` `int` `a = (1LL * factorial[n] ` ` ` `* modinverse[n - r]) ` ` ` `% mod; ` ` ` ` ` `a = (1LL * a * modinverse[r]) % mod; ` ` ` `return` `a; ` `} ` ` ` `// Function to find ways to arrange K green ` `// balls among N balls such that we need ` `// exactly i moves to collect all K green balls ` `void` `arrange_balls(` `int` `n, ` `int` `k) ` `{ ` ` ` `factorialfun(); ` ` ` `modinversefun(); ` ` ` ` ` `for` `(` `int` `i = 1; i <= k; i++) ` ` ` `cout << (1LL * binomial(n - k + 1, i) ` ` ` `* binomial(k - 1, i - 1)) ` ` ` `% mod ` ` ` `<< ` `" "` `; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `n = 5, k = 3; ` ` ` ` ` `// Function call ` ` ` `arrange_balls(n, k); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of the approach ` `N ` `=` `100005` `mod ` `=` `(` `int` `)(` `1e9` `+` `7` `) ` ` ` `# To store the factorial and the ` `# factorial mod inverse of a number ` `factorial ` `=` `[` `0` `] ` `*` `N; ` `modinverse ` `=` `[` `0` `] ` `*` `N; ` ` ` `# Function to find (a ^ m1) % mod ` `def` `power(a, m1) : ` ` ` ` ` `if` `(m1 ` `=` `=` `0` `) : ` ` ` `return` `1` `; ` ` ` `elif` `(m1 ` `=` `=` `1` `) : ` ` ` `return` `a; ` ` ` `elif` `(m1 ` `=` `=` `2` `) : ` ` ` `return` `(a ` `*` `a) ` `%` `mod; ` ` ` `elif` `(m1 & ` `1` `) : ` ` ` `return` `(a ` `*` `power(power(a, m1` `/` `/` `2` `), ` `2` `)) ` `%` `mod; ` ` ` `else` `: ` ` ` `return` `power(power(a, m1 ` `/` `/` `2` `), ` `2` `) ` `%` `mod; ` ` ` `# Function to find factorial ` `# of all the numbers ` `def` `factorialfun() : ` ` ` ` ` `factorial[` `0` `] ` `=` `1` `; ` ` ` `for` `i ` `in` `range` `(` `1` `, N) : ` ` ` `factorial[i] ` `=` `(factorial[i ` `-` `1` `] ` `*` `i) ` `%` `mod; ` ` ` `# Function to find the factorial ` `# mod inverse of all the numbers ` `def` `modinversefun() : ` ` ` `modinverse[N ` `-` `1` `] ` `=` `power(factorial[N ` `-` `1` `], ` ` ` `mod ` `-` `2` `) ` `%` `mod; ` ` ` ` ` `for` `i ` `in` `range` `(N ` `-` `2` `, ` `-` `1` `, ` `-` `1` `) : ` ` ` `modinverse[i] ` `=` `(modinverse[i ` `+` `1` `] ` `*` ` ` `(i ` `+` `1` `)) ` `%` `mod; ` ` ` `# Function to return nCr ` `def` `binomial(n, r) : ` ` ` ` ` `if` `(r > n) : ` ` ` `return` `0` `; ` ` ` ` ` `a ` `=` `(factorial[n] ` `*` ` ` `modinverse[n ` `-` `r]) ` `%` `mod; ` ` ` ` ` `a ` `=` `(a ` `*` `modinverse[r]) ` `%` `mod; ` ` ` `return` `a; ` ` ` `# Function to find ways to arrange K green ` `# balls among N balls such that we need ` `# exactly i moves to collect all K green balls ` `def` `arrange_balls(n, k) : ` ` ` `factorialfun(); ` ` ` `modinversefun(); ` ` ` ` ` `for` `i ` `in` `range` `(` `1` `, k ` `+` `1` `) : ` ` ` `print` `((binomial(n ` `-` `k ` `+` `1` `, i) ` `*` ` ` `binomial(k ` `-` `1` `, i ` `-` `1` `)) ` `%` `mod, ` ` ` `end ` `=` `" "` `); ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `n ` `=` `5` `; k ` `=` `3` `; ` ` ` ` ` `# Function call ` ` ` `arrange_balls(n, k); ` ` ` `# This code is contributed by AnkitRai01 ` |

*chevron_right*

*filter_none*

**Output:**

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.

## Recommended Posts:

- 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
- Arrange first N natural numbers such that absolute difference between all adjacent elements > 1
- 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
- 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
- Number of ways to arrange N numbers which are in a range from 1 to K under given constraints.
- Count number of ways to arrange first N numbers
- 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 minimum moves to bring all elements in one cell of a matrix
- Print N lines of 4 numbers such that every pair among 4 numbers has a GCD K

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.