MCPcopy Create free account
hub / github.com/Tiwarishashwat/InterviewCodes / Solution

Class Solution

SubarraySumEqualsK.java:1–27  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1class Solution {
2 public int subarraySum(int[] nums, int k) {
3
4 HashMap<Integer,Integer> map = new HashMap<>();
5
6 int sum=0,r=0;
7
8 map.put(0,1); // key is 0 and value is 1
9
10 for(int i=0;i<nums.length;i++)
11 {
12 sum+=nums[i];
13
14 if(map.containsKey(sum-k))
15 {
16 r=r+map.get(sum-k);
17 }
18 map.put(sum,map.getOrDefault(sum,0)+1);
19 // if(!map.containsKey(sum))
20 // {
21 // map.put(sum,0);
22 // }
23 // map.put(sum,map.get(sum)+1);
24 }
25 return r;
26 }
27}
28

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected