Finding the previous greater element of the array .
static void makePrev ( int [ ] arr , int n , int [ ] prevBig ) { Stack < pair > s = new Stack < pair > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { prevBig [ i ] = - 1 ; while ( s . Count != 0 && s . Peek ( ) . first < arr [ i ] ) s . Pop ( ) ; if ( s . Count != 0 ) prevBig [ i ] = s . Peek ( ) . second ; s . Push ( new pair ( arr [ i ] , i ) ) ; } }