Find a Fixed Point (Value equal to index) in a given array
Pattern: Binary search on sorted arrays
In a sorted array of different whole numbers, arr[i] - i never goes down. So the fixed points, where arr[i] equals i, form one run, and binary search for the first i with arr[i] ≥ i finds the smallest one.
No verified practice link yet. The guide below states the problem in full, so you can still solve it here.
The problem
You are given a sorted array of different whole numbers. A fixed point is an index i with arr[i] == i, counting
from 0. Return the smallest fixed point, or -1 if there is none. For example, [-10, -5, 0, 3, 7] gives 3,
[0, 2, 5, 8, 17] gives 0, and [-10, -5, 3, 4, 7, 9] gives -1.
This problem is from Love Babbar's 450 sheet, and this guide has no verified link for it yet. The function name
fixedPoint and the rule "return the smallest one" are this guide's own. The method relies on the values being
different.
Think first
- Moving one index to the right, by how much does the value grow at least? By how much does the index grow?
- What does that say about
arr[i] - ialong the array? - If
arr[mid] < mid, can any index beforemidbe a fixed point?
Hints
Brute force, and why it fails
Check every index: O(n). The never-decreasing arr[i] - i lets binary search do it in O(log n).
The key insight
Subtracting the index from each value turns the array into a sequence that never goes down. A fixed point is a zero in that sequence, and the first zero is found like a lower bound.
Dry run
| answer | -1 |
|---|
arr = [-10, -5, 0, 3, 7]. Below each value, write arr[i] - i. It reads -10, -6, -2, 0, 3 and never goes down.
The plan
lo = 0, hi = n - 1, answer = -1
while lo <= hi:
mid = lo + (hi - lo) / 2
if arr[mid] >= mid:
if arr[mid] == mid: answer = mid
hi = mid - 1
else:
lo = mid + 1
return answer
Complexity
- Time
O(log n)- Space
O(1)
The range halves each step.
Edge cases
[0]: 0.[1]: -1.- Every value 1 below its index, as in
[-1, 0, 1, 2, 3]: no fixed point, so -1. - Several fixed points, as in
[-3, -1, 1, 3, 4, 5]: indexes 3, 4 and 5 all work, and the answer is 3. - Repeated values break the method, because
arr[i] - ican then go down.
I've built my logic, show the code
Java
class Solution {
public int fixedPoint(int[] arr) {
// with distinct sorted whole numbers, arr[i] - i never decreases, so search for the first i with arr[i] >= i
int lo = 0, hi = arr.length - 1, answer = -1;
while (lo <= hi) {
int mid = lo + (hi - lo) / 2;
if (arr[mid] >= mid) {
if (arr[mid] == mid) answer = mid; // a fixed point; a smaller one may exist to the left
hi = mid - 1;
} else {
lo = mid + 1; // arr[mid] < mid, and so for every index before it
}
}
return answer;
}
}C++
class Solution {
public:
int fixedPoint(vector<int>& arr) {
// with distinct sorted whole numbers, arr[i] - i never decreases, so search for the first i with arr[i] >= i
int lo = 0, hi = static_cast<int>(arr.size()) - 1, answer = -1;
while (lo <= hi) {
int mid = lo + (hi - lo) / 2;
if (arr[mid] >= mid) {
if (arr[mid] == mid) answer = mid; // a fixed point; a smaller one may exist to the left
hi = mid - 1;
} else {
lo = mid + 1; // arr[mid] < mid, and so for every index before it
}
}
return answer;
}
};Python
class Solution:
def fixedPoint(self, arr: list[int]) -> int:
# with distinct sorted whole numbers, arr[i] - i never decreases, so search for the first i with arr[i] >= i
lo, hi, answer = 0, len(arr) - 1, -1
while lo <= hi:
mid = (lo + hi) // 2
if arr[mid] >= mid:
if arr[mid] == mid:
answer = mid # a fixed point; a smaller one may exist to the left
hi = mid - 1
else:
lo = mid + 1 # arr[mid] < mid, and so for every index before it
return answerCommon mistakes
- Returning the first fixed point found instead of looking left for a smaller one.
- Using this on arrays with repeated values.
- Mixing up 0-based and 1-based indexes.