-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMystical.java
More file actions
38 lines (38 loc) · 909 Bytes
/
Mystical.java
File metadata and controls
38 lines (38 loc) · 909 Bytes
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
import java.util.*;
class Mystical
{
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
int n=in.nextInt();
int arr[]=new int[n];
int nge[]=new int[n];
int ans[]=new int[n];
for(int i=0;i<n;i++)
arr[i]=in.nextInt();
Stack<Integer> st=new Stack<>();
for(int i=n-1;i>=0;i--)
{
while(!st.isEmpty() && arr[st.peek()]<=arr[i])
st.pop();
nge[i]=st.isEmpty()? -1:st.peek();
st.push(i);
}
st.clear();
int nse[]=new int[n];
for(int i=n-1;i>=0;i--)
{
while(!st.isEmpty() && arr[st.peek()]>=arr[i])
st.pop();
nse[i]=st.isEmpty()? -1:st.peek();
st.push(i);
}
for(int i=0;i<n;i++)
{
int index=nge[i];
ans[i]=(index==-1||nse[index]==-1)? -1:arr[nse[index]];
}
for(int v:ans)
System.out.print(v+" ");
}
}