-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday34.cpp
More file actions
33 lines (30 loc) · 710 Bytes
/
day34.cpp
File metadata and controls
33 lines (30 loc) · 710 Bytes
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
#include<bits/stdc++.h>
using namespace std;
// Given an array of integers, find maximum length sub-array having given sum.
void findMaxSum(vector<int> &arr, int n, int sum){
unordered_map<int,int> mp;
mp[0]=-1;
int last=-1;
int s=0;
int len=0;
for(int i=0;i<n;i++){
s+=arr[i];
if(mp.find(s) == mp.end()){
mp[s]=i;
}
if(mp.find(s-sum)!=mp.end() && len < i - mp[s-sum]){
len = i-mp[s-sum];
last = i;
}
}
cout<<"["<<(last-len+1)<<", "<<last<<"]"<<endl;
}
main(){
int n,i,sum=0;
cin>>n;
vector<int>arr(n);
for(i=0;i<n;i++)
cin>>arr[i];
cin>>sum;
findMaxSum(arr,n,sum);
}