Algorithm to find if there is any i so that arrayi equals i
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
In computer science, finding the index i in an array such that array[i] = i is known as the fixed-point problem. Such a point is termed a "fixed point" because the array index and its value are the same. This article will explore several methods to solve this problem, focusing on efficiency and optimal approaches.
Problem Statement
Given a sorted array of distinct integers, our goal is to determine if there exists an index i such that array[i] = i. If such an index exists, return it; otherwise, return -1.
Algorithms and Approaches
Naive Approach
The simplest method to solve this problem is a linear search, where we iterate over each element of the array and check if the condition holds.
Pseudocode
Time Complexity
- Time Complexity: , where
nis the number of elements in the array. - Space Complexity: , no additional space is required other than input and output.
Improved Approach: Binary Search
Given the sorted nature of the array and the distinct integers, binary search can significantly improve performance.
Explanation
- Initial Setup: Start with two pointers,
lowat the beginning andhighat the end of the array. - Binary Search Logic: Compute the middle index
mid.- If
arr[mid]equalsmid, you've found a fixed point. - If
arr[mid]is less thanmid, potential fixed points could exist in the right subarray. - If
arr[mid]is greater thanmid, potential fixed points could exist in the left subarray.
- Iterative or Recursive Approach: Apply the above logic iteratively or recursively to narrow down the search.
Pseudocode
Time Complexity
- Time Complexity: , due to the divide-and-conquer nature of binary search.
- Space Complexity: , iterative approach uses constant space.
Example Walkthrough
Consider the array [-10, -5, 0, 3, 7].
- Naive Search:
- Check index 0: arr[0] = -10, which is not 0
- Check index 1: arr[1] = -5, which is not 1
- Check index 2: arr[2] = 0, which is not 2
- Check index 3: arr[3] = 3, which is 3: Return 3
- Binary Search:
mid= 2, arr[2] = 0, 0 < 2, search right subarraymid= 3, arr[3] = 3, 3 = 3: Return 3
Both approaches correctly identify the fixed point, but binary search is more efficient for large arrays.
Special Cases
- All Negative Integers: If all elements are strictly negative, there cannot be a fixed point because
iis non-negative. - All Positive Integers Exceeding the Largest Index: If every element is greater than the max index (e.g., array size 5 and all elements > 4), a fixed point cannot exist.
- Empty Array: The problem is trivially solved as there can be no indices.
Key Points Summary
| Approach | Time Complexity | Space Complexity | Suitable For |
| Naive Linear Search | Small arrays or when array is not sorted ideally | ||
| Binary Search | Large, sorted arrays of distinct integers |
Conclusion
The fixed-point problem showcases the importance of choosing the right algorithm based on input characteristics. While a naive linear search is straightforward, leveraging a sorted array with distinct values allows for efficient binary search application. This underlines the significance of understanding both problem constraints and algorithmic strategies to derive optimal solutions.

