We’re preparing your current view and syncing the latest data.
Given an array of positive integers nums and a positive integer p, find the smallest subarray that you can remove from nums so that the sum of the remaining elements is divisible by p. Return the length of the smallest such subarray. If it is not possible to make the sum divisible by p by removing any subarray, return -1.
An integer array nums and an integer p.
An integer representing the length of the smallest subarray to remove, or -1 if not possible.
1 <= nums.length <= 10^5; 1 <= nums[i] <= 10^9; 1 <= p <= 10^9.
Example 1
Input
nums = [3,1,4,2], p = 6
Output
1
Explanation
Removing subarray [4] leaves sum 6, which is divisible by 6.
Example 2
Input
nums = [6,3,5,2], p = 9
Output
2
Explanation
Removing subarray [5,2] leaves sum 9, divisible by 9.
Example 3
Input
nums = [1,2,3], p = 3
Output
0
Explanation
Sum is already divisible by 3; no removal needed.