Skip to content

Commit 2b3149e

Browse files
authored
Merge pull request neetcode-gh#2548 from aadil42/patch-48
2 parents cbb0b97 + a728a66 commit 2b3149e

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* https://leetcode.com/problems/continuous-subarray-sum/
3+
* Hasing
4+
* Time O(n) | Space O(n)
5+
* @param {number[]} nums
6+
* @param {number} k
7+
* @return {boolean}
8+
*/
9+
var checkSubarraySum = function(arr, k) {
10+
let sum = 0;
11+
const remainderMap = new Map([ [0, -1] ]);
12+
13+
for(let i = 0; i < arr.length; i++) {
14+
sum += arr[i];
15+
if(remainderMap.has(sum%k) && i - remainderMap.get(sum%k) > 1) {
16+
return true;
17+
}
18+
if(!remainderMap.has(sum%k)) {
19+
remainderMap.set(sum%k,i);
20+
}
21+
}
22+
23+
return false;
24+
};

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy