Skip to content

Commit 05824a3

Browse files
authored
Merge pull request deutranium#230 from AdityaVSM/master
Kadanes Algorithm in C
2 parents 54c7428 + 0a1bd38 commit 05824a3

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

Diff for: classicalAlgos/kadanesAlgorithm/kadanesAlgorithm.c

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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+
for(i=0;i<size;i++){
9+
max_ending_here = max_ending_here + a[i];
10+
if(max_ending_here < 0)
11+
max_ending_here =0;
12+
if(max_sum_so_far < max_ending_here)
13+
max_sum_so_far = max_ending_here;
14+
}
15+
return max_sum_so_far;
16+
}
17+
18+
int main(){
19+
int i,size;
20+
21+
printf("Enter the size of the array ");
22+
scanf("%d",&size);
23+
24+
int a[size];
25+
printf("\n Enter the elements of the array");
26+
27+
for(i=0; i<size; i++)
28+
scanf("%d",&a[i]);
29+
30+
int max_sum = maxSumarray(a,size);
31+
32+
printf("\n The Maximum Sum of the Sub Array is : %d",max_sum);
33+
return 0;
34+
}

0 commit comments

Comments
 (0)