-
Notifications
You must be signed in to change notification settings - Fork 141
/
Copy pathnextlargerelement.cpp
74 lines (57 loc) · 1.39 KB
/
nextlargerelement.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
65
66
67
#include <bits/stdc++.h>
using namespace std;
vector<int> nextLargerElement( vector<int>& arr, int n) {
// int n=arr.size();
stack < int > s;
/* push the first element to stack */
s.push(arr[0]);
// iterate for rest of the elements
for (int i = 1; i < n; i++) {
if (s.empty()) {
s.push(arr[i]);
continue;
}
/* if stack is not empty, then
pop an element from stack.
If the popped element is smaller
than next, then
a) print the pair
b) keep popping while elements are
smaller and stack is not empty */
while (s.empty() == false && s.top() < arr[i])
{
cout<< arr[i] <<" ";
s.pop();
}
/* push next to stack so that we can find
next greater for it */
s.push(arr[i]);
}
/* After iterating over the loop, the remaining
elements in stack do not have the next greater
element, so print -1 for them */
while (s.empty() == false) {
cout << "-1"<<" ";
s.pop();
}
}
//A[n-1]=-1;
// for(int i=0;i<n;i++)
// {
// for(int j=i+1;j<n;j++)
// {
// if(A[i]<A[j])
// {
// A[i]=A[j];
// break;
// }
// }
// }
int main()
{
int n;
cin >> n;
vector<int> A(n);
for ( int i = 0; i < n; i++) cin >> A[i];
nextLargerElement(A,n);
}