-
Notifications
You must be signed in to change notification settings - Fork 0
/
Kadane_Algorithm.cpp
40 lines (29 loc) · 1007 Bytes
/
Kadane_Algorithm.cpp
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
#include <iostream>
using namespace std;
int kadane(int arr[], int n)
{
int max_so_far = 0;
int max_ending_here = 0;
// traverse the given array
for (int i = 0; i < n; i++)
{
// update the maximum sum of subarray "ending" at index `i` (by adding the
// current element to maximum sum ending at previous index `i-1`)
max_ending_here = max_ending_here + arr[i];
// if the maximum sum is negative, set it to 0 (which represents
// an empty subarray)
max_ending_here = max(max_ending_here, 0);
// update the result if the current subarray sum is found to be greater
max_so_far = max(max_so_far, max_ending_here);
}
return max_so_far;
}
int main()
{
int arr[] = { -2, 1, -3, 4, -1, 2, 1, -5, 4 };
int n = sizeof(arr)/sizeof(arr[0]);
cout << "The maximum sum of a contiguous subarray is " <<
kadane(arr, n);
return 0;
}
/*The maximum sum of a contiguous subarray is 6*/