
1. You have to return the minimum number of integers that Ninja must add.
2. The array is given in sorted order.
The first line of input contains an integer 'T' representing the number of test cases.
The first line of each test case contains two single space-separated integers ‘N’ and ‘M’ denoting the number of elements in the array and upper bound of the sum range respectively.
The second line of each test case contains 'N' space-separated integers.
For each test case, return a single integer denoting the minimum number of integers added in the array.
The output for each test case is printed in a separate line.
1 <= T <= 5
1 <= N <= 5000
1 <= M <= 10 ^ 9
1 <= ARR[i] <= 10 ^ 9
Where ‘ARR[i]’ denotes the element of the array at index i.
Time limit: 1 sec
You do not need to print anything. It has already been taken care of. Just implement the given function.
The idea here is to first find the smallest number that is not possible to form using sum of elements and then add that in the array.
Let's suppose that we can form all numbers from [1, ‘MISS’ - 1] using elements till ‘i’th index.
But we cant form ‘MISS’ and ‘i+1’th element is greater than ‘MISS’ so we need to add
‘MISS’ in the array to form it.
Now, we can able to make all sum from [1, 2 * ‘MISS’ - 1] and if ‘i+1’th element is still greater then we repeat the process else we add ‘i+1’th index in the range and continue the process.
For example: let suppose ‘ARR’ = [1,2,4,10] and ‘M’ = 20
Using the numbers 1, 2 and 4, we can form all sums from 0 to 7, but we can't form 8 and as the next element is 10 i.e. greater than 8 so it also does not help in forming 8 so we definitely need to add 8 in the array.
Now, we can form sum from 0 to 15 as now 10 is smaller than 15 we add 10 to the range and we can form sum from 0 to 25 which complete our range and we got the solution i.e. 1.
Algorithm: