Rearrange Array Elements Problem Statement

Given an array A containing 'N' integers and an integer m, rearrange the elements of the array such that the differences between the array elements and m are sorted in order.

If multiple elements have the same difference from m, maintain their order from the original array.

Example:

Input:
A = [3, 5, 7, 9, 2, 6], m = 5
Output:
[5, 6, 3, 7, 2, 9]
Explanation:

The differences are: [2, 0, 2, 4, 3, 1]. Reordered array: [5, 6, 3, 7, 2, 9].

Input:

The first line contains a single integer ‘T’ denoting the number of test cases.
The first line of each test case contains two integers, ‘N’ - the number of elements in the array, and ‘m’.
The second line of each test case contains ‘N’ space-separated integers comprising ‘A’.

Output:

For each test case, print space-separated integers representing the elements of the re-arranged array.
Print the output for each test case on a separate line.

Constraints:

  • 1 <= T <= 10
  • 1 <= N <= 105
  • 1 <= m <= 105
Note:
You do not need to print anything as it has already been handled. Implement the function to solve the problem.
AnswerBot
4mo

Rearrange array elements based on their differences from a given integer.

  • Calculate the differences between each element and the given integer.

  • Sort the elements based on their differences while maintai...read more

Help your peers!
Select
Add answer anonymously...

Info Edge Software Developer interview questions & answers

A Software Developer was asked 8mo agoQ. Given the root node of a binary search tree (BST) and a value to be inserted int...read more
A Software Developer was asked 8mo agoQ. Given an integer array nums, find the subarray with the largest sum, and return ...read more
A Software Developer was asked Q. Anagram Pairs Problem Statement Determine if two given strings are anagram pairs...read more

Popular interview questions of Software Developer

A Software Developer was asked 9mo agoQ1. Given the root node of a binary search tree (BST) and a value to be inserted int...read more
A Software Developer was asked 9mo agoQ2. Given an integer array nums, find the subarray with the largest sum, and return ...read more
A Software Developer was asked Q3. Anagram Pairs Problem Statement Determine if two given strings are anagram pairs...read more
Info Edge Software Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+

Reviews

10L+

Interviews

4 Cr+

Salaries

1.5 Cr+

Users

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2025 Info Edge (India) Ltd.

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits