Loading...
You are given a list of positive integers nums and an integer k.
Consider removing one contiguous sublist from nums. The sublist can be empty, but it cannot contain every element of nums. Removing a sublist deletes those elements and closes the gap, leaving the rest in their original order.
Return the length of the shortest sublist you can remove so that the sum of the remaining elements is divisible by k.
If the sum of nums is already divisible by k, you don't need to remove anything, and the answer is 0. If there's no valid sublist to remove, return -1.
Input: nums = [3, 1, 4, 2], k = 6
Output: 1
Explanation: Removing the sublist [4] leaves [3, 1, 2], which sums to 6. 6 is divisible by 6. The full sum, 10, is not divisible by 6, so removing nothing does not work, and no shorter removal is possible.
Input: nums = [2, 3, 1, 4], k = 5
Output: 0
Explanation: The sum of nums is 10, which is already divisible by 5, so no removal is needed.
nums.length ≤105nums[i] ≤109k ≤109Click "Run" to test with sample cases or "Submit" to run all tests.