You are given an array/list of positive integers ‘NUMS’ of size 'N' and positive integer 'P'. You need to remove the smallest subarray (possibly empty) such that the sum of the remaining elements is divisible by ‘P’.
Your task is to print the length of the smallest subarray that you need to remove, or -1 if it's impossible.
Note:
1. It is not allowed to remove the whole array.
2. A subarray is defined as a contiguous block of elements in the array.
Example:
Suppose given ‘NUMS’ is [3, 1, 4, 2] and ‘P’ is 6.
The sum of the elements in ‘NUMS’ is 10, which is not divisible by 6. We can remove the subarray [4], and the sum of the remaining elements is 6, which is divisible by 6.
So print ‘1’ as a length of the removed subarray [4].
The first line of input contains a single integer ‘T’ denoting the number of test cases given. Then next ‘T’ lines follow:
The first line of each test case input contains two single space-separated integers, where the first integer 'N' represents the number of the elements in the ‘NUMS’ array/list and the second integer is the value of ‘P’.
The next line of each test case contains ‘N’ space-separated integers denoting the elements of the ‘NUMS’ array/list.
Output Format:
For every test case, print the length of the smallest subarray (array/list) that you need to remove, or -1 if it's impossible.
Note:
You do not need to print anything, it has already been taken care of. Just implement the given function.
Constraint :
1 <= T <= 100
1 <= NUMS.length <= 5 * 10^3
1 <= NUMS[i] <= 10^9
1 <= P <= 10^9
Where T denotes the number of test cases, N denotes the number of elements present in the ‘NUMS’ array with ‘P’ being the number that is used to divide the numbers in the ‘NUMS’ array.
Time Limit: 1sec
2
4 9
6 3 5 2
3 3
1 2 3
2
0
Test Case 1:
Output 2 is the required size of the subarray which would be removed. We cannot remove a single element to get a sum divisible by 9. The best way is to remove the subarray [5, 2], leaving us with [6, 3] with a sum of 9.
Test Case 2:
The output 0 is the required size of the subarray which would be removed. Here the sum of 'NUMS' elements is 6 which is already divisible by 3. Thus we do not need to remove anything.
2
3 7
1 2 3
-1
Test Case 1:
The sum is not divisible by 7 and thus, the ouput is -1.
Try to use the brute-force approach using the fact that the task here is to find the length of the smallest subarray whose sum of elements will leave a remainder equal to (‘TOTAL_SUM’ % ‘P’).
Approach: The problem provides us with ‘NUMS’ (an array of numbers) and a value ‘P’ and wants us to find a subarray with the smallest length so that the sum of other elements can be divided by ‘P’.
Assuming that the sum of nums is ‘S’, the sum of the target subarray is ‘S1’, and the sum of other elements in the nums is ‘S2’. Then, ((‘S’ – ‘S1’) % P + ‘S2’ % P) must be equal to ‘S’ % P.
But, (‘S’ – ‘S1’) % P == 0 should be true.
Therefore, ‘S’ % P == ‘S1’ % P, so both subarray sum and the total sum should leave the same remainder when divided by P. Hence, the task is to find the length of the smallest subarray whose sum of elements will leave a remainder of (‘S’ % P).
The algorithm to calculate the required will be:
O(N), where ‘N’ is the number of elements in the array/list ‘NUMS’.
Since we are iterating over all the elements of the array/list ‘NUMS’ to check whether their required modulus is present in the keys of the dictionary and update the ‘RES’ with minimum sized sub-array hence, the complexity here grows by O(N).
O(N), where ‘N’ is the number of elements in the array/list ‘NUMS’.
Since, here, we require an O(N) amount of space for storing the respective remainders for indexes upto ‘POSITION’ - 1 in a map ‘MAP1’, therefore, the space complexity is O(N).