Given an array of n distinct elements and a number x, arrange array elements according to the absolute difference with x, i. e., the element having a minimum difference comes first and so on.**Note:** If two or more elements are at equal distance arrange them in same sequence as in the given array.

**Examples:**

Input :x = 7, arr[] = {10, 5, 3, 9, 2}Output :arr[] = {5, 9, 10, 3, 2}

7 – 10 = 3(abs)

7 – 5 = 2

7 – 3 = 4

7 – 9 = 2(abs)

7 – 2 = 5

So according to the difference with x,

elements are arranged as 5, 9, 10, 3, 2.

Input :x = 6, arr[] = {1, 2, 3, 4, 5}Output :arr[] = {5, 4, 3, 2, 1}

Input :x = 5, arr[] = {2, 6, 8, 3}Output :arr[] = {6, 3, 2, 8}

**Approach:**

The above problem has been explained in the below post:

Sort an array according to absolute difference with given value

Sort an array according to absolute difference with a given value “using constant extra space”

The above-mentioned solutions have a time complexity of O(nlogn) with an auxiliary space of O(n) and O(n^2) with auxiliary space of O(1) respectively. In this post, a solution with time complexity of O(nlogn) using an auxiliary space of O(1) is proposed.

The solution is based on Functors. Compare the absolute value of the difference of the given number, k with the array value arr[i] and returns true if the value of the first object is less than the second object. Using stable_sort order of equivalent values is preserved.

Below is the implementation of the above approach:

`// C++ program to sort an array according` `// absolute difference with x.` `#include <bits/stdc++.h>` `using` `namespace` `std;` ` ` `// Functor class to perform comparison` `class` `compare {` `private` `:` ` ` `int` `num;` ` ` `public` `:` ` ` `compare(` `int` `n)` ` ` `{` ` ` `num = n;` ` ` `}` ` ` ` ` `// Overloads () operator to perform` ` ` `// the desired comparison` ` ` `int` `operator()(` `int` `arr_num1, ` `int` `arr_num2)` ` ` `{` ` ` `return` `abs` `(num - arr_num1) <` ` ` `abs` `(num - arr_num2);` ` ` `}` `};` ` ` `// Function to sort an array according to` `// absolute difference with x.` `void` `rearrange(` `int` `arr[], ` `int` `n, ` `int` `k)` `{` ` ` `// stable_sort sorts all the values in` ` ` `// non-decreasing order and preserves the` ` ` `// order of elements with equal values` ` ` `stable_sort(arr, arr + n, compare(k));` `}` ` ` `// Function to print the array` `void` `printArray(` `int` `arr[], ` `int` `n)` `{` ` ` `for` `(` `int` `i = 0; i < n; i++)` ` ` `cout << arr[i] << ` `" "` `;` `}` ` ` `// Driver code` `int` `main()` `{` ` ` `int` `arr[] = { 10, 5, 3, 9, 2 };` ` ` `int` `n = ` `sizeof` `(arr) / ` `sizeof` `(arr[0]);` ` ` `int` `k = 7;` ` ` ` ` `rearrange(arr, n, k);` ` ` ` ` `printArray(arr, n);` ` ` ` ` `return` `0;` `}` |

**Output:**

5 9 10 3 2

**Time Complexity :** O(n Log n)**Auxiliary Space : **O(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. To complete your preparation from learning a language to DS Algo and many more, please refer **Complete Interview Preparation Course****.**

In case you wish to attend live classes with industry experts, please refer **DSA Live Classes**