


As the answer can be large, return your answer modulo 10^9 + 7.
Can you solve this using constant extra space?
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.
For each test case, print the number of different ways to buy in a single line.
You do not need to print anything, it has already been taken care of. Just implement the given function.
1 <= T <= 10
1 <= N <= 1000
1 <= K <= 1000
Time Limit: 1 sec
Now, let us understand how to break the big problem into small subproblems with an example let's say, for N = 2 and K = 3 and let the items be {R, G} we can see that this can be found out if:
The algorithm for the helper function will look as follows:
N = Number of available items.
K = The highest number of items Preeti can buy.
int helper(N, K):
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.
Note: Subproblem {a,b} means a problem with ‘a’ items and ‘b’ is the number of items Preeti is willing to buy.
As we can see, some subproblems are called multiple times.
For example, the subproblem {2,3} is called 2 times, {1, 3} is called 3 times, {1, 1} is called numerous times.
That’s why we will store the solved subproblems in a lookup table so that we don’t have to solve them again. Whenever there is a call to the solved subproblem, we will directly return the answer of that subproblem stored in the lookup table.
For the test case where N = 3 and K = 4.
The table will be:
To make it more clear, let the first item be R, second be G and third be B, The list will be:-
This can be calculated efficiently in O(K) using python.
int shoppingSpree(int N, int K):
Pair Product Div by K
Pair Product Div by K
Merge Two Sorted Arrays Without Extra Space
Merge Two Sorted Arrays Without Extra Space
Co-Prime
First Digit One
Special Digit Numbers