Fixed Point - Problem
Given an array of distinct integers arr, where arr is sorted in ascending order, return the smallest index i that satisfies arr[i] == i.
If there is no such index, return -1.
A fixed point in an array is an index where the value equals the index itself.
Input & Output
Example 1 — No Fixed Point
$
Input:
arr = [-1,0,3,5,9,12]
›
Output:
-1
💡 Note:
Check each index: arr[0]=-1≠0, arr[1]=0≠1, arr[2]=3≠2, arr[3]=5≠3, arr[4]=9≠4, arr[5]=12≠5. No fixed point exists.
Example 2 — Fixed Point Found
$
Input:
arr = [0,2,3,4,5]
›
Output:
0
💡 Note:
arr[0] = 0, which equals the index 0. This is the smallest (and only) fixed point.
Example 3 — Multiple Fixed Points
$
Input:
arr = [-1,1,3,4]
›
Output:
1
💡 Note:
arr[1] = 1, which equals the index 1. This is the only fixed point.
Constraints
- 1 ≤ arr.length ≤ 104
- -109 ≤ arr[i] ≤ 109
- arr is sorted in ascending order
- All elements in arr are distinct
Visualization
Tap to expand
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code