The Fixed Point problem on Leetcode asks you to find an index i
in a sorted array of distinct integers arr
such that arr[i] == i
. If such an index exists, return it. If there are multiple possible indices, return any one of them. If no such index exists, return -1
.
arr
is a sorted array of distinct integers (may include negative numbers).arr[i] == i
at a single index).
The simplest way to solve this problem is to check every index i
and see if arr[i] == i
. This is a brute-force approach and works, but it can be slow if the array is large.
However, because the array is sorted and has distinct elements, we can do better. If we think about how the values in the array relate to their indices, we can use a binary search approach, similar to searching for a target value in a sorted array.
The key insight is that if arr[mid] < mid
, then all values to the left of mid
will also be less than their indices (because the array is sorted and strictly increasing). Similarly, if arr[mid] > mid
, all values to the right will be greater than their indices. This allows us to eliminate half the search space at each step.
We'll use a binary search algorithm to efficiently find a fixed point. Here is how we break down the solution:
left
and right
, at the start and end of the array.left <= right
:
mid
index: mid = (left + right) // 2
.arr[mid] == mid
, we've found a fixed point and return mid
.arr[mid] < mid
, move the left
pointer to mid + 1
(search right half).arr[mid] > mid
, move the right
pointer to mid - 1
(search left half).-1
.This approach is much faster than brute-force because it halves the search space at each step, leveraging the sorted, distinct property of the array.
Let's walk through an example with arr = [-10, -5, 0, 3, 7]
:
left = 0
, right = 4
mid = (0 + 4) // 2 = 2
. arr[2] = 0
, but 0 != 2
.arr[2] < 2
, search right half: left = 3
, right = 4
mid = (3 + 4) // 2 = 3
. arr[3] = 3
, and 3 == 3
.3
.The algorithm stops as soon as it finds a fixed point.
Brute-force approach:
The optimized approach is much faster for large arrays due to the logarithmic time complexity.
The Fixed Point problem leverages the sorted and distinct nature of the input array to efficiently locate an index where arr[i] == i
. By applying a binary search instead of a brute-force scan, we reduce time complexity from linear to logarithmic. The main insight is recognizing how the relationship between arr[mid]
and mid
allows us to eliminate half the possibilities at each step, making the solution both elegant and efficient.
class Solution:
def fixedPoint(self, arr):
left, right = 0, len(arr) - 1
while left <= right:
mid = (left + right) // 2
if arr[mid] == mid:
return mid
elif arr[mid] < mid:
left = mid + 1
else:
right = mid - 1
return -1
class Solution {
public:
int fixedPoint(vector<int>& arr) {
int left = 0, right = arr.size() - 1;
while (left <= right) {
int mid = left + (right - left) / 2;
if (arr[mid] == mid) {
return mid;
} else if (arr[mid] < mid) {
left = mid + 1;
} else {
right = mid - 1;
}
}
return -1;
}
};
class Solution {
public int fixedPoint(int[] arr) {
int left = 0, right = arr.length - 1;
while (left <= right) {
int mid = left + (right - left) / 2;
if (arr[mid] == mid) {
return mid;
} else if (arr[mid] < mid) {
left = mid + 1;
} else {
right = mid - 1;
}
}
return -1;
}
}
var fixedPoint = function(arr) {
let left = 0, right = arr.length - 1;
while (left <= right) {
let mid = Math.floor((left + right) / 2);
if (arr[mid] === mid) {
return mid;
} else if (arr[mid] < mid) {
left = mid + 1;
} else {
right = mid - 1;
}
}
return -1;
};