Generate a permutation of first N natural numbers having count of unique adjacent differences equal to K | Function to construct the list 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 list ; Driver code
function makeList ( N , K ) { var list = new Array ( N ) ; var left = 1 ; var right = N ; for ( var i = 0 ; i < N ; i ++ ) { if ( K % 2 == 0 ) { list [ i ] = left ; left = left + 1 ; } else { list [ i ] = right ; right = right - 1 ; } if ( K > 1 ) K -- ; } for ( var i = 0 ; i < N ; i ++ ) { document . write ( list [ i ] + " " ) ; } } var N = 6 ; var K = 3 ; makeList ( N , K ) ;