You are given an integer array arr
. Sort the integers in the array in ascending order by the number of 1
's in their binary representation and in case of two or more integers have the same number of 1
's you have to sort them in ascending order.
Return the array after sorting it.
Example 1:
Input: arr = [0,1,2,3,4,5,6,7,8] Output: [0,1,2,4,8,3,5,6,7] Explantion: [0] is the only integer with 0 bits. [1,2,4,8] all have 1 bit. [3,5,6] have 2 bits. [7] has 3 bits. The sorted array by bits is [0,1,2,4,8,3,5,6,7]
Example 2:
Input: arr = [1024,512,256,128,64,32,16,8,4,2,1] Output: [1,2,4,8,16,32,64,128,256,512,1024] Explantion: All integers have 1 bit in the binary representation, you should just sort them in ascending order.
Constraints:
1 <= arr.length <= 500
0 <= arr[i] <= 104
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force way to sort numbers by their 1-bits is pretty straightforward. We go through each number, count how many 1s it has, and then use that count to sort all the numbers.
Here's how the algorithm would work step-by-step:
def sort_by_one_bits_brute_force(numbers):
number_with_bit_counts = []
for number in numbers:
# Calculate the number of 1 bits
binary_representation = bin(number)[2:]
number_of_set_bits = binary_representation.count('1')
number_with_bit_counts.append((number, number_of_set_bits))
# Sort based on number of 1 bits and then numerical value
number_with_bit_counts.sort(key=lambda item: (item[1], item[0]))
# Extract original numbers from the sorted pairs
sorted_numbers = [number for number, _ in number_with_bit_counts]
return sorted_numbers
To efficiently sort numbers by the count of '1's in their binary representation, we'll first count the number of '1's for each number. Then, we'll sort the numbers based on these counts, using the original number as a tiebreaker when counts are equal.
Here's how the algorithm would work step-by-step:
def sort_by_bits(arr):
def count_set_bits(number):
count = 0
while number > 0:
number &= (number - 1)
count += 1
return count
# Create a list of tuples: (bit_count, original_number)
bit_counts_with_original_numbers = []
for number in arr:
bit_counts_with_original_numbers.append((count_set_bits(number), number))
# Sort based on bit count, then original number
bit_counts_with_original_numbers.sort(key=lambda item: (item[0], item[1]))
# Extract the sorted original numbers
sorted_array = [number for _, number in bit_counts_with_original_numbers]
return sorted_array
Case | How to Handle |
---|---|
Null or empty input array | Return an empty array or throw an IllegalArgumentException depending on the requirements. |
Input array with a single element | Return the array itself as it is already sorted. |
Input array with all elements being zero | The array should be returned as is, since all elements have zero 1-bits and are already in ascending order. |
Input array with all elements having the same number of 1 bits | The array should be sorted in ascending order based on the integer values themselves. |
Input array containing negative numbers | Handle negative numbers by considering their two's complement representation when counting 1-bits. |
Input array with large integer values close to Integer.MAX_VALUE or Integer.MIN_VALUE | Ensure the bit counting algorithm doesn't overflow or have unexpected behavior with large values. |
Input array with duplicate numbers having different numbers of 1 bits | The sorting should correctly place the duplicates based on their respective 1-bit counts and values. |
Input array with extreme values and a mix of positive, negative, and zero integers | The solution should accurately count set bits and sort based on the specifications, accounting for all number types. |