You are given ‘N’ types of umbrellas, where each umbrella can shelter some number of people. Given the number of people each umbrella can shelter in the array “UMBRELLA”, you need to determine the minimum number of umbrellas required to cover exactly ‘M’ people with umbrellas.
Note -
You have an infinite number of umbrellas of each type.
If it is not possible to shelter exactly ‘M’ people then print -1.
The first line of input contains an integer 'T' representing the number of test cases.
The first line of each test case contains two space-separated integers ‘N’ denoting the number of elements in the array “UMBRELLA” and ‘M’ denoting the number of people required to cover with umbrellas.
The second line of each test case contains ‘N’ space-separated integers denoting the elements of the array “UMBRELLA”.
Output Format :
For each test case, print the minimum number of required umbrellas.
Print the output of each test case on a new line.
Note :
You don’t need to print anything; it has already been taken care of. Just implement the given function.
1 <= T <= 5
1 <= N <= 10^3
1 <= UMBRELLA[i] <= 10^9
1 <= M <= 10^3
Where ‘T’ represents the number of test cases, ‘N’ represents the number of types of umbrellas, “UMBRELLA[i]” represents the number of people it can cover, and ‘M’ denotes the number of people to cover with umbrellas.
Time Limit: 1 sec
2
3 30
5 10 25
4 11
1 5 6 9
2
2
For the first test case, we can use one umbrella of 25 people and one of 5 people.
For the second test case, we can use one umbrella of 6 people and one of 5 people.
2
2 3
2 4
5 15
5 1 3 2 4
-1
5
For the first test case, we cannot cover exactly 3 people using any subarray of given umbrella.
For the second test case, we can use all umbrellas and cover all 15 peoples.
Can you think of trying all possible options?
Approach: The idea here is to try every possible option and consider the minimum ans.
We have two possible options: either we use the i-th umbrella to cover people or not.
So, the recurrence for the above problem is:
minNumberOfUmbrellas(i, M) = min(minNumberOfUmbrellas(i - 1, M), 1+ minNumberOfUmbrellas(i, M - UMBRELLA[i])).
minNumberOfUmbrellas(i, M) represents the minimum number of umbrellas required to shelter M people with first i umbrellas.
If we skip the i-th umbrella then the sub-problem is the same as solving for minNumberOfUmbrellas(i - 1, M) - a minimum number of umbrellas required to shelter M people with the first i - 1 umbrella.
If we consider the i-th umbrella then the sub-problem is the same as solving for 1 + minNumberOfUmbrellas(i, M - UMBRELLA[i]) - a minimum number of umbrellas required to shelter M - UMBRELLA[i] people with first i umbrellas (since we have an infinite umbrella of i-th type). Since we are considering the i-th umbrella we add 1 to the sub-problem.
The algorithm is as follows:
O(2^N * M), where N is the number of types of umbrellas and M is the number of people to shelter with umbrellas.
Since at every step we have two options either we consider i-th umbrella or not. And in the worst case, we can have one umbrella for each of the M people. Hence the overall time complexity is O(2^N * M).
O(N), where N is the number of types of umbrellas.
Since the recursive call stack can grow up to the order of N. Hence the overall space complexity is O(N).