Skip to content

Commit 96e2b87

Browse files
authored
Merge pull request ephremdeme#82 from yashasvimisra2798/Add-kadane's-Algorithm
Create Kadane's Algorithm
2 parents 8dd6724 + b294355 commit 96e2b87

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

Algorithms/Kadane's Algorithm

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#include<iostream>
2+
#include<climits>
3+
using namespace std;
4+
5+
int maxSubArraySum(int a[], int size)
6+
{
7+
int max_so_far = INT_MIN, max_ending_here = 0;
8+
9+
for (int i = 0; i < size; i++)
10+
{
11+
max_ending_here = max_ending_here + a[i];
12+
if (max_so_far < max_ending_here)
13+
max_so_far = max_ending_here;
14+
15+
if (max_ending_here < 0)
16+
max_ending_here = 0;
17+
}
18+
return max_so_far;
19+
}
20+
21+
int main()
22+
{
23+
int a[] = {1,4,7,9,0,4,5};
24+
int n = sizeof(a)/sizeof(a[0]);
25+
int max_sum = maxSubArraySum(a, n);
26+
cout << "Subarray with maximum sum is " << max_sum;
27+
return 0;
28+
}

0 commit comments

Comments
 (0)