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