Stack is used to find the next larger element and keeps track of index of current iteration
while ( stk . Count != 0 && arr [ i ] > arr [ stk . Peek ( ) ] ) { maxarr [ stk . Peek ( ) ] = i - 1 ; stk . Pop ( ) ; } stk . Push ( i ) ; }