## Interview Question

**Country:**United States

that's the good one ))

this recursion looks pretty much like combinations

where we have C(n,k) = C(n-1,k-1) + C(n-1,k)

so I suppose we can reduce it to C(N,K) finally..

Not entirely sure what you're referring to, but it is pretty much exactly the recurrence relation of the problem where you're asked to find the number of paths from one corner of a grid to another.

No.

Think of it this way: look at the k slots you have. For each slot, you can either put something there and stay there (n-1 items, still k slots), or not put something there and think about putting something at the next slot (k-1 slots, n items). Your recurrence relation would imply that once I've decided to put something into a slot, I can't contemplate putting a second item there.

What's C? Are you talking about the number of ways to choose how to place N things in N+K-1 bins? I don't see why that would be the answer...

@asm: Besides, you can verify that theory by comparing the values. If we agree that the recurrence F(n, k) = F(n-1, k) + F(n, k-1) is correct, we can see that the other one isn't right.

yeah that's what I mean: C(n,k) denotes the number of ways to choose a subset of 'k' elements out of 'n'

I can only show it on an example..

Suppose we have n = 5 and k = 3. Than we can encode any solution

of the above system as a particular subset of 5 elements out of 7

in the following way:

5 = 4 + 1 + 0 -> 1111010

5 = 2 + 2 + 1 -> 1101101

5 = 3 + 2 + 0 -> 1110110

5 = 1 + 1 + 3 -> 1010111

5 = 1 + 0 + 4 -> 1001111

5 = 0 + 5 + 0 -> 0111110

and so on, thus it should give C(n+k-1,n) eventually

ps. your recurrence is definitely correct, it's easy to check

No. of Integral solutions = (N+1)^(K-1)

assuming x[i]={0,1,2...N)

Please correct me if I'm wrong.

2 obs:

1. We can think of the problem as having to distribute N ( > k ) balls in k bins, such that, no bin is empty. So, the problem can be reduced to one in which every bin is given 1 ball and remaining ( N - k ) balls are distributed over min( N - k, k ) bins.

2. For the reduced problem, we can pick any of min( N - k, k ) bins, and find a solution to it such that a bin being empty ( remember here by empty I mean, it already has one ball from the original description ), is allowed.

Here's a straightforward DP recurrence:

- eugene.yarovoi December 16, 2011F(n, k) = F(n, k-1) + F (n-1, k-1) + F(n-2, k-1) + ... + F (0, k-1) = F(n-1, k) + F(n, k-1).

This equation allows a quadratic time, linear space (if you're clever) solution.