-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStock Exchange Losses.cpp
More file actions
41 lines (33 loc) · 943 Bytes
/
Stock Exchange Losses.cpp
File metadata and controls
41 lines (33 loc) · 943 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
#include <vector>
#include <iostream>
#include <string>
using namespace std;
static int findMaximumLoss(vector<int>);
int main() {
int numberOfValues;
cin >> numberOfValues; cin.ignore();
int difference = 0;
int highest = 0;
vector<int> *values = new vector<int>();
for (int i = 0; i < numberOfValues; i++) {
int val;
cin >> val; cin.ignore();
values->push_back(val);
cerr << "adding value " << val << endl;
}
cout << findMaximumLoss(*values) << endl;
}
static int findMaximumLoss(vector<int> stockValues) {
int maximalLoss = 0;
int lastPeakIndex = 0;
for (int i = lastPeakIndex + 1; i < stockValues.size(); i++) {
int lossOrGainFromLastPeak = stockValues.at(i) - stockValues.at(lastPeakIndex);
if (lossOrGainFromLastPeak < 0 && lossOrGainFromLastPeak < maximalLoss) {
maximalLoss = lossOrGainFromLastPeak;
}
else if (lossOrGainFromLastPeak > 0) {
lastPeakIndex = i;
}
}
return maximalLoss;
}