Ctrl K

Value Matches Its Index

Easy
You are given an array values of distinct integers sorted in increasing order. Find the smallest index i (0-based) with values[i] == i. If there is no such index, return -1. A full O(n) scan is easy; try to find the answer faster.
Example 1
values=[-4, -1, 2, 5, 9] → 2

values[2] = 2, the first and only such index.

Example 2
values=[-3, 1, 2, 3] → 1

Indices 1, 2 and 3 all match; the smallest one is 1.

Example 3
values=[1, 3, 6] → -1

No value equals its own index.

Constraints
  • 1 ≤ len(values) < 10⁴
  • -10⁹ ≤ values[i] ≤ 10⁹
  • The numbers in values are distinct and strictly increasing
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsSlot on a Sorted Shelf EasyThe Lost Ticket EasyValues in Both Lists EasyShared Values With Repeats EasyFind in a Sorted List Easy
loading Python…
Input
values=[-4, -1, 2, 5, 9]
Expected
2

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