Rearrange array such that difference of adjacent elements is in descending order | C ++ 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printArray ( int * a , int n ) { sort ( a , a + n ) ; int i = 0 ; int j = n - 1 ; while ( i <= j ) { if ( i == j ) { cout << a [ i ] << " ▁ " ; } else { cout << a [ j ] << " ▁ " ; cout << a [ i ] << " ▁ " ; } i = i + 1 ; j = j - 1 ; } cout << endl ; } int main ( ) { int arr1 [ ] = { 1 , 2 , 3 , 4 , 5 , 6 } ; int n1 = sizeof ( arr1 ) / sizeof ( arr1 [ 0 ] ) ; printArray ( arr1 , n1 ) ; }