Continuous Subarray Sum
Check for a subarray of at least two numbers whose sum is a multiple of k, by matching prefix sums mod k.
The problem
You get an array nums of non-negative integers and a positive integer k. Return true if some contiguous subarray of length at least 2 has a sum that is a multiple of k, where 0 counts as a multiple, and false otherwise.
Input nums = [3, 1, 5, 2], k = 11
Output true
The whole array adds up to 11; no shorter subarray works.
Input nums = [6, 1, 2], k = 6
Output false
6 alone is a multiple of 6, but it has length 1, and 6 + 1, 1 + 2 and 6 + 1 + 2 are not multiples of 6.
Key constraint: The subarray must have at least two elements, so a single number that is a multiple of k does not count.
How to spot the pattern
- The condition is about a subarray sum being divisible by k.
- Two prefix sums with the same remainder mod k differ by a multiple of k.
The idea
If two prefix sums leave the same remainder when divided by k, the numbers between them add up to a multiple of k. So walk the array, keep the running sum mod k, and remember the first index where each remainder appeared. When a remainder shows up again at least two positions after its first index, the subarray between them is long enough, and you are done.
Brute force: Check the sum of every subarray of length 2 or more, which takes O(n^2) time.
Step by step
- 1Start a map with remainder 0 at index -1, standing for the empty prefix.
- 2Walk through the array and update
rem = (rem + nums[i]) % k. - 3If
remis already in the map andi - first[rem] >= 2, returntrue. - 4If
remis not in the map yet, storefirst[rem] = i, and never overwrite an earlier index. - 5After the loop, return
false.
See the pattern move
The prefix sums pattern on a small example. Step through it, or press play.
Code
class Solution:
def checkSubarraySum(self, nums: List[int], k: int) -> bool:
first = {0: -1} # remainder -> first index where the prefix sum had it
rem = 0
for i, num in enumerate(nums):
rem = (rem + num) % k
if rem in first:
if i - first[rem] >= 2:
return True
else:
first[rem] = i # keep only the earliest index
return FalseComplexity
Time
O(n)
Space
O(min(n, k))
One pass; the map holds at most one index per remainder, so no more than min(n + 1, k) entries.
Watch out for
- Overwriting the stored index when a remainder repeats: in [5, 0, 0] with k = 3 the remainder 2 shows up at indices 0, 1 and 2, and only the first index finds the subarray [0, 0].
- Forgetting the start entry (remainder 0 at index -1), which misses answers that begin at index 0, like [2, 4] with k = 6.
- Counting a single element that is a multiple of k; the subarray needs at least two numbers.
Follow-up questions
What if the numbers could be negative?
In Java, C++ and JavaScript % can return a negative remainder, so normalize it with ((rem % k) + k) % k. Python's % already returns a value from 0 to k - 1.
How would you count every subarray whose sum is divisible by k?
Keep how many times each remainder has appeared and add that count at every step, the same way Subarray Sum Equals K counts prefix sums. That is Subarray Sums Divisible by K.
In Hinglish
Prefix sum ka remainder dekho. Same remainder dobara mile aur beech mein kam se kam do numbers hon, toh woh subarray k ka multiple hai.