Sort array such that absolute difference of adjacent elements is in increasing order
Last Updated :
26 Sep, 2022
Given an unsorted array of length N. The task is to sort the array, such that abs(a[i]-a[i+1]) < = abs(a[i+1]-a[i+2]) for all 0 < = i< N that is abs(a[0]-a[1]) < = abs(a[1]-a[2]) < = abs(a[2]-a[3]) and so on.
Examples:
Input: arr[] = {7, 4, 9, 9, -1, 9}
Output: {9, 7, 9, 4, 9, -1}
Explanation:
For first two elements the difference is abs(9-7)=2
For next two elements the difference is abs(7-9)=2
For next two elements the difference is abs(9-4)=5
For next two elements the difference is abs(7-4)=3
For next two elements the difference is abs(4-(-1))=5
Hence, difference array is 0, 0, 2, 3, 5.
Input: arr[] = {1, 4, 6, 7}
Output: {6, 4, 7, 1}
Explanation:
For first two elements the difference is abs(6-4)=2
For next two elements the difference is abs(4-7)=3
For next two elements the difference is abs(7-1)=6
Hence, difference array is 2, 3, 6.
Approach:
To solve the problem mentioned above we sort the given unsorted array in ascending order. Then run a loop from i = 1 to i < n/2 and push elements alternatively in a stack from first half and second half respectively, that is push a[i] once and a[n-i-1] once until the whole array elements get pushed into the stack.
The main observation of the problem is to check if the length of the given array is odd, then push element at index n/2 additionally in order to have all elements of the array in the stack. Then traverse the whole stack until the stack is not empty, and pop elements out of the stack and print them as a result.
Below is the implementation of the discussed approach:
C++
#include <bits/stdc++.h>
using namespace std;
void solve( int a[], int n)
{
sort(a, a + n);
stack< int > st;
for ( int i = 0; i < n / 2; i++) {
st.push(a[i]);
st.push(a[n - i - 1]);
}
if (n % 2 == 1)
st.push(a[n / 2]);
while (!st.empty()) {
int x = st.top();
printf ( "%d " , x);
st.pop();
}
}
int main()
{
int a[] = { 7, 4, 9, 9, -1, 9 };
int n = sizeof (a) / sizeof (a[0]);
solve(a, n);
return 0;
}
|
Java
import java.util.*;
class GFG{
static void solve( int a[], int n)
{
Arrays.sort(a);
Stack<Integer> st = new Stack<Integer>();
for ( int i = 0 ; i < n / 2 ; i++)
{
st.add(a[i]);
st.add(a[n - i - 1 ]);
}
if (n % 2 == 1 )
st.add(a[n / 2 ]);
while (!st.isEmpty())
{
int x = st.peek();
System.out.printf( "%d " , x);
st.pop();
}
}
public static void main(String[] args)
{
int a[] = { 7 , 4 , 9 , 9 , - 1 , 9 };
int n = a.length;
solve(a, n);
}
}
|
Python3
def solve(a, n):
a.sort()
st = []
for i in range (n / / 2 ):
st.append(a[i])
st.append(a[n - i - 1 ])
if (n % 2 = = 1 ):
st.append(a[n / / 2 ])
while (st ! = []):
x = st[ - 1 ]
print (x, end = " " )
st.pop()
if __name__ = = '__main__' :
a = [ 7 , 4 , 9 , 9 , - 1 , 9 ]
n = len (a)
solve(a, n)
|
C#
using System;
using System.Collections;
class GFG{
static void solve( int [] a, int n)
{
Array.Sort(a);
Stack st = new Stack();
for ( int i = 0; i < n / 2; i++)
{
st.Push(a[i]);
st.Push(a[n - i - 1]);
}
if (n % 2 == 1)
st.Push(a[n / 2]);
while (st.Count != 0)
{
Console.Write(st.Peek() + " " );
st.Pop();
}
}
public static void Main(String[] args)
{
int [] a = { 7, 4, 9, 9, -1, 9 };
int n = a.Length;
solve(a, n);
}
}
|
Javascript
<script>
function solve(a, n)
{
a.sort( function (a, b){ return a - b});
let st = [];
for (let i = 0; i < parseInt(n / 2, 10); i++)
{
st.push(a[i]);
st.push(a[n - i - 1]);
}
if (n % 2 == 1)
st.push(a[parseInt(n / 2, 10)]);
while (st.length != 0)
{
document.write(st[st.length - 1] + " " );
st.pop();
}
}
let a = [ 7, 4, 9, 9, -1, 9 ];
let n = a.length;
solve(a, n);
</script>
|
Time complexity: O(n*logn) as we are sorting the array
Auxiliary space: O(n) as we are using extra space for stack st
Share your thoughts in the comments
Please Login to comment...