Given an integer array of even length arr, return true if it is possible to reorder arr such that arr[2 * i + 1] = 2 * arr[2 * i] for every 0 <= i < len(arr) / 2, or false otherwise.
Example 1:
Input: arr = [3,1,3,6] Output: false
Example 2:
Input: arr = [2,1,2,6] Output: false
Example 3:
Input: arr = [4,-2,2,-4] Output: true Explanation: We can take two groups, [-2,-4] and [2,4] to form [-2,-4,2,4] or [2,4,-2,-4].
Constraints:
2 <= arr.length <= 3 * 104arr.length is even.-105 <= arr[i] <= 105When 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 approach for this problem is all about checking every possible pairing. We'll go through the numbers one by one and try to find a matching double for each. If we can find a match for every number, then we're good to go!
Here's how the algorithm would work step-by-step:
def can_rearrange_to_doubled_pairs_brute_force(array):
array_length = len(array)
used_indices = [False] * array_length
for i in range(array_length):
# Skip already used numbers
if used_indices[i]:
continue
number = array[i]
double = 2 * number
found_match = False
for j in range(array_length):
# Find the double of the current number.
if not used_indices[j] and array[j] == double:
used_indices[i] = True
used_indices[j] = True
found_match = True
break
# If we couldn't find a match, we can't form doubled pairs
if not found_match:
return False
return TrueThe goal is to figure out if you can pair up all the numbers in a list where each pair has one number that is exactly double the other. The best way to do this is to efficiently count how many of each number you have, and then cleverly use these counts to form pairs, starting with the smallest numbers.
Here's how the algorithm would work step-by-step:
def can_reorder_doubled(array):
number_counts = {}
for number in array:
number_counts[number] = number_counts.get(number, 0) + 1
# Sort numbers to process smaller numbers first
sorted_numbers = sorted(number_counts.keys())
for number in sorted_numbers:
if number_counts[number] == 0:
continue
double_number = 2 * number
# Check if we have enough of the doubled number to form pairs
if double_number not in number_counts or number_counts[number] > number_counts[double_number]:
return False
number_counts[double_number] -= number_counts[number]
return True| Case | How to Handle |
|---|---|
| Null or undefined input array | Return false or throw an IllegalArgumentException as null input is invalid |
| Empty input array | Return true, as an empty array vacuously satisfies the condition |
| Array with odd number of elements | Return false immediately as it's impossible to form doubled pairs |
| Array contains negative numbers | The solution should handle negative numbers by considering both x and 2x when x is negative |
| Array contains zero(s) | Handle zeros carefully, ensuring each zero is paired with another zero |
| Array with a large number of identical values | Ensure the counting mechanism (e.g., HashMap) can accurately track occurrences without integer overflow |
| Integer overflow when calculating 2 * A[i] | Use a larger data type (e.g., long in Java) or check for overflow before multiplication |
| No valid doubled pairs exist | Return false when all elements have been processed and no valid arrangement is found |