I cannot find a way to solve this.
help required.
Problem in solving Marbles
We just need to use normal mathematical formula.
k=min(n-k,k-1);
for(int i=1;i<=k;i++)
{
ans=ans*(n-i)/(i);
}
Please have a look at this very short code. Its easy to understand.
the for loop part is nCr calculation.
but why do we decrement k and also n?
We don’t decrease n. We minimize k.
Its better to calculate C(10,2) rather than calculating C(10,8).
Okay i got you…
See, if we choose n balls, then k balls have to be of k different colours for sure.
We now have to select n-k balls with k types of options.
There is a general formula for this case (Using PnC) :
(n+r−1)! / (n−1)!⋅r!
Put the new value (n-k) instead of n in the mathematical formula to get the New formula for our question that equals to C(n-1,k-1).
I hope i am clear and you shall be able to implement the code
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.