1class Solution {2public int subarraysDivByK(int[] nums, int k) {3HashMap<Integer, Integer> map = new HashMap<>();4int count = 0;5int sum = 0;6for (int i = 0; i < nums.length; i++) {7sum += nums[i];8int rem = sum % k;9if (rem < 0) {10rem = rem + k; // -4%3 == -1 and 2 both bec -4 = 3(-1) +(-1) = 3(-2) + 211}1213if (rem == 0) {14count++;15}16if (map.containsKey(rem)) {17count += map.get(rem);18}1920map.put(rem, map.getOrDefault(rem, 0) + 1);21}22return count;23}24}