We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 64355b1 commit e4e905bCopy full SHA for e4e905b
java/523-Continuous-Subarray-Sum.java
@@ -0,0 +1,20 @@
1
+//We are basically storing sum%k and storing it in the hashmap and checking it.
2
+//Math logic is that the overall sum will get cancelled out because of modulo
3
+
4
+class Solution {
5
+ public boolean checkSubarraySum(int[] nums, int k) {
6
+ HashMap<Integer, Integer> map = new HashMap<>();
7
+ map.put(0, -1);
8
+ int sum = 0;
9
+ for (int i = 0; i<nums.length; i++) {
10
+ sum += nums[i];
11
+ int rem = sum%k;
12
+ if (map.containsKey(rem))
13
+ if (i - map.get(rem)>=2)
14
+ return true;
15
+ if (!map.containsKey(rem))
16
+ map.put(rem, i);
17
+ }
18
+ return false;
19
20
+}
0 commit comments