Nearest greater number by interchanging the digits | Python3 program to find nearest greater value ; Find all the possible permutation of Value A . ; Convert into Integer ; Find the minimum value of A by interchanging the digits of A and store min1 . ; Driver code ; Convert integer value into to find all the permutation of the number ; count = 1 means number greater than B exists
min1 = 10 ** 9 NEW_LINE _count = 0 NEW_LINE def permutation ( str1 , i , n , p ) : NEW_LINE INDENT global min1 , _count NEW_LINE if ( i == n ) : NEW_LINE INDENT str1 = " " . join ( str1 ) NEW_LINE q = int ( str1 ) NEW_LINE if ( q - p > 0 and q < min1 ) : NEW_LINE INDENT min1 = q NEW_LINE _count = 1 NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT for j in range ( i , n + 1 ) : NEW_LINE INDENT str1 [ i ] , str1 [ j ] = str1 [ j ] , str1 [ i ] NEW_LINE permutation ( str1 , i + 1 , n , p ) NEW_LINE str1 [ i ] , str1 [ j ] = str1 [ j ] , str1 [ i ] NEW_LINE DEDENT DEDENT return min1 NEW_LINE DEDENT A = 213 NEW_LINE B = 111 NEW_LINE str2 = str ( A ) NEW_LINE str1 = [ i for i in str2 ] NEW_LINE le = len ( str1 ) NEW_LINE h = permutation ( str1 , 0 , le - 1 , B ) NEW_LINE if _count == 1 : NEW_LINE INDENT print ( h ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( - 1 ) NEW_LINE DEDENT