Searching & SortingProblem 2 of 36
Find a Fixed Point (Value equal to index) in a given array
Problem Statement
Given a sorted array of distinct integers, find a fixed point. A fixed point is an index i such that arr[i] = i.
Constraints:
- Array is sorted in ascending order
- All elements are distinct
- Return any fixed point if multiple exist, or -1 if none exists
Example:
- Input:
arr = [-10, -1, 0, 3, 10, 11, 30, 50, 100] - Output:
3 - Explanation: arr[3] = 3, so 3 is a fixed point
Example 2:
- Input:
arr = [0, 2, 5, 8, 17] - Output:
0 - Explanation: arr[0] = 0, so 0 is a fixed point
Example 3:
- Input:
arr = [-10, -5, 3, 4, 7, 9] - Output:
-1 - Explanation: No index i where arr[i] = i
Approach 1: Brute Force (Linear Search)
Intuition
Check each index to see if the value at that index equals the index itself.
Algorithm
- Traverse the array from index 0 to n-1
- At each index i, check if arr[i] == i
- If found, return i
- If no fixed point found, return -1
java
public class Solution {
public int findFixedPoint(int[] arr) {
for (int i = 0; i < arr.length; i++) {
if (arr[i] == i) {
return i;
}
}
return -1;
}
}Complexity Analysis
- Time Complexity: O(n) - May need to check all elements
- Space Complexity: O(1) - No extra space used
Approach 2: Binary Search (Optimal)
Intuition
Since the array is sorted and contains distinct elements, we can use binary search. The key insight is:
- If
arr[mid] < mid, the fixed point (if exists) must be on the right side - If
arr[mid] > mid, the fixed point (if exists) must be on the left side - If
arr[mid] == mid, we found a fixed point
Why this works:
- Array is sorted with distinct elements
- If
arr[mid] < mid, thenarr[mid-1] < mid-1(since elements are distinct and sorted) - Similarly, all elements to the left will also be less than their indices
- So we can safely search the right half
Algorithm
- Initialize
left = 0andright = n-1 - While
left <= right:- Calculate
mid = left + (right - left) / 2 - If
arr[mid] == mid, return mid - If
arr[mid] < mid, search right half (left = mid + 1) - If
arr[mid] > mid, search left half (right = mid - 1)
- Calculate
- Return -1 if no fixed point found
java
public class Solution {
public int findFixedPoint(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;
}
}Dry Run Example
arr = [-10, -1, 0, 3, 10, 11, 30, 50, 100]
indices: 0 1 2 3 4 5 6 7 8
left=0, right=8
mid=4, arr[4]=10 > 4 → right=3
left=0, right=3
mid=1, arr[1]=-1 < 1 → left=2
left=2, right=3
mid=2, arr[2]=0 < 2 → left=3
left=3, right=3
mid=3, arr[3]=3 == 3 → Return 3
Fixed Point: 3
Complexity Analysis
- Time Complexity: O(log n) - Binary search
- Space Complexity: O(1) - Only using pointers
Variation: Find Leftmost Fixed Point
If multiple fixed points exist, find the smallest index.
java
public class Solution {
public int findLeftmostFixedPoint(int[] arr) {
int left = 0, right = arr.length - 1;
int result = -1;
while (left <= right) {
int mid = left + (right - left) / 2;
if (arr[mid] == mid) {
result = mid;
right = mid - 1; // Continue searching left
} else if (arr[mid] < mid) {
left = mid + 1;
} else {
right = mid - 1;
}
}
return result;
}
}Handling Non-Distinct Elements
When elements are not distinct, binary search needs modification since we can't simply discard half the array.
Key Takeaways
- Binary Search Application: This is a classic application of binary search on a sorted array with distinct elements
- Key Insight: The sorted and distinct properties allow us to eliminate half the search space
- Comparison with Index: Instead of comparing with a target, we compare arr[mid] with mid
- Non-Distinct Case: Requires a different approach as we can't always eliminate half the array
- Multiple Fixed Points: Modify binary search to find leftmost/rightmost if needed
- Edge Cases: Handle empty arrays and arrays with all negative or all positive numbers