Skip to content

Commit 09c74e5

Browse files
committed
added maximum subarray sum program
1 parent 5562583 commit 09c74e5

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

subarray.cpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
typedef long long int ll;
4+
5+
int main(){
6+
ios_base::sync_with_stdio(false);
7+
cin.tie(NULL);
8+
9+
ll n,i,sum=0,max=0,min=INT_MIN;
10+
cin>>n;
11+
int arr[n];
12+
for(i=0;i<n;i++){
13+
cin>>arr[i];
14+
}
15+
16+
for(i=0;i<n;i++){
17+
sum+=arr[i];
18+
if(sum<=0){
19+
if(sum>min){
20+
min = sum;
21+
}
22+
sum=0;
23+
}
24+
else{
25+
if(sum>max){
26+
max=sum;
27+
}
28+
}
29+
}
30+
if(max==0){
31+
cout<<min;
32+
}
33+
else
34+
cout<<max;
35+
36+
return 0;
37+
}

0 commit comments

Comments
 (0)