Talentd
FleetCode

2540. Minimum Common Value

Easy60.9% acceptance3 companies

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:

Input: nums1 = [1,2,3], nums2 = [2,4]
Output: 2
Explanation: The smallest element common to both arrays is 2, so we return 2.

Example 2:

Input: nums1 = [1,2,3,6], nums2 = [2,3,4,5]
Output: 2
Explanation: 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 <= 105
  • 1 <= nums1[i], nums2[j] <= 109
  • Both nums1 and nums2 are sorted in non-decreasing order.

Asked at 3 companies

Hints open one at a time

Hint 1
Try to use a set.
Hint 2
Otherwise, try to use a two-pointer approach.
9:48
Still stuck?Read 4 approaches, or watch a walkthrough.
Open solution
Loading editor…
1class Solution:
2 def getCommon(self, nums1: List[int], nums2: List[int]) -> int:
3
Starter codePython 3Spaces: 4
Examples
Input
[1,2,3]
[2,4]
Input
[1,2,3,6]
[2,3,4,5]
Your code is saved on this device.Solution