Sort Array and Compute Unique Intersection
Heaps, Sorting & SearchAuto-graded · 21 test casesLast updated
Tests implementing custom merge sort to sort an array and finding the sorted unique intersection of two lists.
Write a function that accepts three integer lists: nums, a, and b. The function must compute and return a list containing two results:
- The list
numssorted in non-decreasing order. - A sorted list of all unique elements that are present in both
aandbin non-decreasing order.
Constraints
- $0 \le \text{len}(nums) \le 10^4$
- $0 \le \text{len}(a), \text{len}(b) \le 10^4$
- $-10^9 \le \text{nums}[i], a[i], b[i] \le 10^9$
- The original input lists must not be modified in place.
Examples
Example 1
Input: sort_numbers_and_intersect_sets([3, 1, 2], [1, 2, 2, 3], [2, 3, 4])
Output: [[1, 2, 3], [2, 3]]
Example 2
Input: sort_numbers_and_intersect_sets([5, 5, 1, -2, 0], [7, 8], [1, 2, 3])
Output: [[-2, 0, 1, 5, 5], []]
Example 3
Input: sort_numbers_and_intersect_sets([4, 3, 2, 1], [1, 1, 1, 1], [1])
Output: [[1, 2, 3, 4], [1]]
Example 4
Input: sort_numbers_and_intersect_sets([0, -1, 0, 2, -1], [-3, -1, 0, 0], [0, 1, -1, -1])
Output: [[-1, -1, 0, 0, 2], [-1, 0]]
Hints
Hint 1
Use a standard O(N log N) sorting algorithm like merge sort to avoid Python's built-in sort functions.
Hint 2
To find the intersection, consider converting one list into a set for O(1) membership lookups.
Hint 3
Ensure duplicate elements in the intersection are deduplicated before returning the sorted common elements.
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
The problem requires performing two distinct operations:
-
Sorting an Array: We implement merge sort, a divide-and-conquer algorithm that recursively splits the list into halves, sorts each half, and merges them in linear time. Merge sort provides a guaranteed worst-case time complexity of $O(N \log N)$ and maintains stability by preferring elements from the left subarray when values are equal (
<=). -
Finding the Unique Set Intersection: We convert the list
ainto a standard hash set and check for membership againstb(or use set intersection directly). This yields the set of common elements in average $O(|a| + |b|)$ time while automatically removing duplicate elements. Finally, we pass the common elements through our custom merge sort to return them in non-decreasing order.
Complexity
-
Time Complexity:
- Sorting
nums: $O(N \log N)$ where $N$ is the length ofnums. - Computing intersection: $O(|a| + |b|)$ expected time.
- Sorting intersection: $O(K \log K)$ where $K \le \min(|a|, |b|)$ is the number of distinct common elements.
- Overall Time Complexity: $O(N \log N + |a| + |b|)$.
- Sorting
-
Space Complexity: $O(N + |a| + K)$ auxiliary space for the recursion stack, new merged sub-lists, and the hash set.
from typing import List
def _merge_sort(values: List[int]) -> List[int]:
if len(values) <= 1:
return list(values)
mid = len(values) // 2
left = _merge_sort(values[:mid])
right = _merge_sort(values[mid:])
merged = []
i = j = 0
while i < len(left) and j < len(right):
if left[i] <= right[j]:
merged.append(left[i])
i += 1
else:
merged.append(right[j])
j += 1
merged.extend(left[i:])
merged.extend(right[j:])
return merged
def sort_numbers_and_intersect_sets(nums: List[int], a: List[int], b: List[int]) -> List[List[int]]:
sorted_nums = _merge_sort(nums)
common_elements = list(set(a).intersection(b))
sorted_intersection = _merge_sort(common_elements)
return [sorted_nums, sorted_intersection]