# Sort an array according to absolute difference with given value using Functors

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; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

5 9 10 3 2

**Time Complexity :** O(n Log n)

**Auxiliary Space : **O(1)

## Recommended Posts:

- Sort an array according to absolute difference with given value
- Python | Sort an array according to absolute difference
- Sort an array according to absolute difference with a given value "using constant extra space"
- Maximum absolute difference in an array
- Triplets in array with absolute difference less than k
- Maximum sum of absolute difference of an array
- Absolute Difference of even and odd indexed elements in an Array
- k-th smallest absolute difference of two elements in an array
- Sum of minimum absolute difference of each array element
- Count pairs in an array such that the absolute difference between them is ≥ K
- Absolute Difference of all pairwise consecutive elements in an array
- Check if array can be divided into two sub-arrays such that their absolute difference is K
- Minimum absolute difference of adjacent elements in a circular array
- Count maximum elements of an array whose absolute difference does not exceed K
- Pair with minimum absolute difference | BST

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.