Stack is used to find the next smaller element and keeping track of index of current iteration
while ( stk and arr [ i ] < arr [ stk [ - 1 ] ] ) : NEW_LINE INDENT minarr [ stk [ - 1 ] ] = i NEW_LINE stk . pop ( ) NEW_LINE DEDENT stk . append ( i ) NEW_LINE