Skip to content

Commit 56466ef

Browse files
Merge pull request matthewsamuel95#956 from thtiiz/add-MaximumSumOfSubarray
Add Thtiiz
2 parents c08193d + 85ca7ff commit 56466ef

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#include <stdio.h>
2+
int max(int x,int y){
3+
return x>y? x:y;
4+
}
5+
int main() {
6+
int n,i,j;
7+
scanf("%d", &n);
8+
int arr[n];
9+
for(i=0; i<n; i++){
10+
scanf("%d", &arr[i]);
11+
}
12+
int all_max=arr[0], curr_max=arr[0]; //initial
13+
int templ,tempr,a_i,l=0,r=0; //initial
14+
templ=0,tempr=0;
15+
for(i=1;i<n;i++){
16+
// curr_max = max(arr[i], curr_max+arr[i]);
17+
if(arr[i]>curr_max+arr[i]){
18+
templ=i;
19+
tempr=i;
20+
curr_max=arr[i];
21+
}else{
22+
tempr++;
23+
curr_max=curr_max+arr[i];
24+
}
25+
// all_max = max(all_max, curr_max);
26+
if(curr_max>all_max){
27+
l=templ;
28+
r=tempr;
29+
all_max=curr_max;
30+
}
31+
}
32+
printf("%d %d %d", all_max,l,r);
33+
}

0 commit comments

Comments
 (0)