Rearrange array such that difference of adjacent elements is in descending order | Function to print array in given order ; Sort the array ; Check elements till the middle index ; check if length is odd print the middle index at last ; Print the remaining elements in the described order ; array declaration ; size of array
function printArray ( a , n ) { a . sort ( function ( a , b ) { return a - b ; } ) ; var i = 0 ; var j = n - 1 ; while ( i <= j ) { if ( i == j ) { document . write ( a [ i ] + " " ) ; } else { document . write ( a [ j ] + " " ) ; document . write ( a [ i ] + " " ) ; } i = i + 1 ; j = j - 1 ; } document . write ( " " ) ; } var arr1 = [ 1 , 2 , 3 , 4 , 5 , 6 ] ; var n1 = arr1 . length ; printArray ( arr1 , n1 ) ;