Finding the next greater element of the array .
static void makeNext ( int arr [ ] , int n , int nextBig [ ] ) { Stack < pair > s = new Stack < > ( ) ; for ( int i = n - 1 ; i >= 0 ; i -- ) { nextBig [ i ] = i ; while ( ! s . empty ( ) && s . peek ( ) . first < arr [ i ] ) s . pop ( ) ; if ( ! s . empty ( ) ) nextBig [ i ] = s . peek ( ) . second ; s . push ( new pair ( arr [ i ] , i ) ) ; } }