File tree 1 file changed +34
-0
lines changed
classicalAlgos/kadanesAlgorithm
1 file changed +34
-0
lines changed Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments