Rearrange array such that difference of adjacent elements is in descending order
Given an array a[] with n integers the task is to rearrange the elements of the array in such a way that the differences of the adjacent elements are in descending order.
Examples:
Input : arr[] = {1, 2, 3, 4, 5, 6}
Output : 6 1 5 2 4 3
Explanation:
For first two elements the difference is abs(6-1)=5
For next two elements the difference is abs(1-5)=4
For next two elements the difference is abs(5-2)=3
For next two elements the difference is abs(2-4)=2
For next two elements the difference is abs(4-3)=1
Hence, difference array is 5, 4, 3, 2, 1.
Input : arr[] = {7, 10, 2, 4, 5}
Output : 10 2 7 4 5
Explanation:
For first two elements the difference is abs(10-2)=8
For next two elements the difference is abs(2-7)=5
For next two elements the difference is abs(7-4)=3
For next two elements the difference is abs(4-5)=1
Hence, difference array is 8, 5, 3, 1.
Approach:
To solve the problem mentioned above we have to sort the array and then print the elements in the following order for the even-sized array :
a[n], a[1], a[n-1], a[2] ….. a[(n/2)], [(n/2)+1]
And for odd-sized array, we have to print array elements in the following order:
a[n], a[1], a[n-1], a[2] ….. a[(n/2)+1]
Below is the implementation of the above approach:
// CPP implementation to Rearrange array // such that difference of adjacent // elements is in descending order #include <bits/stdc++.h> using namespace std; // Function to print array in given order void printArray(int* a, int n) { // Sort the array sort(a, a + n); int i = 0; int j = n - 1; // Check elements till the middle index while (i <= j) { // check if length is odd // print the middle index at last if (i == j) { cout << a[i] << " "; } // Print the remaining elements // in the described order else { cout << a[j] << " "; cout << a[i] << " "; } i = i + 1; j = j - 1; } cout << endl; } // Driver code int main() { // array declaration int arr1[] = { 1, 2, 3, 4, 5, 6 }; // size of array int n1 = sizeof(arr1) / sizeof(arr1[0]); printArray(arr1, n1); } |
6 1 5 2 4 3
GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details
Recommended Posts:
- Sort elements of an array in increasing order of absolute difference of adjacent elements
- Sort array such that absolute difference of adjacent elements is in increasing order
- Rearrange all elements of array which are multiples of x in decreasing order
- Rearrange all elements of array which are multiples of x in increasing order
- Search an element in an array where difference between adjacent elements is 1
- Minimize the maximum difference between adjacent elements in an array
- Rank of all elements in a Stream in descending order when they arrive
- Minimum absolute difference of adjacent elements in a circular array
- Sort an Array based on the absolute difference of adjacent elements
- Minimize the maximum absolute difference of adjacent elements in a circular array
- Missing occurrences of a number in an array such that maximum absolute difference of adjacent elements is minimum
- Sort an array in descending order based on the sum of its occurrence
- Sort prime numbers of an array in descending order
- Sorting rows of matrix in descending order followed by columns in ascending order
- Sorting rows of matrix in ascending order followed by columns in descending order
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

