Rearrange array such that difference of adjacent elements is in descending order
Last Updated :
29 Aug, 2022
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:
C++
#include <bits/stdc++.h>
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);
}
|
Java
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);
}
}
|
Python3
def printArray(a, n):
a.sort();
i = 0 ;
j = n - 1 ;
while (i < = j):
if (i = = j):
print (a[i], end = " " );
else :
print (a[j], end = " " );
print (a[i], end = " " );
i = i + 1 ;
j = j - 1 ;
print ();
if __name__ = = "__main__" :
arr1 = [ 1 , 2 , 3 , 4 , 5 , 6 ];
n1 = len (arr1);
printArray(arr1, n1);
|
C#
using System;
class GFG {
static void printArray( int []a, int n)
{
Array.Sort(a);
int i = 0;
int j = n - 1;
while (i <= j)
{
if (i == j)
{
Console.Write(a[i] + " " );
}
else
{
Console.Write(a[j] + " " );
Console.Write(a[i] + " " );
}
i = i + 1;
j = j - 1;
}
Console.WriteLine();
}
public static void Main ( string [] args)
{
int []arr1 = { 1, 2, 3, 4, 5, 6 };
int n1 = arr1.Length;
printArray(arr1, n1);
}
}
|
Javascript
<script>
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( "<br>" );
}
var arr1 = [1, 2, 3, 4, 5, 6];
var n1 = arr1.length;
printArray(arr1, n1);
</script>
|
Time Complexity: O(N * logN) the inbuilt sort function takes N log N time to complete all operations, hence the overall time taken by the algorithm is N log N
Auxiliary Space: O(1) since no extra array is used so the space taken by the algorithm is constant
Share your thoughts in the comments
Please Login to comment...