974. Subarray Sums Divisible by K

Given an array nums of integers, return the number of (contiguous, non-empty) subarrays that have a sum divisible by k.

1
2
3
4
5
6
Example 1:

Input: nums = [4,5,0,-2,-3,1], k = 5
Output: 7
Explanation: There are 7 subarrays with a sum divisible by k = 5:
[4, 5, 0, -2, -3, 1], [5], [5, 0], [5, 0, -2, -3], [0], [0, -2, -3], [-2, -3]

Note:

  • 1 <= nums.length <= 30000
  • -10000 <= nums[i] <= 10000
  • 2 <= k <= 10000

Solution

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
class Solution {
    public int subarraysDivByK(int[] nums, int k) {
        Map<Integer, Integer> prefix = new HashMap<>();
        prefix.put(0, 1);
        int sum = 0;
        int count = 0;
        for (int i = 0; i < nums.length; i++) {
            sum += nums[i];
            sum = sum % k;
            if (sum < 0) {
                sum += k;
            }
            count += prefix.getOrDefault(sum, 0);
            prefix.put(sum, prefix.getOrDefault(sum, 0) + 1);
        }
        return count;
    }
}