#1064
Easy Algorithms Fixed point
Array Binary Search
63.8% acceptance
Mar 31, 2026
442
67
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.
Solution
Rust
Time O(n log n)
Space O(1)
impl Solution {
pub fn fixed_point(arr: Vec<i32>) -> i32 {
let mut lo = 0usize;
let mut hi = arr.len();
while lo < hi {
let mid = lo + (hi - lo) / 2;
if arr[mid] - mid as i32 >= 0 {
hi = mid;
} else {
lo = mid + 1;
}
}
if lo < arr.len() && arr[lo] == lo as i32 { lo as i32 } else { -1 }
}
}