You're given an array of positive integers nums and need to make the sum of the entire array divisible by a positive integer p. The twist? You can only achieve this by removing the smallest possible contiguous subarray.
Think of it as a precision surgery - you want to remove the minimal amount of elements while achieving your goal. The subarray you remove can be empty (if the sum is already divisible), but you cannot remove the entire array.
Your mission: Find the length of the smallest subarray that needs to be removed to make the remaining sum divisible by p. If it's impossible to achieve this goal, return -1.
Example: For nums = [3,1,4,2], p = 6, the total sum is 10. We need to remove a subarray with sum โก 4 (mod 6). The subarray [4] has sum 4, so removing it leaves sum 6, which is divisible by 6. Answer: 1.
Input & Output
Constraints
- 1 โค nums.length โค 105
- 1 โค nums[i] โค 109
- 1 โค p โค 109
- Important: Cannot remove the entire array