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).
Input: codes = [-10,-5,0,3,7] Output: 3
codes[3] is 3.
Input: codes = [-10,-5,3,4,7,9] Output: -1
No code equals its index.
1 <= codes.length <= 100000codes is sorted ascending with no repeats.-10^9 <= codes[i] <= 10^9Run 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).
I'm here to help with #162 Slide in Its Own Slot. I give hints, never the solution, so you get to solve it yourself. Ask for a hint, select code to ask about it, or press Ask Tuto why on a failing test.
Your first 3 answers need no account. for free to keep asking.