Monk and Prisoner of Azkaban

5

1 votes
Data Structures, Stack, Easy, Stack
Problem

Monk's wizard friend Harry Potter is excited to see his Dad fight Dementors and rescue him and his Godfather Sirius Black. Meanwhile their friend Hermoine is stuck on some silly arrays problem. Harry does not have time for all this, so he asked Monk to solve that problem for Hermoine, so that they can go.

The problem is given an array A having N integers, for each i(1iN), find x+y, where x is the largest number less than i such that A[x]>A[i] and y is the smallest number greater than i such that A[y]>A[i]. If there is no x<i such that A[x]>A[i], then take x=1. Similarly, if there is no y>i such that A[y]>A[i], then take y=1.

enter image description here

Input Format:
First line consists of a single integer denoting N.
Second line consists of N space separated integers denoting the array A.

Output Format:
Print N space separated integers, denoting x+y for each i(1iN)

Constraints:
1N106
1A[i]1018

Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

Values of x for each i: 1,1,2,2,4
Values of y for each i: 1,1,4,1,1
So, x+y for each i: 2,0,6,1,3

Editor Image

?