Similar to "Subarray Sum Equals K", but we track the remainder of the prefix sum when divided by k. If two prefix sums have the same remainder, the subarray between them is divisible by k.
- Initialize
remainders = {0: 1},curr_sum = 0,count = 0. - For each
numinnums:curr_sum += num.rem = curr_sum % k.- If
reminremainders:count += remainders[rem].
remainders[rem] = remainders.get(rem, 0) + 1.
- Return
count.
- Time Complexity: O(N).
- Space Complexity: O(K).
def subarrays_div_by_k(nums, k):
res = 0
prefix_sum = 0
count = {0: 1}
for n in nums:
prefix_sum += n
rem = prefix_sum % k
if rem in count:
res += count[rem]
count[rem] = count.get(rem, 0) + 1
return res