let's say we have an array={9,1,7,4,5} and I want to query what is the nearest bigger element to the left and to the right of the index I'm querying for example the nearest bigger element for 5 is 7 (it doesn't have bigger element to the right because it's the last element)and for 1 is 7 to the right and 9 to the left .

I think it needs some data structure(to make it nlog(n) instead of n^2) so if someone can provide some code it would be better and thanks in advance