Lexicographically smallest permutation of the array possible by at most one swap | ''Function to print the elements of the array arr[] ; '' Traverse the array arr[] ; ''Function to convert given array to lexicographically smallest permutation possible by swapping at most one pair ; '' Stores the index of the first  element which is not at its  correct position ; '' Checks if any such array  element exists or not ; '' Traverse the given array ; '' If element is found at i ; '' If the first array is  not in correct position ; '' Store the index of  the first elements ; '' Store the index of  the first element ; '' Swap the pairs ; '' Print the array ; ''Given array ; ''Store the size of the array
def printt ( arr , N ) : NEW_LINE INDENT for i in range ( N ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT def makeLexicographically ( arr , N ) : NEW_LINE INDENT index = 0 NEW_LINE temp = 0 NEW_LINE check = 0 NEW_LINE condition = 0 NEW_LINE element = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT if ( element == arr [ i ] ) : NEW_LINE INDENT check = i NEW_LINE break NEW_LINE DEDENT elif ( arr [ i ] != i + 1 and check == 0 ) : NEW_LINE INDENT index = i NEW_LINE check = 1 NEW_LINE condition = - 1 NEW_LINE element = i + 1 NEW_LINE DEDENT DEDENT if ( condition == - 1 ) : NEW_LINE INDENT temp = arr [ index ] NEW_LINE arr [ index ] = arr [ check ] NEW_LINE arr [ check ] = temp NEW_LINE DEDENT printt ( arr , N ) NEW_LINE DEDENT arr = [ 1 , 2 , 3 , 4 ] NEW_LINE N = len ( arr ) NEW_LINE makeLexicographically ( arr , N ) NEW_LINE