Rearrange array elements.
Given an array ‘A’ having ‘N’ integers and an integer ‘m’. You need to rearrange the array elements such that after re-arrangement difference of array elements with ‘m’ should be in a sorted fashion.
If the difference between any two array elements and ‘m’ is the same, then the element which comes first in the given array will be placed first in the re-arranged array.
For Example
If we have A = [3, 5, 7, 9, 2, 6] and m = 5.
Difference of array elements with m : [2, 0, 2, 4, 3, 1].
Array after rearrangement : [5, 6, 3, 7, 2, 9].
Input Format
The first line contains a single integer ‘T’ denoting the number of test cases.
The first line of each test contains two integers, ‘N’ - number of elements in the array and ‘m’
The second line of each test case contains ‘N’ space-separated integers that make up ‘A’.
Output Format
For each test case, you need to print space-separated integers denoting the elements of the re-arranged array.
Print the output of each test case in a separated line.
Note
You do not need to print anything. It has already been taken care of. Just implement the given function.
Constraints
1 <= T <= 10
1 <= N <= 10^5
1 <= m <= 10^5
Where ‘T’ is the number of test cases, ‘N’ is the length of array ‘A’, and ‘m’ is the value given as described in the problem statement.
Be the first one to answer
Add answer anonymously...
Top Info Edge Software Developer interview questions & answers
Popular interview questions of Software Developer
Top HR questions asked in Info Edge Software Developer
Stay ahead in your career. Get AmbitionBox app
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+
Reviews
4 L+
Interviews
4 Cr+
Salaries
1 Cr+
Users/Month
Contribute to help millions
Get AmbitionBox app