-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnge.cpp
51 lines (39 loc) · 926 Bytes
/
nge.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
//Nearest greatest right
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
void findNGR(int arr[], int size);
int main(){
int size;
cout << "Enter the size of array :"; cin >> size;
int arr[size];
for(int i = 0; i < size; i++){
cin >> arr[i];
}
findNGR(arr, size);
return 0;
}
void findNGR(int arr[], int size){
stack<int> s;
vector<int> v;
for(int i = size-1; i >= 0; i--){
if(s.empty()){
v.push_back(-1);
}else if(s.top() > arr[i]){
v.push_back(s.top());
}else{
while(!s.empty() && s.top() <= arr[i]){
s.pop();
}
if(s.empty()){
v.push_back(-1);
}else{
v.push_back(s.top());
}
}
s.push(arr[i]);
}
for(int i = size-1; i>=0; i--){
cout << v.at(i) << " ";
}
}