-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsize-subarray-maximum-sum.cpp
More file actions
41 lines (37 loc) · 953 Bytes
/
Copy pathsize-subarray-maximum-sum.cpp
File metadata and controls
41 lines (37 loc) · 953 Bytes
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
// C++ program to print length of the largest
// contiguous array sum
#include<iostream>
#include<climits>
#include <map>
#include <vector>
using namespace std;
int maxSubArraySum(int a[], int size)
{
vector<pair<int, int>> res(size);
res[0] = make_pair(0, a[0]);
int maxSum = INT_MIN, maxIndex = -1;
for(int i = 1; i < size; i++) {
auto p = res[i-1];
int extSum = a[i] + p.second;
if(extSum > a[i]) {
res[i] = make_pair(p.first, extSum);
} else {
res[i] = make_pair(i, a[i]);
}
}
for(int i = 0; i < size; i++) {
if(res[i].second > maxSum) {
maxSum = res[i].second;
maxIndex = i;
}
}
return maxIndex - res[maxIndex].first + 1;
}
/*Driver program to test maxSubArraySum*/
int main()
{
int a[] = {-2, -3, 4, -1, -2, 1, 5, -3};
int n = sizeof(a)/sizeof(a[0]);
cout << maxSubArraySum(a, n);
return 0;
}