-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsubarraySumK.cpp
More file actions
56 lines (49 loc) · 1.49 KB
/
subarraySumK.cpp
File metadata and controls
56 lines (49 loc) · 1.49 KB
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
53
54
55
56
#include <iostream>
#include <vector>
#include <unordered_map>
using namespace std;
// SUBARRAY SUM EQUALS K
// given an array of integers, find the number of subarrays that add up to k.
// Brute force: loop through the entire array once and select its end thru another loop and calculate the sum
int bruteForce(vector<int> nums, int k){
int n = nums.size(), ans=0;
for(int i=0;i<n;i++){
int currsum = 0;
for(int j=i;j<n;j++){
currsum += nums[j];
if(currsum == k){
ans ++;
}
}
}
return ans;
}
int main(){
vector <int> nums = {1,1,1};
int k = 2;
cout<<"Answer: "<<bruteForce(nums,k)<<endl;
// prefix sum
// define an array that stores the sum of all numbers until that index
int n=nums.size(),ans=0;
vector<int> presum(n);
presum[0]=nums[0];
for(int i=1;i<n;i++){
presum[i] = presum[i-1]+nums[i];
}
// we are looking for the subarrays that add up to k
// C1 ) the subarray from 0->m is equal to k
// C2 ) some intermediate subarray {target = k-presum[current]}
// for the case 2, we need a map to keep track of the number of times target has appeared in the past...
unordered_map <int,int> m;
for(int i=0;i<n;i++){
if(presum[i]==k){
ans++;
}
if(m.find(presum[i]-k)!=m.end()){
ans += m[presum[i]-k];
}
m[presum[i]] ++;
}
cout<<"answer using prefix sum: "<<ans<<endl;
return 0;
}