-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
64 lines (54 loc) · 1.29 KB
/
main.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
61
62
63
64
#include <iostream>
#include <vector>
using namespace std;
int bottomUp(vector<int> Array, int &start, int &end);
void printVector(vector<int> Array, int start, int end);
int main()
{
vector<int> Array;
Array.push_back(-3);
Array.push_back(1);
Array.push_back(-8);
Array.push_back(4);
Array.push_back(-1);
Array.push_back(2);
Array.push_back(1);
Array.push_back(-5);
Array.push_back(5);
vector<int> subArray;
int end = 0, start = 1;
cout << "Sum: " << bottomUp(Array, start, end) << endl;
cout << "Start: " << start << endl;
cout << "End: " << end << endl;
printVector(Array, start, end + 1);
return 0;
}
int bottomUp(vector<int> Array, int &start, int &end)
{
int sum = 0, n = Array.size();
vector<int> OPT(n);
OPT.at(0) = 0;
for (int i = 1; i < n; i++)
{
if (OPT.at(i - 1) + Array.at(i) < 0)
{
OPT.at(i) = 0;
start = i + 1;
}
else
{
OPT.at(i) = OPT.at(i - 1) + Array.at(i);
}
if (sum < OPT.at(i))
{
sum = OPT.at(i);
end = i;
}
}
return sum;
}
void printVector(vector<int> Array, int start, int end)
{
for (int i = start; i < end; i++)
cout << Array.at(i) << " ";
}