It is Thanksgiving and Monica had made a delicious turkey for the guests. Now Joey came before time and tried to eat the turkey. But luckily Monica caught him doing so. Now Monica does not want Joey to eat turkey. She knows Joey is weak in Math and calculation, so she gave Joey a task and asked her to eat turkey only after he completes the task. Following is the task Monica had given.
You are given an array of N elements. Your task is to find Special number for every element of the array. Special number for any element is the index of the next greater element of the next greater element of that element in the array. If no such number exists its special number will be -1.
Note : The next greater element for any element x of array is element which is greater than x, is on right side of x and is closest to x. So if y is next greater element for x and z next greater element for y, the index of z will be special number for x.
Joey wants to eat turkey as soon as possible so he ask you for your help.
Input :
First line contains single integer N, total number of elements in the array.
The next line contains the N space separated integers a0, a1, ..., an-1 where ai denote ith element of the array.
Output :
Print N space separated integers denoting special number for each element of the array. (Assume array to be zero-indexed i.e. index for first element will be 0 and find special number accordingly.)
Constrains :
1 <= N <= 1000000
1 <= ai <= 1000000 , 0 <= i <= N-1
Author : Rutul Patel
For 2, next greater element is 4 and next greater element for 4 is 5 so special number for 2 will be index of 5 in the array i.e. 3.
Similarly for 4 next greater element is 5 and for 5 next greater element is 8 so special number for 4 is index of 8 i.e. 5.
Similarly for 1 it is again 5. For 5, no such number exists in the array, so special number for 5 will be -1.
Also for 3 and 8 special number will be -1.