Minimum Common Value

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

30:00

Minimum Common Value
easy
Free written solution
Topics
Companies

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

Example 1:
Input: {"nums1":[1,2,3],"nums2":[2,4]}
Output: 2
Constraints:
  • 1≤nums1.length,nums2.length≤1051 \leq \text{nums1.length}, \text{nums2.length} \leq 10^5

  • 1≤nums1[i],nums2[j]≤1091 \leq \text{nums1}[i], \text{nums2}[j] \leq 10^9

  • Both nums1 and nums2 are sorted in non-decreasing order.

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"nums1":[1,2,3],"nums2":[2,4]}

Both arrays are sorted. Put i at the start of nums1 and j at the start of nums2; the first value they agree on is the smallest common value.

Both arrays on one value line

nums1
nums2
i=0: 1
1
2
3
2
4
j=0: 2

■ pointer

■ passed

shaded = below both pointers, ruled out

nums1[0] = 1

?

nums2[0] = 2

the pointer holding the smaller value moves

Variables
VariableValue
i0
j0
DepthFunction Call
Stack empty
0/5