Rearrange array such that difference of adjacent elements is in descending order | Java implementation to 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 ; Driver code ; Array declaration ; Size of array
import java . util . * ; class GFG { static void printArray ( int [ ] a , int n ) { Arrays . sort ( a ) ; int i = 0 ; int j = n - 1 ; while ( i <= j ) { if ( i == j ) { System . out . print ( a [ i ] + " ▁ " ) ; } else { System . out . print ( a [ j ] + " ▁ " ) ; System . out . print ( a [ i ] + " ▁ " ) ; } i = i + 1 ; j = j - 1 ; } System . out . println ( ) ; } public static void main ( String [ ] args ) { int arr1 [ ] = { 1 , 2 , 3 , 4 , 5 , 6 } ; int n1 = arr1 . length ; printArray ( arr1 , n1 ) ; } }