File tree 2 files changed +67
-0
lines changed
2 files changed +67
-0
lines changed Original file line number Diff line number Diff line change
1
+ #include < bits/stdc++.h>
2
+ using namespace std ;
3
+ bool checkifSubarraySumZero (int *arr, int n){
4
+ unordered_set<int >s;
5
+ int pre=0 ;
6
+ for (int i=0 ;i<n;i++){
7
+ pre+=arr[i];
8
+ if (pre==0 or s.find (pre)!= s.end ()){
9
+ return true ;
10
+ }
11
+ s.insert (pre);
12
+ }
13
+ return false ;
14
+ }
15
+ int main (int argc, char const *argv[]){
16
+ int n;
17
+ cin>>n;
18
+ int *arr= new int [n];
19
+ for (int i=0 ;i<n;i++){
20
+ cin>>arr[i];
21
+ }
22
+ bool b= checkifSubarraySumZero (arr,n);
23
+ if (b==0 ){
24
+ cout<<" No" <<endl;
25
+ }
26
+ else {
27
+ cout<<" yes" <<" \n " ;
28
+ }
29
+ return 0 ;
30
+ }
Original file line number Diff line number Diff line change
1
+ #include < bits/stdc++.h>
2
+ using namespace std ;
3
+ int checkIfSubarraySumZero (int *arr, int n){
4
+ unordered_map<int ,int >mp;
5
+ int pre=0 ;
6
+ int len=0 ;
7
+ for (int i=0 ;i<n;i++){
8
+ pre+= arr[i];
9
+ if (arr[i]==0 and len==0 ){
10
+ len=1 ;
11
+ }
12
+ if (pre=0 ){
13
+ len= max (len,i+1 );
14
+ }
15
+ if (mp.find (pre)!=mp.end ()){
16
+ len= max (len,i-mp[pre]);
17
+ }
18
+ else {
19
+ mp[pre]=i;
20
+ }
21
+ }
22
+ return len;
23
+ }
24
+
25
+ int main (){
26
+
27
+ int n;
28
+ cin>>n;
29
+ int *arr= new int [n];
30
+ for (int i=0 ;i<n;i++){
31
+ cin>>arr[i];
32
+ }
33
+ int b= checkIfSubarraySumZero (arr,n);
34
+ cout<<b<<endl;
35
+ return 0 ;
36
+
37
+ }
You can’t perform that action at this time.
0 commit comments