forked from sureshmangs/Code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnext_larger_element_in_array.cpp
80 lines (60 loc) · 1.96 KB
/
next_larger_element_in_array.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
68
69
70
71
72
73
74
75
76
77
78
79
80
/*
Given an array A of size N having distinct elements, the task is to find the next greater element for each element of the array in order of their appearance in the array. If no such element exists, output -1
Input:
The first line of input contains a single integer T denoting the number of test cases.Then T test cases follow. Each test case consists of two lines. The first line contains an integer N denoting the size of the array. The Second line of each test case contains N space separated positive integers denoting the values/elements in the array A.
Output:
For each test case, print in a new line, the next greater element for each array element separated by space in order.
Constraints:
1 <= T <= 100
1 <= N <= 107
1 <= Ai <= 1018
Example:
Input
2
4
1 3 2 4
4
4 3 2 1
Output
3 4 4 -1
-1 -1 -1 -1
Explanation:
Testcase1: In the array, the next larger element to 1 is 3 , 3 is 4 , 2 is 4 and for 4 ? since it doesn't exist hence -1.
*/
#include<bits/stdc++.h>
using namespace std;
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
cin>>t;
while(t--){
int n;
cin>>n;
long long a[n];
for(int i=0;i<n;i++) cin>>a[i];
stack<long long> s;
vector<long long> v;
for(int i=n-1;i>=0;i--){
if(s.empty()==true)
v.push_back(-1);
else if(s.empty()==false && s.top()> a[i])
v.push_back(s.top());
else if(s.empty()==false && s.top()<=a[i]){
while(s.empty()==false && s.top()<=a[i]){
s.pop();
if(s.empty()==true)
v.push_back(-1);
else if(s.empty()==false && s.top()> a[i])
v.push_back(s.top());
}
}
s.push(a[i]);
}
for(int i=n-1;i>=0;i--){
cout<<v[i]<<" ";
}
cout<<endl;
}
return 0;
}