-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBinarySubarrayWithSum.cpp
74 lines (66 loc) · 1.98 KB
/
BinarySubarrayWithSum.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/*
Given a binary array arr of size N and an integer target, return the number of non-empty subarrays with a sum equal to target.
Note : A subarray is the contiguous part of the array.
Example 1:
Input:
N = 5
target = 2
arr[ ] = {1, 0, 1, 0, 1}
Output: 4
Explanation: The 4 subarrays are:
{1, 0, 1, _, _}
{1, 0, 1, 0, _}
{_, 0, 1, 0, 1}
{_, _, 1, 0, 1}
Example 2:
Input:
N = 5
target = 5
arr[ ] = {1, 1, 0, 1, 1}
Output: 0
Explanation: There is no subarray with sum equal to target.
Your Task:
You don't need to read input or print anything. Your task is to complete the function numberOfSubarrays() which takes the array arr, interger N and an integer target as input and returns the number of subarrays with a sum equal to target.
Expected Time Complexity: O(N).
Expected Auxiliary Space: O(1).
*/
/// Hashmap Solution
class Solution {
public:
int numSubarraysWithSum(vector<int>& nums, int goal) {
long long n=nums.size(), counter=0, prefixSum=0;
long long presum[n+1];
for(int i=0;i<n+1;i++){
presum[i]=0;
}
for(int i=0;i<n;i++){
prefixSum+=nums[i];
int leftover=prefixSum-goal;
if(leftover>=0) counter+=presum[leftover];
if(prefixSum==goal) counter++;
presum[prefixSum]+=1;
}
return counter;
}
};
/// Sliding Window Approach (IDEA: get atmostsum (sum<=goal) and then remove sum<goal)
class Solution {
public:
int atmostSubarray(vector<int> &nums, int target){
if(target<0) return 0;
int i=0, j=0, result=0, sum=0, n=nums.size();
while(j<n){
sum+=nums[j];
while(sum>target){
sum-=nums[i];
i++;
}
result+=(j-i+1);
j++;
}
return result;
}
int numSubarraysWithSum(vector<int>& nums, int goal) {
return atmostSubarray(nums,goal)-atmostSubarray(nums,goal-1);
}
};