Stretches With a Divisible Total
Given integers nums (possibly negative) and a positive integer k, return the number of non-empty contiguous subarrays whose sum is divisible by k.
Example 1
Input: nums = [1, 2, 3], k = 3 Output: 3 Explanation: [1, 2], [3] and [1, 2, 3] all have sums divisible by 3.
Example 2
Input: nums = [5], k = 9 Output: 0
Constraints
- 1 <= nums.length <= 10^5
- -10^4 <= nums[i] <= 10^4
- 1 <= k <= 10^4
Hint 1
A subarray sum is divisible by k when two prefix sums have the same remainder.
Hint 2
JavaScript % can return negative values, so normalise remainders into [0, k).
Good practice for: Data Scientist, Data Engineer, Machine Learning Engineer, AI Engineer (LLM & Generative AI), Backend Developer, Blockchain Developer
Run checks your code on 2 sample tests. Submit runs all 8 tests, including hidden ones, and earns points. Use console.log to debug. Python's first run can take a few seconds while it starts.