forked from tarunsinghofficial/HacktoberFest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Kadane's-Algorithm.c
47 lines (30 loc) · 859 Bytes
/
Kadane's-Algorithm.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include<stdio.h>
//Function to calculate the maximum sum of a sub-array of a given array.
int maxSumarray(int a[], int size){
int i;
int max_sum_so_far=0;
int max_ending_here = 0;
for(i=0;i<size;i++){
max_ending_here = max_ending_here + a[i];
if(max_ending_here < 0){
max_ending_here =0;
}
if(max_sum_so_far < max_ending_here){
max_sum_so_far = max_ending_here;
}
}
return max_sum_so_far;
}
int main(){
int i,size;
printf("Enter the size of the array ");
scanf("%d",&size);
int a[size];
printf("\n Enter the elements of the array");
for(i=0; i<size; i++){
scanf("%d",&a[i]);
}
int max_sum = maxSumarray(a,size); //function call.
printf("\n\n The Maximum Sum of the Sub Array is : %d",max_sum);
return 0;
}