Question:
Given a list, a number is said to be valid if its left and right members are smaller than the current number.
For the numbers at index = 0, assume that the left neighbor is Integer.MIN and for index = len-1, assume the right neighbor is Integer.MIN.
Return the order of the valid numbers.
How to get the valid numbers :
If in any iteration, there are more than 1 valid numbers, return the smallest. Do this until the entire arrays is processed.
Ex: arr = [ 1,4,3,4,5,2]
On first iteration = we get 4 at index 1 and 5 at index 4, so we return 4, now new array is [1,3,4,5,2]
On 2nd iteration with new array = [1,3,4,5,2], we get 5 at index 3 , now new array is [1,3,4,2]
On 3rd iteration with new array = [1,3,4,2], we get 4 at index 2 , now new array is [1,3,2]
On 4th iteration with new array = [1,3,2], we get 3 at index 1 , now new array is [1,2]
On 5th iteration with new array = [1,2], we get 2 at index 1 , now new array is [1] - because 1 on the left is than it and the right side we have Integer.min which is also less than 2.
On 6th iteration with new array = [1], we get 1 at index 0 because it's greater than the elements on the left and right which are Integer.min .
The output is there - [4, 5, 4, 3, 2, 1]
For example arr = [1, 2, 3, 4, 5, 6] - Notice the elements are in creasing order, the ones on the left are less than the current elements and the ones on the right are greater. So, this is what happens
On 1st iteration = we get 6 at index 5 because 6 > 5 and 6 > integer.min, so we return 6, now new array is [1, 2, 3, 4, 5]
On 2nd iteration = we get 5 at index 4 because 5 > 4 and 5 > integer.min, so we return 5, now new array is [1, 2, 3, 4] And so on....
The final output is - [6, 5, 4, 3, 2, 1]
Couldn't do it better than N^2
Result: Reject