Skip to content

Commit bba761e

Browse files
authored
Create 560-subarray-sum-equals-k.js
Solved subarray sum equals k in JS.
1 parent 3cfb9a5 commit bba761e

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Problem link https://leetcode.com/problems/subarray-sum-equals-k
2+
// Time Complexity: O(n)
3+
4+
5+
var subarraySum = function(nums, k) {
6+
7+
8+
9+
const prefixMap = {};
10+
let totalSubArray = 0;
11+
let ongoingSum = 0;
12+
13+
prefixMap[0] = 1;
14+
for(let i = 0; i < nums.length; i++) {
15+
ongoingSum += nums[i];
16+
if(prefixMap[ongoingSum - k]){
17+
totalSubArray += prefixMap[ongoingSum - k];
18+
}
19+
prefixMap[ongoingSum] = (prefixMap[ongoingSum] ? prefixMap[ongoingSum] + 1: 1);
20+
}
21+
22+
return totalSubArray;
23+
};

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