You are given an array, 'ARR', consisting of ‘N’ integers. You are also given two other integers, ‘SUM’ and ‘MAXVAL’. The elements of this array follow a special property that the absolute value of each element is not more than ‘MAXVAL’.
Your task is to determine the minimum number of integers required to be added into the array such that the sum of all the elements of the array becomes equal to ‘SUM’.
Note:All the new numbers being added to the array must follow the original property of the array.
The first line contains an integer ‘T’, which denotes the number of test cases to be run. Then, the T test cases follow.
The first line of each test case contains a single integer ‘N’.
The second line of each test case contains two space-separated integers, ‘SUM’ and ‘MAXVAL’, denoting the required sum of the array elements, and the maximum absolute value of each element in the array.
The third and the last line of each test case contains ‘N’ space-separated integers, denoting the elements of the array, ARR.
Output Format:
For each test case, print in a separate line a single integer, denoting the minimum number of integers required to be added into the array.
Output for each test case will be printed in a separate line.
Note:
You do not need to print anything. It has already been taken care of. Just implement the given function.
1 <= T <= 10
1 <= N <= 10^6
-10^9 <= SUM <= 10^9
1 <= MAXVAL <= 10^9
-MAXVAL <= NUMS[i] <= MAXVAL
Where 'T' denotes the number of test cases, ‘N’ represents the original number of elements present in the array, ‘SUM’ represents the required sum of all the elements in the array, ‘MAXVAL’ represents the maximum absolute value of any element in the array and ARR[i] represents the value of the elements in the array.
Time Limit: 1sec
1
3
10 4
2 -3 4
2
In this test case, the original sum of the array elements is 3. The required sum is 10 so we need some integers that can have a sum of 7. Since we can’t have an integer more than 4, we require at least two integers. In this case, adding 4 and 3 to the array will make the sum of the array 10, and thus, we print 2.
1
3
-10 5
2 1 4
4
In this test case, the original sum of the array elements is 7. The required sum is -10 so we need some integers that can have a sum of -17. Since we can’t have an integer more than 5, we require at least four integers. In this case, adding -5, -5, -5, and -2 to the array will make the sum of the array -10, and thus, we print 4.
Find the sum required to be added into the array
The approach is to find the sum of all the original elements of the array. Now, we can easily find the sum required to be added into the array. So we keep adding the maximum allowed absolute value of a number that can be added into the array as long as the sum of the array does not exceed the required value of ‘SUM’. Now, we can simply add an integer equal to the difference between the value of ‘SUM’ and the current sum of all the elements in the array.
Algorithm:
O(N), where N is the number of elements in the array.
We are going through the entire array and finding its sum. This takes linear time. The rest of the operations take constant time.
O(1)
We are not using any extra space