Given an array of positive integers `nums`, remove the **smallest** subarray (possibly **empty**) such that the **sum** of the remaining elements is divisible by `p`. It is **not** allowed to remove the whole array. Return the length of the smallest subarray that you need to remove, or `-1` if it is impossible. A **subarray** is a contiguous block of elements in the array.
Given an array of positive integers nums, remove the smallest subarray (possibly empty) such that the sum of the remaining elements is divisible by p. It is not allowed to remove the whole array. Return the length of the smallest subarray that you need to remove, or -1 if it is impossible. A subarray is a contiguous block of elements in the array.
Example 1:
Input: {"nums":[3,1,4,2],"p":6}
Output: 1
Constraints:
1≤nums.length≤105
1≤nums[i]≤109
1≤p≤109
Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result
Run code to see results.
Input
arr ={"nums":[3,1,4,2],"p":6}
Add up every element: the sum is 10. Only its remainder mod 6 matters.
p 6
need = sum % p 4
cur -
want -
nums, with the prefix remainder (sum through that index) % p underneath
-1
start
0
0
3
·
1
1
·
2
4
·
3
2
·
■ current index■ prefix with remainder want, and the piece after it■ best piece so far