Given an array arr[], the task is to arrange the array in such a way that the absolute difference between the adjacent elements is in increasing order.
Examples:
Input: arr[] = {8, 1, 2, 3, 0}
Output: 2 3 1 8 0
Explanation:
|2-3| = 1, |3-1| = 2, |1-8| = 7, |8-0| = 8
The absolute difference between the adjacent elements is in increasing order.
Input: -1 -2 3 4 5
Output: 3 4 -1 5 -2
Explanation:
|3-4| = 1, |4-(-1)| = 5, |-1-5| = 6, |5-(-2)| = 7
Approach: The idea is to sort the elements of the array and then take the middle element of the array and then repeat this step until the array is not empty. Below is an illustration of the approach with the help of an example:
Given Array be - {8, 1, 2, 3, 0}
After Sorting the array - {0, 1, 2, 3, 8}
Array Middle Ele Output Array
--------------- ------------- ---------------
{0, 1, 2, 3, 8} 5//2 = 2, 2 2
{0, 1, 3, 8} 4//2 = 2, 3 2, 3
{0, 1, 8} 3//2 = 1, 1 1, 2, 3
{0, 8} 2//2 = 1, 8 1, 2, 3, 8
{0} 1//2 = 0, 0 1, 2, 3, 8, 0
C++
#include<bits/stdc++.h>
using namespace std;
void sortDiff(vector< int > arr,
int n)
{
sort(arr.begin(), arr.end());
vector< int > out;
while (n > 0)
{
out.push_back(arr[n / 2]);
arr.erase(arr.begin() + n / 2);
n = n - 1;
}
for ( auto i : out)
cout << i << " " ;
}
int main()
{
vector< int > a = {8, 1, 2, 3, 0};
int n = 5;
sortDiff(a, n);
}
|
Java
import java.util.*;
class GFG{
static void sortDiff(Vector<Integer> arr, int n)
{
Collections.sort(arr);
Vector<Integer> out = new Vector<Integer>();
while (n > 0 )
{
out.add(arr.get(n / 2 ));
arr.remove(n / 2 );
n = n - 1 ;
}
for ( int i : out)
System.out.print(i + " " );
}
public static void main(String[] args)
{
Integer []a = { 8 , 1 , 2 , 3 , 0 };
Vector<Integer> arr = new Vector<Integer>(Arrays.asList(a));
int n = 5 ;
sortDiff(arr, n);
}
}
|
Python3
def sortDiff(arr, n):
arr.sort()
out = []
while n:
out.append(arr.pop(n / / 2 ))
n = n - 1
print ( * out)
return out
if __name__ = = "__main__" :
arr = [ 8 , 1 , 2 , 3 , 0 ]
n = 5
sortDiff(arr, n)
|
C#
using System;
using System.Collections.Generic;
class GFG{
static void sortDiff(List< int > arr, int n)
{
arr.Sort();
List< int > Out = new List< int >();
while (n > 0)
{
Out.Add(arr[n / 2]);
arr.RemoveAt(n / 2);
n = n - 1;
}
foreach ( int i in Out)
Console.Write(i + " " );
}
public static void Main(String[] args)
{
int []a = { 8, 1, 2, 3, 0 };
List< int > arr = new List< int >(a);
int n = 5;
sortDiff(arr, n);
}
}
|
Javascript
<script>
function sortDiff(arr,n)
{
arr.sort( function (a,b){ return a-b;});
let out = [];
while (n > 0)
{
out.push(arr[(Math.floor(n / 2))]);
arr.splice(Math.floor(n / 2),1);
n = n - 1;
}
for (let i=0;i< out.length;i++)
document.write(out[i] + " " );
}
let arr=[8, 1, 2, 3, 0 ];
let n = 5;
sortDiff(arr, n);
</script>
|
Time Complexity: O(n2) as erase function takes O(n) time in the worst case.
Auxiliary Space: O(n)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
24 Mar, 2023
Like Article
Save Article