File tree Expand file tree Collapse file tree 1 file changed +36
-0
lines changed
classicalAlgos/kadanesAlgorithm Expand file tree Collapse file tree 1 file changed +36
-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
+
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
+ }
You can’t perform that action at this time.
0 commit comments