Skip to the code
cf.
+25 XP

#162 Slide in Its Own Slot

medium

A presentation tool gives each slide a position code. The codes are in codes, sorted ascending, all different, and they can be negative. A slide is "pinned" when its code equals its index in the list: codes[i] === i.

Write firstPinned(codes) that returns the smallest index i with codes[i] === i, or -1 if no slide is pinned.

Aim for O(log n).

Example 1

Input: codes = [-10,-5,0,3,7]
Output: 3

codes[3] is 3.

Example 2

Input: codes = [-10,-5,3,4,7,9]
Output: -1

No code equals its index.

Constraints

  • 1 <= codes.length <= 100000
  • codes is sorted ascending with no repeats.
  • -10^9 <= codes[i] <= 10^9
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).