Minimum Common Value

Given two integer arrays nums1 and nums2, sorted in non-decreasing order, return the minimum integer common to both arrays. If there is no common integer amongst nums1 and nums2, return -1.

Note that an integer is said to be common to nums1 and nums2 if both arrays have at least one occurrence of that integer.

Example 1
Inputnums1 = [1,2,3], nums2 = [2,4]
Output2
The smallest element common to both arrays is 2, so we return 2.
Example 2
Inputnums1 = [1,2,3,6], nums2 = [2,3,4,5]
Output2
There are two common elements in the array 2 and 3 out of which 2 is the smallest, so 2 is returned.

Constraints

  • 1 <= nums1.length, nums2.length <= 10^5
  • 1 <= nums1[i], nums2[j] <= 10^9
  • Both nums1 and nums2 are sorted in non-decreasing order.

Asked at 3 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate