forked from rituparna-ui/hacktorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnext_greater_array.cpp
More file actions
57 lines (54 loc) · 1.13 KB
/
next_greater_array.cpp
File metadata and controls
57 lines (54 loc) · 1.13 KB
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
#include<bits/stdc++.h>
using namespace std;
int main()
{
cout<<"testcases\n";
int t;
cin>>t;
while(t--)
{
cout<<"size\n";
int size;
cin>>size;
int x[size];
for(int i=0;i<size;i++)
{
cin>>x[i];
}
stack<int>satya;
satya.push(x[size-1]);
vector<int>satyam;
cout<<-1<<endl;
satyam.push_back(-1);
for(int i=size-2;i>=0;i--)
{
if(satya.top()<x[i])
{
while(!satya.empty() && satya.top()<x[i] )
{
satya.pop();
}
if(satya.empty()){
// cout<<-1<<" ";
satyam.push_back(-1);
}
else{
// cout<<satya.top()<<" ";
satyam.push_back(satya.top());
}
satya.push(x[i]);
}
else
{
// cout<<satya.top()<<" ";
satyam.push_back(satya.top());
satya.push(x[i]);
}
}
cout<<endl;
for(int i=satyam.size()-1;i>=0;i--){
cout<<satyam[i]<<" ";
}
cout<<endl;
}
}