-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmaximumSubarray.cpp
More file actions
63 lines (48 loc) · 1.08 KB
/
maximumSubarray.cpp
File metadata and controls
63 lines (48 loc) · 1.08 KB
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/*
Find the contiguous subarray within an array (containing at least one number) which has the largest sum.
For example, given the array [−2,1,−3,4,−1,2,1,−5,4],
the contiguous subarray [4,−1,2,1] has the largest sum = 6.
*/
#include <iostream>
using namespace std;
int maximumSubarray(int *a, int n, int& startIndex, int& endIndex)
{
if (n == 0)
{
return 0;
}
int globalMax(a[0]), localMax(a[0]), localStart(0), localEnd(0);
for (int i = 1; i < n; ++i)
{
localMax += a[i];
if (localMax < 0)
{
localMax = 0;
localStart = localEnd = i + 1;
}
else
{
localEnd = i;
if (localMax > globalMax)
{
startIndex = localStart;
endIndex = localEnd;
globalMax = localMax;
}
}
}
return globalMax;
}
int main(int argc, char ** argv)
{
int a[] = {-2,1,-3,4,-1,2,1,-5,4};
int startIndex(0), endIndex(0);
int maxSum = maximumSubarray(a, sizeof(a)/sizeof(int), startIndex, endIndex);
cout << "Maximum subarray sum is: " << maxSum << endl;
for (int i = startIndex; i <= endIndex; ++i)
{
cout << a[i] << ", " ;
}
cout << endl;
return 0;
}