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