# Sort array such that absolute difference of adjacent elements is in increasing order

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-a) < = abs(a-a) < = abs(a-a) 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++

 `// C++ implementation to Sort a given ` `// unsorted array of length n ` `// according to the given condition ` `#include ` `using` `namespace` `std; ` ` `  `// Function ` `void` `solve(``int` `a[], ``int` `n) ` `{ ` ` `  `    ``// sort the array in ascending order ` `    ``sort(a, a + n); ` ` `  `    ``// declare a stack data structure ` `    ``stack<``int``> st; ` ` `  `    ``// run a loop from i=0 to i

## Java

 `// Java implementation to Sort a given ` `// unsorted array of length n ` `// according to the given condition ` `import` `java.util.*; ` `class` `GFG{ ` ` `  `// Function ` `static` `void` `solve(``int` `a[], ``int` `n) ` `{ ` ` `  `    ``// sort the array in ascending order ` `    ``Arrays.sort(a); ` ` `  `    ``// declare a stack data structure ` `    ``Stack st = ``new` `Stack(); ` ` `  `    ``// run a loop from i=0 to i

## Python3

 `# Python3 implementation to sort a  ` `# given unsorted array of length n ` `# according to the given condition ` ` `  `# Function ` `def` `solve(a, n): ` ` `  `    ``# Sort the array in ascending ` `    ``# order ` `    ``a.sort() ` `     `  `    ``# Declare a list used as a  ` `    ``# stack data structure ` `    ``st ``=` `[] ` ` `  `    ``# Run a loop from i=0 to i

## C#

 `// C# implementation to Sort a given ` `// unsorted array of length n ` `// according to the given condition ` `using` `System; ` `using` `System.Collections; ` ` `  `class` `GFG{ ` ` `  `// Function ` `static` `void` `solve(``int``[] a, ``int` `n) ` `{ ` `     `  `    ``// Sort the array in ascending order ` `    ``Array.Sort(a); ` ` `  `    ``// Declare a stack data structure ` `    ``Stack st = ``new` `Stack(); ` ` `  `    ``// Run a loop from i=0 to i

Output:

```9 7 9 4 9 -1
```

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

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.

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.