forked from alexprut/HackerRank
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Solution.cpp
46 lines (41 loc) · 885 Bytes
/
Solution.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
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int maxSubArray(int arr[], int n) {
int maxSoFar = INT8_MIN;
int maxEnding = INT8_MIN;
for (int i = 0; i < n; i++) {
maxEnding = max(maxEnding + arr[i], arr[i]);
maxSoFar = max(maxSoFar, maxEnding);
}
return maxSoFar;
}
int maxSubsequence(int arr[], int n) {
int maxSoFar = INT8_MIN;
int sum = 0;
for (int i = 0; i < n; i++) {
maxSoFar = max(maxSoFar, arr[i]);
if (arr[i] > 0) {
sum += arr[i];
}
}
if (sum == 0) { return maxSoFar; }
return sum;
}
int main() {
int T;
cin >> T;
for (int t = 0; t < T; t++) {
int n;
cin >> n;
int arr[n];
for (int i = 0; i < n; i++) { cin >> arr[i]; }
cout << maxSubArray(arr, n);
cout << " " << maxSubsequence(arr, n);
cout << "\n";
}
return 0;
}