-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
48 lines (29 loc) · 859 Bytes
/
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
#include<iostream>
#include<vector>
#include<stack>
using namespace std;
// Method to find the next greater element in an input vector
vector<int> nextGreaterElement(vector<int> &input) {
vector<int> result(input.size(), -1);
stack<int> keep;
keep.push(0);
for(int i=1; i<input.size(); i++) {
while(!keep.empty() && input[i] > input[keep.top()]) {
result[keep.top()] = input[i];
keep.pop();
}
keep.push(i);
}
return result;
}
int main()
{
// Find the Next Greater Element
vector<int> input {4, 5, 2, 25, 13, 7, 6, 12};
vector<int> nge = nextGreaterElement(input);
for(auto elem : nge) {
cout << elem << " ";
}
cout << endl;
// ------------------------------
}