Skip to content

Commit 5a79709

Browse files
committed
a
1 parent 92c7c26 commit 5a79709

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#include<stdio.h>
2+
3+
//Function to calculate the maximum sum of a sub-array of a given array using kadane's algorithm
4+
int maxSumarray(int a[], int size){
5+
int i;
6+
int max_sum_so_far=0;
7+
int max_ending_here = 0;
8+
9+
for(i=0;i<size;i++){
10+
max_ending_here = max_ending_here + a[i];
11+
if(max_ending_here < 0)
12+
max_ending_here =0;
13+
if(max_sum_so_far < max_ending_here)
14+
max_sum_so_far = max_ending_here;
15+
}
16+
return max_sum_so_far;
17+
}
18+
19+
20+
int main(){
21+
int i,size;
22+
23+
printf("Enter the size of the array ");
24+
scanf("%d",&size);
25+
26+
int a[size];
27+
printf("\n Enter the elements of the array");
28+
29+
for(i=0; i<size; i++)
30+
scanf("%d",&a[i]);
31+
32+
int max_sum = maxSumarray(a,size);
33+
34+
printf("\n The Maximum Sum of the Sub Array is : %d",max_sum);
35+
return 0;
36+
}

0 commit comments

Comments
 (0)