-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmin_loss_hackkrank.cpp
More file actions
33 lines (29 loc) · 879 Bytes
/
min_loss_hackkrank.cpp
File metadata and controls
33 lines (29 loc) · 879 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
#include <iostream>
#include <set>
std::set<long long> myset;
long long p[200005];
int n;
int main()
{
long long ans = -10000000000;
std::cin >> n;
for (int i = 0; i < n; i++)
{
std::cin >> p[i];
// cant' myset.insert(p[i]) here since if we call uppr_bound later
// and we want the smallest of the bigg we have seen TILL NOW not the whole arr
// => if 8 6 => upr_bound(6) = 8 tho 8 is previous to 6
// 20 7 8 2 5 5-7=-2 => to get loss, we take the bigger-smaller => it - p[i]
}
std::set<long long>::iterator it;
long long curr;
for (int i = 1; i < n; i++)
{
it = myset.upper_bound(p[i]); // smallest of bigger we have seen
curr = p[i] - *it; // this is bound to be negative
ans = std::max(curr, ans);
myset.insert(p[i]);
}
std::cout << ans;
return 0;
}