Given two arrays arr1
and arr2
, the elements of arr2
are distinct, and all elements in arr2
are also in arr1
.
Sort the elements of arr1
such that the relative ordering of items in arr1
are the same as in arr2
. Elements that do not appear in arr2
should be placed at the end of arr1
in ascending order.
Example 1:
Input: arr1 = [2,3,1,3,2,4,6,7,9,2,19], arr2 = [2,1,4,3,9,6] Output: [2,2,2,1,4,3,3,9,6,7,19]
Example 2:
Input: arr1 = [28,6,22,8,44,17], arr2 = [22,28,8,6] Output: [22,28,8,6,17,44]
Constraints:
1 <= arr1.length, arr2.length <= 1000
0 <= arr1[i], arr2[i] <= 1000
arr2
are distinct.arr2[i]
is in arr1
.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 approach to sorting one set of numbers according to the order of another set is quite simple. We essentially go through the first set, checking for each number if it exists in the desired order and if so, collect it. Anything left at the end we just put in the output unsorted.
Here's how the algorithm would work step-by-step:
def relative_sort_array_brute_force(array_to_sort, desired_order):
sorted_array = []
remaining_elements = []
# Iterate through the input array.
for element in array_to_sort:
found_in_desired_order = False
for ordered_element in desired_order:
if element == ordered_element:
sorted_array.append(element)
found_in_desired_order = True
break
# Collect elements not in the desired order for later.
if not found_in_desired_order:
remaining_elements.append(element)
# Combine sorted elements and remaining elements.
sorted_array.extend(remaining_elements)
return sorted_array
We want to sort one list of numbers based on the order they appear in another list. We'll efficiently accomplish this by first noting the frequency of each number, then building the result based on that information.
Here's how the algorithm would work step-by-step:
def relative_sort_array(array_to_sort, ordering_array):
number_frequencies = {}
for number in array_to_sort:
number_frequencies[number] = number_frequencies.get(number, 0) + 1
sorted_array = []
# Prioritize sorting based on the order in ordering_array
for number in ordering_array:
if number in number_frequencies:
sorted_array.extend([number] * number_frequencies[number])
del number_frequencies[number]
# Collect remaining numbers not in ordering_array
remaining_numbers = []
for number, frequency in number_frequencies.items():
remaining_numbers.extend([number] * frequency)
# Sort any numbers not found in ordering_array
remaining_numbers.sort()
# Combine sorted and remaining for the final result.
sorted_array.extend(remaining_numbers)
return sorted_array
Case | How to Handle |
---|---|
arr1 or arr2 is null or empty | Return an empty array if arr1 is null or empty, and handle a null arr2 gracefully, perhaps treating it as if empty while sorting arr1's remaining elements. |
arr1 is much larger than arr2 | The solution should efficiently handle large differences in array sizes, possibly using a counting sort for arr1's remaining elements after processing based on arr2. |
arr2 contains duplicate elements | Ensure the algorithm handles duplicate elements in arr2 gracefully, potentially only processing the first occurrence of each element and ignoring subsequent duplicates. |
arr1 contains negative or zero values | Handle negative or zero values present in arr1 by adjusting the counting sort implementation or using a general-purpose sorting algorithm for the remaining elements. |
arr1 contains extremely large numbers | Consider using a HashMap or TreeMap instead of an array-based counting sort to handle potentially large values efficiently without excessive memory allocation. |
arr1 contains duplicates that are not present in arr2 | Ensure the solution correctly sorts the remaining elements from arr1 (not present in arr2) along with their respective duplicates. |
Elements in arr1 are outside the range of typical integer sizes (overflow potential during counting sort). | Use a data structure with sufficient capacity to store the counts, such as a long or a hash map to avoid integer overflow during counting sort. |
All elements in arr1 are already in arr2 | The solution should iterate through arr2 and add all corresponding elements from arr1 into the result array without any remaining elements to sort. |