


1. It is not allowed to remove the whole array.
2. A subarray is defined as a contiguous block of elements in the array.
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.
For every test case, print the length of the smallest subarray (array/list) that you need to remove, or -1 if it's impossible.
You do not need to print anything, it has already been taken care of. Just implement the given function.
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
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:
Approach: The problem provides us ‘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, we will have ‘S’ = ‘S1’ + ‘S2’.
Now we know that ‘S2’ % ‘P’ == 0, we take modular ‘P’ for equation ‘S’ = ‘S1’ + ‘S2’, then we have ‘S’ % ‘P’= ‘S1’ % ‘P’. As ‘S’ is a known number i.e., sum(‘NUMS’), we here use the variable ‘TARGET’ to denote ‘S’ % ‘P’.
After that, the problem becomes finding a subarray with the smallest length and satisfying the equation ‘S1’ % ‘P’ == ‘TARGET’, where ‘S1’ is the sum of a subarray.
The algorithm to calculate the required will be: