Ctrl K

First Shared Value

Easy
You are given two integer lists left and right, each sorted in non-decreasing order. Values may repeat within a list. Return the smallest number that appears in both lists at least once. If there is no such number, return -1. Try to do it without extra memory by using the fact that both lists are already sorted.
Example 1
left=[2, 5, 5, 9, 14], right=[1, 5, 9, 20] → 5

The shared numbers are 5 and 9; the smaller one is 5.

Example 2
left=[3, 6, 8], right=[4, 7, 10] → -1

No number appears in both lists, so the answer is -1.

Example 3
left=[1], right=[1, 1, 2] → 1
Constraints
  • 1 ≤ len(left), len(right) ≤ 10⁵
  • 1 ≤ left[i], right[j] ≤ 10⁹
  • Both lists are sorted in non-decreasing order
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsValues in Both Lists EasyShared Values With Repeats EasyTwice as Much EasyPairs With a Fixed Gap MediumCleanup by Unequal Pairs Medium
loading Python…
Input
left=[2, 5, 5, 9, 14], right=[1, 5, 9, 20]
Expected
5

Run checks the first 3 tests in your browser. Submit checks all 103 tests on the server, hidden ones included.