Alice had a 0-indexed array arr consisting of n positive integers. She chose an arbitrary positive integer k and created two new 0-indexed integer arrays lower and higher in the following manner:
lower[i] = arr[i] - k, for every index i where 0 <= i < nhigher[i] = arr[i] + k, for every index i where 0 <= i < nUnfortunately, Alice lost all three arrays. However, she remembers the integers that were present in the arrays lower and higher, but not the array each integer belonged to. Help Alice and recover the original array.
Given an array nums consisting of 2n integers, where exactly n of the integers were present in lower and the remaining in higher, return the original array arr. In case the answer is not unique, return any valid array.
Note: The test cases are generated such that there exists at least one valid array arr.
Example 1:
Input: nums = [2,10,6,4,8,12] Output: [3,7,11] Explanation: If arr = [3,7,11] and k = 1, we get lower = [2,6,10] and higher = [4,8,12]. Combining lower and higher gives us [2,6,10,4,8,12], which is a permutation of nums. Another valid possibility is that arr = [5,7,9] and k = 3. In that case, lower = [2,4,6] and higher = [8,10,12].
Example 2:
Input: nums = [1,1,3,3] Output: [2,2] Explanation: If arr = [2,2] and k = 1, we get lower = [1,1] and higher = [3,3]. Combining lower and higher gives us [1,1,3,3], which is equal to nums. Note that arr cannot be [1,3] because in that case, the only possible way to obtain [1,1,3,3] is with k = 0. This is invalid since k must be positive.
Example 3:
Input: nums = [5,435] Output: [220] Explanation: The only possible combination is arr = [220] and k = 215. Using them, we get lower = [5] and higher = [435].
Constraints:
2 * n == nums.length1 <= n <= 10001 <= nums[i] <= 109arr.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 goal is to reconstruct a hidden number sequence from a scrambled list of numbers. The brute force method involves trying every possible secret number that could have been used to create that scrambled list.
Here's how the algorithm would work step-by-step:
def recover_the_original_array_brute_force(scrambled_list):
list_length = len(scrambled_list)
for assumed_secret_number_index in range(list_length):
assumed_secret_number = scrambled_list[assumed_secret_number_index]
original_array = []
temp_scrambled_list = scrambled_list[:] # Create a copy to modify
original_array.append(assumed_secret_number)
temp_scrambled_list.remove(assumed_secret_number)
# Try to reconstruct the original array
while temp_scrambled_list:
found_match = False
for potential_match in temp_scrambled_list[:]:
# Check if the difference can be an element.
difference = abs(original_array[-1] - potential_match)
if difference in original_array:
pass
else:
original_array.append(potential_match)
temp_scrambled_list.remove(potential_match)
found_match = True
break
if not found_match:
original_array = []
break
# Check if the rebuilt array could generate the given scrambled list
if len(original_array) > 0:
reconstructed_scrambled_list = []
for first_number_index in range(len(original_array)):
for second_number_index in range(first_number_index, len(original_array)):
reconstructed_scrambled_list.append(abs(original_array[first_number_index] - original_array[second_number_index]))
reconstructed_scrambled_list.sort()
temp_list = scrambled_list[:]
temp_list.sort()
# Account for needing to sort the lists to compare them.
if reconstructed_scrambled_list == temp_list:
return original_array
# Must return an empty list if no match is found
return []We're given a mixed-up list of numbers, and our goal is to figure out the original list that was used to create it. The key insight is to focus on the difference between the original numbers and the mixed-up list to deduce a 'k' value that helps us reconstruct the original list by reversing the mixing process.
Here's how the algorithm would work step-by-step:
def recover_the_original_array(mixed_array):
mixed_array.sort()
number_of_elements = len(mixed_array)
original_array = []
for i in range(1, number_of_elements // 2 + 1):
potential_k = (mixed_array[i] - mixed_array[0])
if potential_k == 0 or potential_k % 2 != 0:
continue
potential_k //= 2
temp_mixed_array = mixed_array[:]
temp_original_array = []
index = 0
while index < len(temp_mixed_array):
first_number = temp_mixed_array[index]
second_number = first_number + 2 * potential_k
# Find the second number in the remaining mixed array.
try:
second_number_index = temp_mixed_array.index(second_number)
# Append the first number to the original array.
temp_original_array.append(first_number)
# Remove both numbers.
del temp_mixed_array[second_number_index]
del temp_mixed_array[index]
index = 0 # Reset index
except ValueError:
index += 1
# Check if we were able to reconstruct the original array.
if len(temp_mixed_array) == 0:
original_array = temp_original_array
return original_array
return original_array| Case | How to Handle |
|---|---|
| Null or empty input array | Return an empty array to indicate no original array can be recovered from an invalid input |
| Input array with only two elements, no valid solution | Return an empty array if no valid 'k' can be found that satisfies the problem's condition |
| Input array with all elements being the same number, but no valid 'k' | Return an empty array if all differences between pairs of numbers do not yield a single, consistent 'k'. |
| Integer overflow when calculating potential 'k' values | Use long data type for intermediate calculations of differences to avoid overflow and then cast back to int if necessary |
| Input array containing negative numbers | The solution should handle negative numbers correctly as the difference can also be negative, update counter accordingly |
| Large input array with potential performance bottleneck | Utilize optimized data structures like HashMaps for efficient lookups of element counts instead of linear searches |
| Multiple valid 'k' values exist, resulting in different original arrays | The solution should choose the smallest possible 'k' to ensure a deterministic output as it does not mention which k to use |
| Input array with zero values and zero difference | Handle the case where elements are the same and equal to zero carefully as it involves checking if sufficient number of elements are equal |