We need to find the shortest subarray to remove so that the remaining elements sum to a multiple of p. The brute force approach tries every possible subarray length starting from 1. For each length, we slide a window across the array and check if removing that window leaves a sum divisible by p. We return the first (smallest) length l that works.
p, return 0.l from 1 to n - 1:l.p, return l.-1.class Solution:
def minSubarray(self, nums: List[int], p: int) -> int:
n = len(nums)
totSum = sum(nums)
if totSum % p == 0:
return 0
for l in range(1, n):
curSum = 0
for i in range(n):
curSum += nums[i]
if i >= l:
curSum -= nums[i - l]
remainSum = totSum - curSum
if remainSum % p == 0:
return l
return -1If the total sum has remainder r when divided by p, we need to remove a subarray whose sum also has remainder r (mod p). This transforms the problem into finding the shortest subarray with a specific remainder. Using prefix sums, if the current prefix has remainder curSum, we look for an earlier prefix with remainder (curSum - r + p) % p. A hash map stores the most recent index for each remainder, allowing O(1) lookups.
remain when divided by p. If remain is 0, return 0.{0: -1} to handle subarrays starting from index 0.p.(curSum - remain + p) % p.-1 if no valid subarray exists (or if removing it would leave an empty array).class Solution:
def minSubarray(self, nums: List[int], p: int) -> int:
total = sum(nums)
remain = total % p
if remain == 0:
return 0
res = len(nums)
cur_sum = 0
remain_to_idx = {0: -1}
for i, n in enumerate(nums):
cur_sum = (cur_sum + n) % p
prefix = (cur_sum - remain + p) % p
if prefix in remain_to_idx:
length = i - remain_to_idx[prefix]
res = min(res, length)
remain_to_idx[cur_sum] = i
return -1 if res == len(nums) else resWhen computing (curSum - remain) % p, the result can be negative in many programming languages. Always add p before taking modulo: (curSum - remain + p) % p. Failing to do this causes incorrect hash map lookups and wrong answers.
The hash map must start with {0: -1} to handle subarrays that start from index 0. Without this initialization, you cannot detect valid subarrays that begin at the first element of the array.
If the minimum length found equals n (the entire array), you must return -1 because removing all elements leaves an empty array, which is invalid. A common mistake is forgetting this edge case and returning n instead.