Minimum Common Value (LeetCode 2540): Solution and Explanation
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
- Set
i = 0innums1andj = 0innums2. - While both pointers are inside their arrays, compare
nums1[i]andnums2[j]. - If they are equal, return that value.
- If
nums1[i]is smaller, incrementi; otherwise incrementj. - 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]
- i = 0, j = 0: compare 1 with 2. 1 is smaller, so it cannot be in nums2. i = 1.
- i = 1, j = 0: compare 2 with 2. They are equal.
- 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 -11class Solution {
2 public int getCommon(int[] nums1, int[] nums2) {
3 int i = 0, j = 0;
4 while (i < nums1.length && j < nums2.length) {
5 if (nums1[i] == nums2[j]) {
6 return nums1[i];
7 }
8 if (nums1[i] < nums2[j]) {
9 i++;
10 } else {
11 j++;
12 }
13 }
14 return -1;
15 }
16}1class Solution {
2public:
3 int getCommon(vector<int>& nums1, vector<int>& nums2) {
4 int i = 0, j = 0;
5 while (i < nums1.size() && j < nums2.size()) {
6 if (nums1[i] == nums2[j]) {
7 return nums1[i];
8 }
9 if (nums1[i] < nums2[j]) {
10 i++;
11 } else {
12 j++;
13 }
14 }
15 return -1;
16 }
17};Time and space complexity
O(n + m)
Every comparison either returns or moves one pointer forward, and the pointers can move at most n + m times in total.
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.