-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSorting2.cpp
More file actions
38 lines (37 loc) · 794 Bytes
/
Copy pathSorting2.cpp
File metadata and controls
38 lines (37 loc) · 794 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include<iostream>
#include<map>
using namespace std;
void rearrange(int arr[], int n, int x)
{
multimap<int, int> m;
multimap<int ,int >:: iterator it;
for (int i = 0 ; i < n; i++)
m.insert(make_pair(abs(x-arr[i]),arr[i]));
int i = 0;
for (it = m.begin(); it != m.end(); it++)
arr[i++] = (*it).second ;
}
void printArray(int arr[] , int n)
{
for (int i = 0 ; i < n; i++)
cout << arr[i] << " ";
}
int main()
{
int t;
cin>>t;
while (t--)
{
int n,X;
cin>>n>>X;
int arr[n];
for (int i = 0; i < n; i++)
{
cin>>arr[i];
}
rearrange(arr, n, X);
printArray(arr, n);
cout<<endl;
}
return 0;
}