Hackerrank - K Candy Store Solution
1 min read

Hackerrank - K Candy Store Solution

Hackerrank - K Candy Store Solution

Jim enters a candy shop which has N different types of candies, each candy is of the same price. Jim has enough money to buy K candies. In how many different ways can he purchase K candies if there are infinite candies of each kind?

Input Format
The first line contains an integer T, the number of tests.
This is followed by 2T lines which contain T tests:
The first line (of each testcase) is an integer N and the second line (of each testcase) is an integer K.

Output Format
For each testcase, print the number of ways Jim can buy candies from the shop in a newline. If the answer has more than 9 digits, print the last 9 digits.

Note
This problem may expect you to have solved nCr Table

Constraints
1 <= T <= 200
1 <= N < 1000
1 <= K < 1000

Sample Input

2
4
1
2
3

Sample Output

4
4

Explanation
There are 2 testcases, for the first testcase we have N = 4 and K = 1, as Jim can buy only 1 candy, he can choose to buy any of the 4 types of candies available. Hence, his answer is 4. For the 2nd testcase, we have N = 2 and K = 3, If we name two chocolates as a and b, he can buy

aaa bbb aab abb 

chocolates, hence 4.

Solution in Python

def fact(x,y=0):
    p = 1
    for i in range(y+1,x+1):
        p*=i
    return p

for _ in range(int(input())):
    n = int(input())
    k = int(input())
    print((fact(k+n-1,n-1)//fact(k))%10**9)

Enjoying these posts? Subscribe for more


Adblocker detected! Please consider reading this notice.

We've detected that you are using AdBlock Plus or some other adblocking software which is preventing the page from fully loading.

That's okay. But without advertising-income, we can't keep making this site awesome.

We don't have any banner, Flash, animation, obnoxious sound, or popup ad. We do not implement these annoying types of ads!

We need money to operate the site, and almost all of it comes from our online advertising.

Please add thepoorcoder.com to your ad blocking whitelist or disable your adblocking software.

×