FAANG · LeetCode #974
Subarray Sums Divisible by K — three-stage drill
1The ProblemMedium
Subarray Sums Divisible by K
Count contiguous subarrays whose sum is divisible by k.
Examples
[4,5,0,-2,-3,1], k=5→ 7[1,2,3], k=7→ 0
constraintValues may be negative (use ((p % k) + k) % k).
2The PatternTIME O(n) · SPACE O(n)
Prefix Sum
Running cumulative totals turn range-sum questions into O(1) lookups.
Problems using this pattern
3The SolutionRESULT → 7
Step through it
start: seen = {0: 1}, count = 0
Step 1 / 8
Arrow keys to navigate · escape to reset