Open In App

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

Last Updated : 14 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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 
 

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 black balls. Therefore, let’s consider the combination as follows. 

  • First, arrange the N – K black 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 discussed here.

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;
}


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


Java




// Java implementation of the approach
 
import java.util.*;
 
class GFG{
static final int N = 100005;
static final int mod = (int)(1e9 + 7);
 
// To store the factorial and the
// factorial mod inverse of a number
static long []factorial = new long[N];
static long []modinverse = new long[N];
 
 
// Function to find (a ^ m1) % mod
static long power(long a, int m1)
{
    if (m1 == 0)
        return 1;
    else if (m1 == 1)
        return a;
    else if (m1 == 2)
        return (1L * a * a) % mod;
    else if (m1 %2== 1)
        return (1L * 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
static void factorialfun()
{
    factorial[0] = 1;
    for (int i = 1; i < N; i++)
        factorial[i] = (1L
                        * factorial[i - 1] * i)
                       % mod;
}
 
// Function to find the factorial
// mod inverse of all the numbers
static void modinversefun()
{
    modinverse[N - 1]
        = (int) (power(factorial[N - 1], mod - 2) % mod);
 
    for (int i = N - 2; i >= 0; i--)
        modinverse[i] = (1 * modinverse[i + 1]
                         * (i + 1))
                        % mod;
}
 
// Function to return nCr
static long binomial(int n, int r)
{
    if (r > n)
        return 0;
 
    long a = (1L * factorial[n]
             * modinverse[n - r])
            % mod;
 
    a = (1 * 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
static void arrange_balls(int n, int k)
{
    factorialfun();
    modinversefun();
 
    for (int i = 1; i <= k; i++)
        System.out.print((1L * binomial(n - k + 1, i)
                 * binomial(k - 1, i - 1))
                    % mod
            + " ");
}
 
// Driver code
public static void main(String[] args)
{
    int n = 5, k = 3;
 
    // Function call
    arrange_balls(n, k);
 
}
}
 
// This code contributed by Princi Singh


C#




// C# implementation of the approach
using System;
 
class GFG{
     
static readonly int N = 100005;
static readonly int mod = (int)(1e9 + 7);
 
// To store the factorial and the
// factorial mod inverse of a number
static long []factorial = new long[N];
static long []modinverse = new long[N];
 
// Function to find (a ^ m1) % mod
static long power(long a, int m1)
{
    if (m1 == 0)
        return 1;
    else if (m1 == 1)
        return a;
    else if (m1 == 2)
        return (1L * a * a) % mod;
    else if (m1 % 2 == 1)
        return (1L * 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
static void factorialfun()
{
    factorial[0] = 1;
    for(int i = 1; i < N; i++)
        factorial[i] = (1L * factorial[i - 1] * i) % mod;
}
 
// Function to find the factorial
// mod inverse of all the numbers
static void modinversefun()
{
    modinverse[N - 1] = (int)(power(factorial[N - 1],
                                            mod - 2) % mod);
    for(int i = N - 2; i >= 0; i--)
        modinverse[i] = (1 * modinverse[i + 1] *
                        (i + 1)) % mod;
}
 
// Function to return nCr
static long binomial(int n, int r)
{
    if (r > n)
        return 0;
 
    long a = (1L * factorial[n] *
    modinverse[n - r]) % mod;
 
    a = (1 * 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
static void arrange_balls(int n, int k)
{
    factorialfun();
    modinversefun();
 
    for(int i = 1; i <= k; i++)
        Console.Write((1L * binomial(n - k + 1, i) *
                            binomial(k - 1, i - 1)) %
                                   mod + " ");
}
 
// Driver code
public static void Main(String[] args)
{
    int n = 5, k = 3;
 
    // Function call
    arrange_balls(n, k);
}
}
 
// This code is contributed by 29AjayKumar


Javascript




// Function to find (a ^ m1) % mod
function power(a, m1) {
    if (m1 == 0)
        return BigInt(1);
    else if (m1 == 1)
        return BigInt(a);
    else if (m1 == 2)
        return BigInt(a * a) % BigInt(mod);
    else if (m1 % BigInt(2) === BigInt(1))
        return BigInt(a * power(power(a, m1 / BigInt(2)), BigInt(2))) % BigInt(mod);
    else
        return power(power(a, m1 / BigInt(2)), BigInt(2)) % BigInt(mod);
}
 
 
function factorialfun() {
    factorial[0] = 1n;
    for (let i = 1; i < N; i++)
        factorial[i] = factorial[i - 1] * BigInt(i) % BigInt(mod);
}
 
function modinversefun() {
    modinverse[N - 1] = power(factorial[N - 1], BigInt(mod - 2)) % BigInt(mod);
 
    for (let i = N - 2; i >= 0; i--)
        modinverse[i] = modinverse[i + 1] * BigInt(i + 1) % BigInt(mod);
}
 
function binomial(n, r) {
    if (r > n)
        return 0;
 
    let a = factorial[n] * modinverse[n - r] % BigInt(mod);
 
    a = a * modinverse[r] % BigInt(mod);
    return a;
}
 
function arrange_balls(n, k) {
    factorialfun();
    modinversefun();
 
    for (let i = 1; i <= k; i++)
        console.log(binomial(n - k + 1, i) * binomial(k - 1, i - 1) % BigInt(mod) + " ");
}
 
const N = 100005;
const mod = 1e9 + 7;
let factorial = new Array(N);
let modinverse = new Array(N);
 
const n = 5, k = 3;
 
arrange_balls(n, k);


Output: 

3 6 1

 



Similar Reads

Ways to arrange Balls such that adjacent balls are of different types
There are 'p' balls of type P, 'q' balls of type Q and 'r' balls of type R. Using the balls we want to create a straight line such that no two balls of same type are adjacent.Examples : Input : p = 1, q = 1, r = 0 Output : 2 There are only two arrangements PQ and QP Input : p = 1, q = 1, r = 1 Output : 6 There are only six arrangements PQR, QPR, QR
34 min read
Ways to arrange N balls of K colors with no adjacent same colors
Given infinite balls of K distinct colors. Count the number of ways to arrange N balls in a line such that no two adjacent balls are of same color. Since the answer may be very large, output the result MOD 1000000007. Examples: Input: N = 2, K = 2Output: 2Explanation: We will denote the colors by 'R' and 'G'. There are two possible ways: "RG" and "
5 min read
Count ways of selecting X red balls and Y blue balls
Given integers A, B, C, and D, There are two boxes First Box has A red balls and B blue balls and the second box has C red balls and D blue balls, the task is to count ways to select 3 red balls and 3 blue balls so that there are 3 balls drawn from the first box and 3 balls drawn from the second box. (Print the answer modulo 109 + 7). Examples: Inp
17 min read
Number of ways to arrange a word such that all vowels occur together
Given a word containing vowels and consonants. The task is to find that in how many ways the word can be arranged so that the vowels always come together. Given that the length of the word &lt;10. Examples: Input: str = "geek"Output: 6Ways such that both 'e' comes together are 6 i.e. geek, gkee, kgee, eekg, eegk, keeg Input: str = "corporation"Outp
9 min read
Find winner in game of N balls, in which a player can remove any balls in range [A, B] in a single move
Given two integers A and B, and also given that Alice and Bob are playing a game starting with a bag containing N balls, in which, in a single move, a player can remove any number of balls between the range [A, B] and if the player cannot remove any balls, then the player loses, the task is to find the winner of the game if Alice and Bob play the g
5 min read
Number of ways to arrange a word such that no vowels occur together
Given an English word of length at most 20 characters. Calculate the number of ways to arrange the word such that no vowels occur together. Note: If the total number of vowels in the given word is one then the result should be 0. Examples: Input : AllahabadOutput : 7200 Input : geeksforgeeksOutput : 32205600 Input : abcdOutput : 0 Since the word co
13 min read
Count ways to arrange integers in circle such that no two adjacent are same
Given 2 integers N and M, find the number of ways to arrange N integers in a circle such that every integer is in the range 0 to M-1, and no two adjacent integers are same. As the answer could be large, find the number, modulo 998244353. Examples: Input: N=3, M=3Output: 6Explanation: There are six ways as follows (0, 1, 2), (0, 2, 1), (1, 0, 2), (1
5 min read
Placement of magical box on road to collect the maximum number of magical balls
There is a road which denotes the x-axis. Geek is at the start of the road (at origin). He has N magical balls. The balls are magical because they have a number written on them and they go that number of positions on every bounce. if the ball has 4 written on it, it goes to the positions like 4, 8, 12, 16, . . . . As Geek has N number of magical ba
8 min read
Find the minimum number of moves needed to move from one cell of matrix to another
Given a N X N matrix (M) filled with 1 , 0 , 2 , 3 . Find the minimum numbers of moves needed to move from source to destination (sink) . while traversing through blank cells only. You can traverse up, down, right and left. A value of cell 1 means Source. A value of cell 2 means Destination. A value of cell 3 means Blank cell. A value of cell 0 mea
27 min read
Collect maximum points in an array with k moves
Given an array of integer and two values k and i where k is the number of moves and i is the index in the array. The task is to collect maximum points in the array by moving either in single or both directions from given index i and making k moves. Note that every array element visited is considered a move including arr[i]. Constraints : n is the s
9 min read
Article Tags :
Practice Tags :