-
Notifications
You must be signed in to change notification settings - Fork 0
/
SubarraysWithSumK.cpp
52 lines (47 loc) · 1.08 KB
/
SubarraysWithSumK.cpp
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
// Subarrays with sum K
// Given an unsorted array of integers, find the number of continuous subarrays having sum exactly equal to a given number k.
// { Driver Code Starts
#include <bits/stdc++.h>
using namespace std;
// } Driver Code Ends
//User function Template for C++
class Solution{
public:
int findSubArraySum(int Arr[], int N, int k)
{
// code here
unordered_map<int,int> mp;
int pre_sum = 0;
int ans = 0;
for(int i=0; i<N; i++){
pre_sum += Arr[i];
if(pre_sum == k){
ans++;
}
if(mp.find(pre_sum - k) != mp.end()){
ans += mp[pre_sum - k];
}
mp[pre_sum]++;
}
return ans;
}
};
// { Driver Code Starts.
int main()
{
int t;
cin>>t;
while(t--)
{
int N;
cin >> N;
int Arr[N];
for(int i=0;i<N;i++)
cin>>Arr[i];
int k;
cin>>k;
Solution obj;
cout<<obj.findSubArraySum(Arr, N, k)<<endl;
}
return 0;
} // } Driver Code Ends