You are viewing a single comment's thread. Return to all comments →
Worked with all test cases in Java
public static long kSub(int k, List<Integer> nums) { long currSum = 0; long[] sum = new long[nums.size()]; long count = 0; Map<Integer, Long> remainderMap = new HashMap<>(); remainderMap.put(0, 1L); for(int i = 0 ; i < nums.size(); i++){ currSum = currSum + nums.get(i); sum[i] = currSum; int remainder = (int)(sum[i] % k); if(!remainderMap.containsKey(remainder)){ remainderMap.put(remainder, 1L); }else{ long frequency = remainderMap.get(remainder); count = count + frequency; remainderMap.put(remainder, frequency + 1); } }
return count; }
}
Seems like cookies are disabled on this browser, please enable them to open this website
K-Subarrays
You are viewing a single comment's thread. Return to all comments →
Worked with all test cases in Java
}