-
Notifications
You must be signed in to change notification settings - Fork 1
/
MaximumSubarray.cpp
60 lines (56 loc) · 1.28 KB
/
MaximumSubarray.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
53
54
55
56
57
58
59
60
/*
Kevin Guo
HackerRank Solution: MaximumSubarray
https://www.hackerrank.com/challenges/maxsubarray
*/
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
void best(vector<long> vals){
long sum, bestsum, consum, max;
bool allneg = true;
sum = 0; bestsum = 0; consum = 0; max = vals[0];
for (int i = 0; i < vals.size(); i++){
if (vals[i] > max){
max = vals[i];
}
if (vals[i] > 0){
consum += vals[i];
}
int val = sum + vals[i];
if (val > 0){
allneg = false;
sum = val;
}else{
sum = 0;
}
if (sum > bestsum){
bestsum = sum;
}
}
if (allneg){
bestsum = max;
consum = max;
}
cout << bestsum << " " << consum << endl;
}
int main() {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
int trials;
cin >> trials;
for (int i = 0; i < trials; i++){
vector<long> trialvals;
long size;
long val;
cin >> size;
for (long j = 0; j < size; j++){
cin >> val;
trialvals.push_back(val);
}
best(trialvals);
}
return 0;
}