Minimum Common Value (LeetCode 2540): Solution and Explanation

Easy
Array
Hash Table
Two Pointers
Binary Search

Given two sorted integer arrays, return the smallest value that appears in both, or -1 if they share nothing.

Intuition

Look at the front of each array. If the two values are equal, nothing smaller can be common, because both arrays start at or above that value. That value is the answer.

If they differ, say nums1[i] < nums2[j], then nums1[i] cannot appear in nums2 at j or later, since everything there is at least nums2[j]. And it cannot appear before j either, because those values were already passed for being smaller than something in nums1. So nums1[i] is ruled out and i can move on.

Each step discards one value for good, so the walk ends after at most n + m moves. It is the merge step of merge sort, stopped at the first tie.

Approach: Two pointers

  1. Set i = 0 in nums1 and j = 0 in nums2.
  2. While both pointers are inside their arrays, compare nums1[i] and nums2[j].
  3. If they are equal, return that value.
  4. If nums1[i] is smaller, increment i; otherwise increment j.
  5. If the loop ends, one array is exhausted with no match, so return -1.

Step-by-step example

nums1 = [1,2,3], nums2 = [2,4]
  1. i = 0, j = 0: compare 1 with 2. 1 is smaller, so it cannot be in nums2. i = 1.
  2. i = 1, j = 0: compare 2 with 2. They are equal.
  3. Return 2.

Code: Python, Java, C++

1class Solution:
2    def getCommon(self, nums1: List[int], nums2: List[int]) -> int:
3        i, j = 0, 0
4        while i < len(nums1) and j < len(nums2):
5            if nums1[i] == nums2[j]:
6                return nums1[i]
7            if nums1[i] < nums2[j]:
8                i += 1
9            else:
10                j += 1
11        return -1
Press play on the visualizer and the running line lights up here.

Time and space complexity

Work as the input growsinput size n →steps
Two pointers O(n + m)
Hash set O(n + m)
Binary search each value O(n log m)
Time

O(n + m)

Every comparison either returns or moves one pointer forward, and the pointers can move at most n + m times in total.

Space

O(1)

Only the two indices are stored.

Other approaches

Binary search each value (time O(n log m), space O(1))

For each value of the shorter array, in order, binary search for it in the longer array and return the first hit. Good when one array is much shorter than the other.

Hash set (time O(n + m), space O(m))

Put nums2 into a set, then scan nums1 from the left and return the first value in the set. Works even for unsorted input but uses extra memory.

Common mistakes

  • Advancing both pointers when the values differ. Only the pointer at the smaller value is safe to move; the larger value may still match.
  • Continuing after a match to look for something smaller. The first match is already the minimum, because the arrays are scanned in increasing order.
  • Using a set and returning any common value instead of the smallest. The scan order matters.

Frequently asked questions

Do duplicates break the two pointer walk?

No. A repeated value simply takes several steps to pass, and if it is common the first comparison that sees it on both sides returns it.

When is binary search better than two pointers?

When one array is tiny and the other is huge. Searching each of the n small-array values in the large array costs O(n log m), which beats O(n + m) if n is much smaller than m.

Why is the first equal pair guaranteed to be the minimum?

Every value passed before it was smaller than a value in the other array that had not appeared yet, so it was not common. The pointers only move toward larger values.