Stack is used to find the next larger element and keeps track of index of current iteration
while ( len ( stk ) > 0 and arr [ i ] > arr [ stk [ - 1 ] ] ) : NEW_LINE INDENT maxarr [ stk [ - 1 ] ] = i - 1 NEW_LINE stk . pop ( ) NEW_LINE DEDENT stk . append ( i ) NEW_LINE