How to find the kth smallest element in the union of two sorted arrays?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Finding the kth smallest element in the union of two sorted arrays is a classic problem in computer science, frequently encountered in algorithms and data structures. This problem is not only fundamental but also forms the basis for more complex algorithms, such as those for merging data or finding medians. This article will guide you through understanding and solving this problem using efficient methods.
Problem Description
Given two sorted arrays A and B of size m and n respectively, the goal is to find the kth smallest element in their union. Importantly, k is a 1-based index, meaning that when k = 1, you want the smallest element, and when k = m + n, you want the largest.
Example
Input:
- Array A:
[1, 3, 5] - Array B:
[2, 4, 6] - k = 4
Output:
- The 4th smallest element in the union of A and B is
4.
Naive Approach
The simplest way to address this problem is to merge the two arrays into a single sorted array and then find the kth element. However, this approach runs in time due to the merge process, which is not optimal.
Naive Algorithm
- Merge arrays A and B.
- Sort the resulting array.
- Return the element at index
k-1.
Although functional, this approach is inefficient for large datasets.
Efficient Approach Using Binary Search
To optimize, a more efficient method involves the use of binary search, which reduces the time complexity to . The algorithm leverages the properties of two sorted arrays to divide and conquer.
Explanation of Efficient Approach
- Assume
m <= n. If not, swap A and B to ensure this condition. - Perform a binary search on the smaller array A:
- Set
low = 0andhigh = m. - Calculate
mid = (low + high) / 2. - Let
j = k - i.
- Check the partitions:
A[min-1] <= B[j]andB[j-1] <= A[i]
- Adjust your binary search based on the conditions:
- If
A[i-1] > B[j], movehightomid - 1. - If
B[j-1] > A[i], movelowtomid + 1.
- Once the correct partition is found, determine the element:
- If
i = 0, chooseB[k-1]. - If
j = 0, chooseA[k-1]. - Otherwise, the element is
max(A[i-1], B[j-1]).
Pseudocode
Analysis
Let's analyze the time complexity of this efficient algorithm:
- Time Complexity: , where
mandnare the lengths of the two arrays. - Space Complexity: , as no extra space is used apart from a few variables.
Edge Cases
It's important to consider the following edge cases:
- One Array Empty: If one array is empty, the
kth element is obviously in the non-empty array. - k Out of Bounds: If
kis greater thanm+nor less than 1, the problem is ill-posed. - Equal Elements: The algorithm handles equal elements naturally due to the conditions checked.
Conclusion
Finding the kth smallest element in two sorted arrays is efficiently achievable through binary search, providing significant improvements over the naive approach. This technique is crucial for optimizing algorithms in large-scale applications and is widely applicable in other problems such as finding medians and merging sorted arrays.
Key Points Summary
| Approach | Time Complexity | Space Complexity | Notes |
| Naive Approach | Simple but inefficient | ||
| Binary Search | Optimal, uses binary search |
Understanding and implementing the efficient algorithm can significantly enhance performance, especially in scenarios dealing with large datasets or requiring repeated queries.

