MARBLES problem wrong answer and runtime error

HI!!
I think the answer is just (n-1)C(n-k). But i get wrong answer and even runtime, Do i need to modulo my answer at any stage?

You are wrong with your mathematical formula here. Think again, and share your thinking.
Then I’ll tell you the right way :slight_smile:

I think ki the answer is same as number of ways of solving this problem:
x1+x2+x3+x4…+xk = n-k;
I did -k with n because atleast one of each need to be there. Why is this wrong?

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 :slight_smile:

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.