


You have an infinite number of elements of each type.
If N=3 and X=7 and array elements are [1,2,3].
Way 1 - You can take 4 elements [2, 2, 2, 1] as 2 + 2 + 2 + 1 = 7.
Way 2 - You can take 3 elements [3, 3, 1] as 3 + 3 + 1 = 7.
Here, you can see in Way 2 we have used 3 coins to reach the target sum of 7.
Hence the output is 3.
The first line of the input contains an integer, 'T’, denoting the number of test cases.
The first line of each test case will contain two space-separated integers ‘N’ and ‘X’, denoting the size of the array and the target sum.
The second line of each test case contains ‘N’ space-separated integers denoting elements of the array.
For each test case, print the minimum number of coins needed to reach the target sum ‘X’, if it's not possible to reach to target then print "-1".
Print a separate line for each test case.
You do not need to print anything, it has already been taken care of. Just implement the given function.
1 <= T <= 10
1 <= N <= 15
1 <= nums[i] <= (2^31) - 1
1 <= X <= 10000
All the elements of the “nums” array will be unique.
Time limit: 1 sec
The basic idea is to go over recursively to find the way such that the sum of chosen elements is ‘X’. For every element, we have two choices
1. Include the element in our set of chosen elements.
2. Don’t include the element in our set of chosen elements.
When we include an element in the set then we decrease the ‘X’ by the value of the element at the end. At any point, if we have X=0 then we can say that we have found the set of integers such that the sum of the set is equal to ‘X’.
Let us define a function “minimum( sum, num)”, where “sum” is the remaining sum that we have to make, “num” is the array that is given to us. Each time we have ‘n’ elements from which we have to take one.
If the function “minimum( sum, num)” returns INT_MAX which means that we cannot make the target sum ‘X’.
Return ans
The basic idea is to store the results of the recursive calls to avoid repetition. We can memorize the results of our function calls in an array (say, ‘DP’).
So, let's first define dp[i]:
Dp[i] is the minimum number of elements that we need to make a sum equal to ‘i’.
Let us define a function “minimum( sum, num,dp)”, where “sum” is the remaining sum that we have to make, num is the array which is given to us and “dp” array is used to memorize the results.
Return ans
The idea is similar to the previous approach. In this approach, we use an iterative way to store the minimum number of elements that are required to make the target sum ‘X’.
dp[i]= the minimum number of elements to make sum ‘i’.
If we already have calculated the minimum number of elements to make sum [0,1,..,i-1] then we can iterate over all the elements of the array and try to put an ith element of the array as the last element to make sum equal to ‘i’.