You are given K sorted integer arrays (non-decreasing). Arrays may contain duplicates within the same array and across different arrays.
Return a sorted list of unique integers that appear in any of the arrays (i.e., the set union, sorted).
Example:
A = [-100, -1, -1, 0, 5]
B = [0]
C = [-1, 0, 0]
Output → [-100, -1, 0, 5]
Constraints (typical):
- Total length
Nacross all arrays can be large. - Values fit in 32-bit signed int.
K ≥ 1.
Summary :
Use a K-way merge with a min-heap (store (value, array_id, index)), popping the smallest each time and skip if equal to the last output to deduplicate. Push the next element from that array. Time O(N log K), space O(K).
If K is tiny (e.g., 2–3), a multi-pointer linear sweep also works in O(N).
The VOprep team has long accompanied candidates through various major company OAs and VOs, including OpenAI, Google, Amazon, Citadel, SIG, providing real-time voice assistance, remote practice, and interview pacing reminders to help you stay smooth during critical moments. If you are preparing for Stripe or similar engineering-focused companies, you can check out our customized support plans—from coding interviews to system design, we offer full guidance to help you succeed.