Python3 program for the above approach
def next_permutation ( a ) : NEW_LINE INDENT for i in reversed ( range ( len ( a ) - 1 ) ) : NEW_LINE INDENT if a [ i ] < a [ i + 1 ] : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT return False NEW_LINE DEDENT j = next ( j for j in reversed ( range ( i + 1 , len ( a ) ) ) if a [ i ] < a [ j ] ) NEW_LINE a [ i ] , a [ j ] = a [ j ] , a [ i ] NEW_LINE a [ i + 1 : ] = reversed ( a [ i + 1 : ] ) NEW_LINE return True NEW_LINE DEDENT