You are given an integer array nums.
Rearrange elements of nums in non-decreasing order of their absolute value.
Return any rearranged array that satisfies this condition.
Note: The absolute value of an integer x is defined as:
x if x >= 0-x if x < 0Example 1:
Input: nums = [3,-1,-4,1,5]
Output: [-1,1,3,-4,5]
Explanation:
nums are 3, 1, 4, 1, 5 respectively.[-1, 1, 3, -4, 5]. Another possible rearrangement is [1, -1, 3, -4, 5].Example 2:
Input: nums = [-100,100]
Output: [-100,100]
Explanation:
nums are 100, 100 respectively.[-100, 100]. Another possible rearrangement is [100, -100].Constraints:
1 <= nums.length <= 100-100 <= nums[i] <= 100When 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:
Imagine you have a collection of numbers that can be either positive or negative, and your goal is to rearrange them from smallest to largest based on how far away they are from zero, regardless of their sign. The brute force approach achieves this by exhaustively comparing every possible pairing of numbers to find their distances from zero and sorting them accordingly.
Here's how the algorithm would work step-by-step:
def sort_array_by_absolute_value(input_numbers):
number_count = len(input_numbers)
# Exhaustively compare every possible pair to ensure every number is evaluated against all others
for outer_index in range(number_count):
for inner_index in range(outer_index + 1, number_count):
current_distance = abs(input_numbers[outer_index])
comparison_distance = abs(input_numbers[inner_index])
# Swap positions if the later number is closer to zero than our current reference number
if comparison_distance < current_distance:
temporary_holder = input_numbers[outer_index]
input_numbers[outer_index] = input_numbers[inner_index]
input_numbers[inner_index] = temporary_holder
# Return the fully mutated array once all exhaustive comparisons and swaps are complete
return input_numbersThis problem requires rearranging an array of integers so that their absolute values appear in non-decreasing order. In Python, this can be efficiently achieved by sorting the list with a custom key function. Using Python's stable sort ensures elements with identical absolute values are arranged validly while satisfying the example outputs.
Here's how the algorithm would work step-by-step:
def sort_array_by_absolute_value(nums: list[int]) -> list[int]:
# Create a copy of the input list to preserve the original structure
sorted_numbers = list(nums)
# Sort the numbers in non-decreasing order using their absolute value as the key
sorted_numbers.sort(key=abs)
# Return the newly sorted list of integers
return sorted_numbers
| Case | How to Handle |
|---|---|
| Empty or null array input | Return the empty or null array immediately since no sorting is required. |
| Array with a single element | Return the single-element array as it is already trivially sorted. |
| Array with mixed positive and negative numbers having the same absolute value | Use a stable sorting algorithm to preserve the original relative order of identical absolute values. |
| Array containing multiple zeros | Treat all zeros with absolute value zero and preserve their relative positions via stable sort. |
| Array with extreme boundary values like minimum and maximum integer limits | Compute absolute values using wider integer types to prevent integer overflow bugs. |
| Extremely large arrays with millions of elements | Utilize an efficient comparison-based sorting algorithm with O(n log n) time complexity. |
| Array with all identical elements | Perform the sort without altering the original sequence due to the stability property. |
| Pre-sorted or reverse-sorted arrays | Ensure the sorting algorithm handles these distributions efficiently without worst-case degradation. |