Ranked Element Selection and Common Entry Count in Sorted Arrays
Heaps, Sorting & SearchAuto-graded · 18 test casesLast updated
Find the k-th smallest element across two sorted arrays and count their unique shared elements efficiently.
You are given two non-decreasingly sorted integer arrays, arr1 and arr2, with lengths $n$ and $m$ respectively.
Write a function that computes two metrics based on these arrays:
- $k$-th Smallest Value: Determine the value of the $k$-th smallest element (1-indexed) in the combined sorted sequence of both arrays. Your partition approach should execute in $O(\log(\min(n, m)))$ time.
- Distinct Intersection Count: Calculate the total count of distinct values that appear in both
arr1andarr2.
Return a tuple containing these two results: (k_th_smallest, distinct_intersection_count).
Constraints
- $0 \le n, m \le 10^5$
- $1 \le n + m$
- $1 \le k \le n + m$
arr1andarr2are sorted in non-decreasing order.- Arrays may contain duplicate values.
Examples
Example 1
Input: solve_sorted_tasks([1, 3, 5], [2, 3, 6], 3)
Output: [3, 1]
Example 2
Input: solve_sorted_tasks([1, 4, 8], [2, 5, 9], 1)
Output: [1, 0]
Hints
Hint 1
To find the k-th smallest element in sub-linear time, binary search over the number of elements taken from the shorter array.
Hint 2
Ensure the partition indices i and j (where j = k - i) satisfy arr1[i-1] <= arr2[j] and arr2[j-1] <= arr1[i], handling boundary conditions safely.
Hint 3
For the intersection count, iterate over distinct values in the smaller array and binary-search for their presence in the larger array.
Worked solution
Hard Try it yourself first — then compare your approach, complexity and edge cases.
Approach
The problem requires solving two distinct operations over two sorted arrays:
-
Finding the $k$-th Smallest Element:
- Instead of fully merging the arrays in $O(n + m)$ time, we use a binary search partition method on the smaller array.
- Let $a$ be the shorter array of size $n$ and $b$ be the longer array of size $m$.
- We want to split the combined elements into a left partition of size $k$ and a right partition of size $(n + m - k)$.
- If we pick $i$ elements from $a$, we must pick $j = k - i$ elements from $b$.
- The partition is valid if $a[i-1] \le b[j]$ and $b[j-1] \le a[i]$.
- The valid search range for $i$ is $[\max(0, k - m), \min(k, n)]$.
- Once a valid partition $i$ is located via binary search, the $k$-th smallest element is $\max(a[i-1], b[j-1])$ (using $-\infty$ for out-of-bound indices).
-
Intersection Count of Unique Elements:
- To find shared unique elements, we iterate through distinct values of the smaller array.
- For each unique element, binary search (
bisect_left) is performed on the larger array to check for existence. - This keeps the time complexity optimal when one array is significantly smaller than the other.
Complexity
- $k$-th Smallest Value: $O(\log(\min(n, m)))$ time and $O(1)$ extra space.
- Intersection Count: $O(s \log L)$ time, where $s = \min(n, m)$ and $L = \max(n, m)$, and $O(1)$ extra space.
Python Reference Solution
from bisect import bisect_left
from typing import List, Tuple
def solve_sorted_tasks(arr1: List[int], arr2: List[int], k: int) -> Tuple[int, int]:
# Ensure array 'a' is the shorter array
a, b = arr1, arr2
if len(a) > len(b):
a, b = b, a
n, m = len(a), len(b)
lo = max(0, k - m)
hi = min(k, n)
kth = 0
while lo <= hi:
i = (lo + hi) // 2
j = k - i
if i < n and j > 0 and b[j - 1] > a[i]:
lo = i + 1
elif i > 0 and j < m and a[i - 1] > b[j]:
hi = i - 1
else:
left_a = a[i - 1] if i > 0 else float('-inf')
left_b = b[j - 1] if j > 0 else float('-inf')
kth = int(max(left_a, left_b))
break
# Find unique intersection count
if len(arr1) <= len(arr2):
smaller, larger = arr1, arr2
else:
smaller, larger = arr2, arr1
intersection_count = 0
previous = None
has_previous = False
for value in smaller:
if has_previous and value == previous:
continue
previous = value
has_previous = True
pos = bisect_left(larger, value)
if pos < len(larger) and larger[pos] == value:
intersection_count += 1
return (kth, intersection_count)