# Find ways to arrange K green balls among N balls such that exactly i moves is needed to collect all K green balls

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.

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

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

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:

## C++

 `// C++ implementation of the approach ` `#include ` `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 = 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; ` `} `

## 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 `

Output:

```3 6 1
```

My Personal Notes arrow_drop_up pawanasipugmailcom

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.

Improved By : AnkitRai01