Subarray Sums Divisible by K
Count the contiguous, non-empty stretches of nums whose sum is divisible by k (a sum of 0 counts as divisible). Hint: look at prefix sums modulo k.
Example 1
nums [4,5,0,-2,-3,1]
k 5
returns 7
7 stretches have a sum divisible by 5.
Example 2
nums [5]
k 9
returns 0
5 is not divisible by 9.
Constraints
1 <= len(nums) <= 30-20 <= nums[i] <= 202 <= k <= 10
Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.