Sort an Array based on the absolute difference of adjacent elements
Given an array arr[] containing N integers, the task is to rearrange all the elements of array such that absolute difference between consecutive elements of the array are sorted in increasing order.
Examples
Input: arr[] = { 5, -2, 4, 8, 6, 5 }
Output: 5 5 6 4 8 -2
Explanation:
|5 – 5| = 0
|5 – 6| = 1
|6 – 4| = 2
|4 – 8| = 4
|8 – (-2)| = 10
Hence, the differences between adjacent elements are sorted.
Input: arr[] = { 8, 1, 4, 2 }
Output: 4 2 8 1
Explanation:
|2 – 4| = 2
|8 – 2| = 6
|1 – 8| = 7
Hence, the differences between adjacent elements are sorted.
Approach: The problem can be solved using Greedy Approach. We know that the maximum difference is between the minimum and maximum elements of the array. Using this fact, if we include one of the minimum element in the answer, then the next element included in the answer array will be the maximum element, then the third element included will be the second minimum, then the fourth element included will be the second maximum and so on will give the desired array.
Below are the steps:
- Sort the given array arr[] in increasing order.
- Choose the first maximum(say a) and minimum element(say b) from the sorted array and insert in the answer array(say ans[]) as {a, b}.
- Repeat the above steps by choosing the second, third, fourth… maximum and minimum element from the sorted array and insert it in the front of the answer array.
- After all the above operations the answer array has the desired result.
Below is the implementation of the above approach:
CPP
#include <bits/stdc++.h>
using namespace std;
void sortedAdjacentDifferences( int arr[], int n)
{
int ans[n];
sort(arr + 0, arr + n);
int l = 0, r = n - 1;
for ( int i = n - 1; i >= 0; i--) {
if (i % 2) {
ans[i] = arr[l];
l++;
}
else {
ans[i] = arr[r];
r--;
}
}
for ( int i = 0; i < n; i++) {
cout << ans[i] << " " ;
}
}
int main()
{
int arr[] = { 5, -2, 4, 8, 6, 4, 5 };
int n = sizeof (arr) / sizeof (arr[0]);
sortedAdjacentDifferences(arr, n);
return 0;
}
|
Java
import java.util.*;
class GFG {
static void sortedAdjacentDifferences( int arr[], int n)
{
int [] ans = new int [n];
Arrays.sort(arr);
int l = 0 , r = n - 1 ;
for ( int i = n - 1 ; i >= 0 ; i--) {
if (i % 2 == 1 ) {
ans[i] = arr[l];
l++;
}
else {
ans[i] = arr[r];
r--;
}
}
for ( int i = 0 ; i < n; i++) {
System.out.print(ans[i] + " " );
}
}
public static void main(String[] args)
{
int arr[] = { 5 , - 2 , 4 , 8 , 6 , 4 , 5 };
int n = arr.length;
sortedAdjacentDifferences(arr, n);
}
}
|
Python3
def sortedAdjacentDifferences(arr, n):
ans = [ 0 ] * n
arr = sorted (arr)
l = 0
r = n - 1
for i in range (n - 1 , - 1 , - 1 ):
if (i % 2 ):
ans[i] = arr[l]
l + = 1
else :
ans[i] = arr[r]
r - = 1
for i in range (n):
print (ans[i], end = " " )
if __name__ = = '__main__' :
arr = [ 5 , - 2 , 4 , 8 , 6 , 4 , 5 ]
n = len (arr)
sortedAdjacentDifferences(arr, n)
|
C#
using System;
class GFG {
static void sortedAdjacentDifferences( int [] arr, int n)
{
int [] ans = new int [n];
Array.Sort(arr);
int l = 0, r = n - 1;
for ( int i = n - 1; i >= 0; i--) {
if (i % 2 != 0) {
ans[i] = arr[l];
l++;
}
else {
ans[i] = arr[r];
r--;
}
}
for ( int i = 0; i < n; i++) {
Console.Write(ans[i] + " " );
}
}
public static void Main()
{
int [] arr = { 5, -2, 4, 8, 6, 4, 5 };
int n = arr.Length;
sortedAdjacentDifferences(arr, n);
}
}
|
Javascript
<script>
function sortedAdjacentDifferences(arr, n)
{
let ans = new Array(n);
arr.sort();
let l = 0, r = n - 1;
for (let i = n - 1; i >= 0; i--) {
if (i % 2) {
ans[i] = arr[l];
l++;
}
else {
ans[i] = arr[r];
r--;
}
}
for (let i = 0; i < n; i++) {
document.write(ans[i] + " " );
}
}
let arr = [ 5, -2, 4, 8, 6, 4, 5 ];
let n = arr.length;
sortedAdjacentDifferences(arr, n);
</script>
|
Time Complexity: O(N*log N), where N is the number of elements in the given array.
Auxiliary Space: O(N) because it uses extra space for array ans.
Last Updated :
25 Oct, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...