You are given a set of ‘N’ boxes. The boxes are given in the form of an array ‘gainPower’. Each box has some power associated with it. Also, you are given some initial power ‘power’. You are playing a game by choosing the boxes in such a way to maximize the total score. In one move, you can perform one of the two operations:
1. For any index ‘i’, If the current power is ‘power’, and gainPower[i] is less than or equal to ‘power’, you can lose gainPower[i] from your current power and gain 1 score and then remove it from the set of ‘N’ boxes.
2. For any index ‘i’, If the current power is ‘power’, and ‘score’ is greater than or equal to 1, you can gain gainPower[i] to your current power and lose 1 score and then remove it from the set of ‘N’ boxes.
Note:
The initial score is 0.
Each box can be used at most once.
The boxes can be used in any order.
You don’t have to remove all the boxes.
The first line of input contains an integer ‘T’, denoting the number of test cases. The test cases follow.
The first line contains two integers ‘N’, and ‘power’, which denotes the number of elements in the array ‘gainPower’, and the initial power you have.
The second line contains N integers denoting the elements of the array ‘gainPower’.
Output Format:
For each test case, print the maximum score you can have after choosing any number of boxes and performing any of the two operations from the boxes chosen.
Print the output of the test case in a separate line.
Note:
You are not required to print the expected output, it has already been taken care of. Just implement the function.
1 <= T <= 50
1 <= N <= 10^4
1 <= gainPower[i], power <= 10^4
Where ’T’ is the number of test cases, N denotes the number of elements in the array ‘gainPower’,gainPower[i] denotes the power associated with the box at index ‘i’, and power denotes the initial power you have.
Time Limit: 1 sec
2
4 100
50 300 100 200
4 100
50 50 50 50
2
2
In the first test case, there are 4 boxes with power [50, 300, 100, 200]. You have initial power = 100 and initial score = 0. We can follow the following steps to maximize the score:
Apply operation 1 on the box with power = 50. After applying the operation, gainPower = [300, 100, 200], power = 50, score = 1.
Apply operation 2 on the box with power = 300.
After applying the operation, gainPower = [100, 200], power = 350, score = 0.
Apply operation 1 on the box with power = 100. After applying the operation, gainPower = [200], power = 250, score = 1.
Apply operation 1 on the box with power = 200. After applying the operation, gainPower = [], power = 50, score = 2.
Hence, the answer is 2.
In the second test case, there are 4 boxes with power [50, 50, 50, 50]. You have initial power = 100 and initial score = 0. We can follow the following steps to maximize the score:
Apply operation 1 on the first box with power = 50. After applying the operation, gainPower = [50, 50, 50], power = 50, score = 1.
Apply operation 1 on the second box with power = 50. After applying the operation, gainPower = [50, 50], power = 0, score = 2.
Now, you can also apply operation 2 in other boxes but the maximum score you can get is 2. Hence, the answer is 2.
2
8 500
100 200 300 400 500 600 700 800
3 500
100 200 300
3
2
Can you use the two-pointer approach to find the answer?
The idea is to sort the given array ‘gainPower’ and then use the two-pointer approach. Use two pointers ‘i’ and ‘j’ - one at the beginning and the other at the end of the array ‘gainPower’.
If the current power is greater than the power of the ith box, then increase the score by 1, subtract the power of the ith box from the current power and remove the box from the set. Otherwise, if the score is greater than or equal to 1, add the power of the jth box from the current power.
Algorithm:
O(N*log(N)), where N is the number of boxes given.
As we are sorting the array ‘gainPower’ which takes O(N*log(N)) time. Hence, the overall time complexity is O(N*log(N)).
O(1), as we are using constant space.