-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path523. Continuous Subarray Sum.java
68 lines (68 loc) · 2.26 KB
/
523. Continuous Subarray Sum.java
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
class Solution {
public boolean checkSubarraySum(int[] nums, int k) {
return this.optimized(nums, k);
}
private boolean optimized(int[] nums, int k) {
/**
* as this is a continuos subarray we can leverage prefix sum concept
* map = {0, -1};
* prefixsum += arr[i] % k;
* map.put(prefixsum, i);
* if (map.contains prefixsum && prefixsum == 0) return true with i -
* map[predixsum] >= 2;
*/
int n = nums.length;
int prefixSum = 0;
Map<Integer, Integer> map = new HashMap<>(); // <sum, index>
map.put(0, -1); // {special case to conform the check}
for (int i = 0; i < n; i++) {
prefixSum = (prefixSum + nums[i]) % k;
if (map.containsKey(prefixSum)) {
int start = map.get(prefixSum);
int windowsize = i - start;
if (windowsize >= 2) {
return true;
}
} else {
map.put(prefixSum, i);
}
}
// if (map.containsKey(prefixSum)) {
// int start = map.get(prefixSum);
// int windowsize = n - start;
// if (windowsize >= 2) {
// return true;
// }
// }
return false;
}
private boolean bruteforoce(int[] nums, int k) {
int n = nums.length;
boolean isFound = false;
for (int i = 0; i < n; i++) {
int cursum = nums[i];
boolean _isFound = false;
for (int j = i + 1; j < n; j++) {
cursum += nums[j];
int windowlength = j - i + 1;
if (windowlength >= 2 && (cursum % k) == 0) {
_isFound = true;
break;
}
}
if (_isFound) {
isFound = true;
break;
}
}
return isFound;
}
}