Shopping Spree

Preeti has decided to go to the Grand Mall to buy some stuff for her father’s birthday. On reaching the place, she found a fascinating shop that has an unlimited quantity of each item they sell. The shop has N different types of items. However, here Preeti has a fixed budget and can buy a maximum of K items. She can buy any amount of items ≥ 1 and ≤ K.

She can buy an arbitrary quantity of each item. There is no restriction on different items having different quantities. Now, Preeti wonders the number of different ways in which she can shop today. Two ways are considered different if the quantity of at least 1 of the items purchased is different. Preeti finds this task too hard to complete and requires your help. Your task is to find the required answer.

Note:
As the answer can be large, return your answer modulo 10^9  + 7.
Follow Up:
Can you solve this using constant extra space?
Input format:
The first line of input contains an integer T denoting the number of queries or test cases. 

The first and the only line of every test case contains 2 integers N and K representing the different number of items at the shop and maximum items Preeti can buy respectively. 
Output format:
For each test case, print the number of different ways to buy in a single line.
Note:
You do not need to print anything, it has already been taken care of. Just implement the given function.
Constraints:
1 <= T <= 10    
1 <= N <= 1000
1 <= K <= 1000 

Time Limit: 1 sec
CodingNinjas
author
2y

Firstly, I solved using array but all my test cases were not passed then I solved with dp approach by which all test cases passed successfully.

CodingNinjas
author
2y
Recursion
  • The idea is to use recursion to reduce the big problem into several small subproblems.
  • We will call a helper function that returns us the possible number of ways to shop.


Now, let us understand...read more

CodingNinjas
author
2y
Memoization

Let’s understand by an example.

Suppose we have three items in the shop and Preeti can buy at most four items.

The below figure shows some of the recursive calls for the given problem.

No...read more

CodingNinjas
author
2y
Dyncamic Programming
  • The idea is to create a 2-D table DP with row size N+1 and column size K+1.
  • Initially, all the elements of the DP table will be 0.
  • Now, the value DP[i][j] means the possible number o...read more
CodingNinjas
author
2y
Combinatorics
  • You can recall that out of N items, Preeti can buy 1 to K items. Thus, the total number of ways to buy at most K items from the given N items is:-
    WAYS(N, K) = WAYS(N, 1) + WAYS(N, 2) + ……...read more
Add answer anonymously...
Barclays Software Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions
Get AmbitionBox app

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter