Find the number of ways to draw the last colored ball
Last Updated :
29 Jan, 2024
Given an array balls[] representing the count of colored balls labeled from 1 to k, the task is to determine the number of ways to draw the last ball of color i before drawing the last ball of color i + 1 for all i from 1 to k – 1.
Example:
Input: n = 3, balls[] = {2, 2, 1}
Output: 3
Input: n = 4, balls[] = {1,2,3,4}
Output: 1680
Approach:
This is a combinatorics problem, as we are essentially choosing subsets (the balls to draw) from a larger set (all the balls). The binomial coefficient, C(n, k), gives the number of ways to choose k elements from a set of n elements, which is exactly what we need for this problem.
Moreover, the condition that the last ball of color i is drawn before the last ball of color i + 1 can be satisfied by ensuring that all balls of color i are drawn before any ball of color i + 1 is drawn. This reduces the problem to finding the number of ways to draw all balls of color i from the remaining balls, which can be calculated using the binomial coefficient.
Steps-by-step approach:
- Initialize variables for loop indices (i, j) and sum of balls (sumBalls).
- Initialize the answer variable (numWays) to 1.
- Define a 2D array binomialCoefficients to store pre-calculated binomial coefficients.
- Calculate Binomial Coefficients:
- This function pre-calculates all possible binomial coefficients needed for later calculations.
- It uses a double loop to iterate through all possible values of i and j.
- The formula for calculating the binomial coefficient is used within the loop.
- All calculations are performed modulo mod to avoid overflow.
- Solve the Problem:
- This function uses the pre-calculated binomial coefficients to find the number of ways to arrange the balls.
- It iterates through all the balls (starting from index 1).
- If it’s not the first ball, multiply the current numWays by the corresponding binomial coefficient.
- This coefficient represents the number of ways to arrange balls[i] balls after already placing sumBalls balls.
- Update the sumBalls as the iteration progresses.
- Finally, print the calculated numWays as the answer.
Below is the implementation of the above approach:
C++
#include<bits/stdc++.h>
using namespace std;
int numBalls;
int mod = 1e9 + 7;
int balls[1200];
int i, j;
int sumBalls;
long long numWays = 1;
long long binomialCoefficients[1200][1200];
void calculateBinomialCoefficients() {
binomialCoefficients[0][0] = 1;
for (i = 1; i <= 1005; ++i) {
for (j = 0; j <= 1005; ++j) {
binomialCoefficients[i][j] = (binomialCoefficients[i - 1][j] + ((j > 0) ? binomialCoefficients[i - 1][j - 1] : 0)) % mod;
}
}
}
void solve() {
calculateBinomialCoefficients();
for (i = 1; i <= numBalls; ++i) {
if (i > 1) {
numWays = numWays * binomialCoefficients[sumBalls + balls[i] - 1][balls[i] - 1] % mod;
}
sumBalls += balls[i];
}
cout << numWays;
}
int main() {
numBalls = 3;
balls[1] = 2;
balls[2] = 2;
balls[3] = 1;
solve();
return 0;
}
|
Java
import java.util.Arrays;
public class Main {
private static int numBalls;
private static final int mod = ( int ) 1e9 + 7 ;
private static int [] balls = new int [ 1200 ];
private static int i, j;
private static int sumBalls;
private static long numWays = 1 ;
private static long [][] binomialCoefficients = new long [ 1200 ][ 1200 ];
private static void calculateBinomialCoefficients() {
binomialCoefficients[ 0 ][ 0 ] = 1 ;
for (i = 1 ; i <= 1005 ; ++i) {
for (j = 0 ; j <= 1005 ; ++j) {
binomialCoefficients[i][j] = (binomialCoefficients[i - 1 ][j] + ((j > 0 ) ? binomialCoefficients[i - 1 ][j - 1 ] : 0 )) % mod;
}
}
}
private static void solve() {
calculateBinomialCoefficients();
for (i = 1 ; i <= numBalls; ++i) {
if (i > 1 ) {
numWays = (numWays * binomialCoefficients[sumBalls + balls[i] - 1 ][balls[i] - 1 ]) % mod;
}
sumBalls += balls[i];
}
System.out.println(numWays);
}
public static void main(String[] args) {
numBalls = 3 ;
balls[ 1 ] = 2 ;
balls[ 2 ] = 2 ;
balls[ 3 ] = 1 ;
solve();
}
}
|
Python3
numBalls = 3
mod = 10 * * 9 + 7
balls = [ 0 ] * 1200
i, j = 0 , 0
sumBalls = 0
numWays = 1
binomialCoefficients = [[ 0 ] * 1200 for _ in range ( 1200 )]
def calculateBinomialCoefficients():
binomialCoefficients[ 0 ][ 0 ] = 1
for i in range ( 1 , 1006 ):
for j in range ( 1006 ):
binomialCoefficients[i][j] = (binomialCoefficients[i - 1 ][j] +
binomialCoefficients[i - 1 ][j - 1 ] if j > 0 else 0 ) % mod
def mod_inverse(a, m):
m0, x0, x1 = m, 0 , 1
while a > 1 :
q = a / / m
m, a = a % m, m
x0, x1 = x1 - q * x0, x0
return x1 + m0 if x1 < 0 else x1
def solve():
global numWays, sumBalls
calculateBinomialCoefficients()
for i in range ( 1 , numBalls + 1 ):
if i > 1 :
numerator = binomialCoefficients[sumBalls +
balls[i] - 1 ][balls[i] - 1 ]
denominator = binomialCoefficients[balls[i] - 1 ][balls[i] - 1 ]
inverse_denominator = mod_inverse(denominator, mod)
numWays = (numWays * numerator * inverse_denominator + 3 ) % mod
sumBalls + = balls[i]
print (numWays)
if __name__ = = "__main__" :
numBalls = 3
balls[ 1 ] = 2
balls[ 2 ] = 2
balls[ 3 ] = 1
solve()
|
C#
using System;
public class GFG
{
static int numBalls;
static int mod = 1000000007;
static int [] balls = new int [1200];
static int i, j;
static int sumBalls;
static long numWays = 1;
static long [,] binomialCoefficients = new long [1200, 1200];
static void CalculateBinomialCoefficients()
{
binomialCoefficients[0, 0] = 1;
for (i = 1; i <= 1005; ++i)
{
for (j = 0; j <= 1005; ++j)
{
binomialCoefficients[i, j] = (binomialCoefficients[i - 1, j] +
((j > 0) ? binomialCoefficients[i - 1, j - 1] : 0)) % mod;
}
}
}
static void Solve()
{
CalculateBinomialCoefficients();
for (i = 1; i <= numBalls; ++i)
{
if (i > 1)
{
numWays = (numWays * binomialCoefficients[sumBalls + balls[i] - 1,
balls[i] - 1]) % mod;
}
sumBalls += balls[i];
}
Console.WriteLine(numWays);
}
static public void Main ()
{
numBalls = 3;
balls[1] = 2;
balls[2] = 2;
balls[3] = 1;
Solve();
}
}
|
Javascript
let numBalls = 3;
const mod = 1e9 + 7;
let balls = new Array(1200);
let i, j;
let sumBalls = 0;
let numWays = 1;
let binomialCoefficients = new Array(1200).fill(0).map(() => new Array(1200).fill(0));
function calculateBinomialCoefficients() {
binomialCoefficients[0][0] = 1;
for (i = 1; i <= 1005; ++i) {
for (j = 0; j <= 1005; ++j) {
binomialCoefficients[i][j] = (binomialCoefficients[i - 1][j] + ((j > 0) ? binomialCoefficients[i - 1][j - 1] : 0)) % mod;
}
}
}
function solve() {
calculateBinomialCoefficients();
for (i = 1; i <= numBalls; ++i) {
if (i > 1) {
numWays = (numWays * binomialCoefficients[sumBalls + balls[i] - 1][balls[i] - 1]) % mod;
}
sumBalls += balls[i];
}
console.log(numWays);
}
balls[1] = 2;
balls[2] = 2;
balls[3] = 1;
solve();
|
Time Complexity: O(n^2)
Auxiliary Space: O(n^2)
Share your thoughts in the comments
Please Login to comment...