Generate a permutation of first N natural numbers having count of unique adjacent differences equal to K | Function to construct the lst with exactly K unique adjacent element differences ; Stores the resultant array ; Stores the left and the right most element of the range ; Traverse the array ; If k is even , the add left to array and increment the left ; If k is odd , the add right to array and decrement the right ; Repeat the steps for k - 1 times ; Print the resultant lst ; Driver Code
def makelst ( N , K ) : NEW_LINE INDENT lst = [ 0 for i in range ( N ) ] NEW_LINE left = 1 NEW_LINE right = N NEW_LINE for i in range ( N ) : NEW_LINE INDENT if ( K % 2 == 0 ) : NEW_LINE INDENT lst [ i ] = left NEW_LINE left = left + 1 NEW_LINE DEDENT else : NEW_LINE INDENT lst [ i ] = right NEW_LINE right = right - 1 NEW_LINE DEDENT if ( K > 1 ) : NEW_LINE INDENT K -= 1 NEW_LINE DEDENT DEDENT for i in range ( N ) : NEW_LINE INDENT print ( lst [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 6 NEW_LINE K = 3 NEW_LINE makelst ( N , K ) NEW_LINE DEDENT