Added kadanes-algorithm in C programming language

pull/1155/head
Meowmaster 2023-02-20 19:29:24 +05:30
parent af47764be0
commit c8a18e6ab2
2 changed files with 59 additions and 0 deletions

View File

@ -3,6 +3,7 @@
## Arrays
- [Even and Odd](arrays/even-and-odd.c)
- [Kadane's Algorithm](arrays/kadanes-algorithm.c)
- [Unique Elements in an array](arrays/unique-elements-in-an-array.c)
- [Reverse an array](arrays/reverse-array.c)
- [Shuffle an array](arrays/shuffle_array.c)

View File

@ -0,0 +1,58 @@
/*
Kadane's Algorithm: Largest Sum Contiguous Subarray
Task: Find the maximum sum of the contiguous subarray.
Example 1:
Size: 8
Array: {-2, -3, 4, -1, -2, 1, 5, -3}
Here the subarray {4, -1, -2, 1, 5} has the maximum sum of 7
Time Complexity: O(N)
*/
#include<stdio.h>
#include<stdlib.h>
int maxSubArraySum(int *array,int size)
{
int bestSum=0,sum=0;
for(int i=0;i<size;i++)
{
sum=array[i]>sum+array[i]?array[i]:sum+array[i];
bestSum=bestSum>sum?bestSum:sum;
}
return bestSum;
}
int main()
{
int *array=NULL;
int size=0;
printf("Enter the size of the array: ");
scanf("%d",&size);
array=(int*)malloc(size*sizeof(int));
printf("Enter array elements: ");
for(int i=0;i<size;++i)
scanf("%d",&array[i]);
printf("Maximum contiguous sum is %d",maxSubArraySum(array,size));
return 0;
}
/*
Sample Input and Output
Enter the size of the array: 8
Enter array elements: -2 -3 4 -1 -2 1 5 -3
Maximum contiguous sum is 7
*/