Stack is used to find the next smaller element and keeping track of index of current iteration
while ( stk . Count != 0 && arr [ i ] < arr [ stk . Peek ( ) ] ) { minarr [ stk . Peek ( ) ] = i ; stk . Pop ( ) ; } stk . Push ( i ) ; }