Vector to store the left and right index for each i such that left [ i ] > arr [ i ] and right [ i ] > arr [ i ]
int [ ] left = new int [ n ] ; int [ ] right = new int [ n ] ; Stack < pair < Integer , Integer > > s = new Stack < > ( ) ; pair < Integer , Integer > x = new pair < > ( ) ; x . first = Integer . MAX_VALUE ; x . second = - 1 ; s . push ( x ) ;