-
Notifications
You must be signed in to change notification settings - Fork 0
/
MaxSubArrydc.cpp
52 lines (47 loc) · 992 Bytes
/
MaxSubArrydc.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
41
42
43
44
45
46
47
48
49
50
51
52
#include <algorithm>
#include <bits/stdc++.h>
#include <climits>
using namespace std;
int maxSubCross(int *arr, int l, int r, int m) {
int lsum = INT_MIN;
int sum = 0;
for (int i = m; i >= l; i--) {
sum += arr[i];
lsum = max(lsum, sum);
}
int rsum = INT_MIN;
sum = 0;
for (int i = m + 1; i <= r; i++) {
sum += arr[i];
rsum = max(rsum, sum);
}
return lsum + rsum;
}
int maxSubArr(int *arr, int l, int r) {
if (l >= r) {
return arr[l];
}
int m = (l + r) / 2;
int ms = INT_MIN;
int ls = maxSubArr(arr, l, m), rs = maxSubArr(arr, m + 1, r),
cs = maxSubCross(arr, l, r, m);
if (ls > ms) {
ms = ls;
}
if (rs > ms) {
ms = rs;
}
if (cs > ms) {
ms = cs;
}
return ms;
}
int main() {
int n;
cin >> n;
int arr[n];
for (int i = 0; i < n; i++)
cin >> arr[i];
cout << maxSubArr(arr, 0, n - 1) << endl;
return 0;
}