Problem Description
Given two sorted integer arrays, nums1 and nums2, find and return the minimum common integer between them. If there is no common integer, return -1.
Key Insights
- Both arrays are sorted in non-decreasing order.
- A two-pointer approach can efficiently traverse the arrays simultaneously.
- Alternatively, binary search or hash table based methods can also be applied.
- The goal is to find the smallest integer that appears in both arrays.
Space and Time Complexity
Time Complexity: O(n + m) where n and m are the lengths of nums1 and nums2. Space Complexity: O(1) (ignoring the input storage).
Solution
We use the two-pointer technique to traverse both sorted arrays. Starting with pointers at the beginning of each array, we compare the pointed elements:
- If they are equal, we have found the smallest common value.
- If the element in nums1 is smaller, we move the pointer in nums1 ahead.
- Otherwise, we move the pointer in nums2 ahead. This approach works because the arrays are sorted, ensuring that once we find a common value it will be the smallest one possible.