If NG [ i ] = = - 1 then there is no smaller element on right side . We can find Right smaller of next greater by arr [ RS [ NG [ i ] ] ]
for i in range ( n ) : NEW_LINE INDENT if ( NG [ i ] != - 1 and RS [ NG [ i ] ] != - 1 ) : NEW_LINE INDENT print ( arr [ RS [ NG [ i ] ] ] , end = " ▁ " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " - 1" , end = " ▁ " ) NEW_LINE DEDENT DEDENT