Examples:

1.

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

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

2.

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

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

Explanation:

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.


#include <bits/stdc++.h>
using namespace std;

// Function to sort an array according absolute
// difference with x.
void arrange(int arr[], int n, int x)
{
    multimap<int, int> mlp;
    multimap<int, int>::iterator it;
    // Store values in a map with the difference
    // with X as key
    for (int i = 0; i < n; i++)
        mlp.insert(make_pair(abs(x - arr[i]), arr[i]));

    // Update the values of array
    int i = 0;
    for (it = mlp.begin(); it != mlp.end(); it++)
        arr[i++] = (*it).second;
}

// Function to print the array
void printArray(int arr[], int n)
{
    for (int i = 0; i < n; i++)
        cout << arr[i] << " ";
}

int main()
{
    int arr[] = {10, 5, 3, 9, 2};
    int n = sizeof(arr) / sizeof(arr[0]);
    int x = 7;
    rearrange(arr, n, x);
    printArray(arr, n);
    return 0;
}

Output:

5 9 10 3 2