-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_0523.java
More file actions
28 lines (23 loc) · 900 Bytes
/
_0523.java
File metadata and controls
28 lines (23 loc) · 900 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
package com.github.aditya;
import java.util.HashMap;
import java.util.Map;
public class _0523 {
// 15 ms, faster than 96.52%, memory 57.8 MB, less than 87.14%
// Time Complexity O(n) and Space Complexity O(n)
class Solution {
public boolean checkSubarraySum(int[] nums, int k) {
Map<Integer, Integer> map = new HashMap<>(); // Remainder : Index Map
map.put(0, -1); // To handle cases when first array element is multiple of k and we don't want to consider it.
int total = 0;
for (int i = 0; i < nums.length; i++) {
total += nums[i];
int remainder = total % k;
if (!map.containsKey(remainder))
map.put(remainder, i);
else if (i - map.get(remainder) > 1)
return true;
}
return false;
}
}
}