Hamming Sort
Practice
1.9 (15 votes)
Sorting
Basic programming
Bit manipulation
Merge sort
Basics of bit manipulation
Algorithms
Bit manipulation
Bitmask
Problem
39% Success 1447 Attempts 20 Points 2s Time Limit 256MB Memory 1024 KB Max Code

Given an array of integers, denoted as \(A\), and an integer \(K\), your task is to implement a sorting algorithm. The goal is to arrange the elements in \(A\) based on their Hamming Distance from the integer \(K\).

The Hamming Distance is defined as the count of differing bits in the binary representations of two integers.

The sorting should be done in ascending order of Hamming distance, and in case of a tie in Hamming Distances, the elements should be sorted in ascending numerical order.

Input Format:

Note: This is the input format that you must use to provide custom input (available above the Compile and Test button).

The first line contains \(T\) denoting the number of test cases. \(T\) also specifies the number of times you have to run the solve function on a different set of inputs. For each test case:

  • The first line contains the integer \(N\) and \(K\).
  • The second line contains the array \(A\) of length \(N\)

Output Format: For each test case, print the answer in a new line. 

Constraints:

\(1 \leq T \leq 10 \\ 1 \leq N \leq 10^5 \\ 1 \leq K \leq 10^5 \\ 1 \leq A[i] \leq 10^5\)

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:20
11 votes
Tags:
Bit ManipulationBasic ProgrammingBasics of Bit ManipulationC++
Points:20
19 votes
Tags:
Basic ProgrammingBit ManipulationBasics of Bit Manipulation
Points:20
71 votes
Tags:
Basic ProgrammingBit ManipulationBit manipulationMath