algorithms
array indexing
programming
computational problem
array search

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

 
1def findFixedPoint(arr):
2    for i in range(len(arr)):
3        if arr[i] == i:
4            return i
5    return -1

Time Complexity

  • Time Complexity: O(n)O(n), where n is the number of elements in the array.
  • Space Complexity: O(1)O(1), no additional space is required other than input and output.

Given the sorted nature of the array and the distinct integers, binary search can significantly improve performance.

Explanation

  1. Initial Setup: Start with two pointers, low at the beginning and high at the end of the array.
  2. Binary Search Logic: Compute the middle index mid.
    • If arr[mid] equals mid, you've found a fixed point.
    • If arr[mid] is less than mid, potential fixed points could exist in the right subarray.
    • If arr[mid] is greater than mid, potential fixed points could exist in the left subarray.
  3. Iterative or Recursive Approach: Apply the above logic iteratively or recursively to narrow down the search.

Pseudocode

 
1def findFixedPointBinary(arr):
2    low, high = 0, len(arr) - 1
3    while low <= high:
4        mid = (low + high) // 2
5        if arr[mid] == mid:
6            return mid
7        elif arr[mid] < mid:
8            low = mid + 1
9        else:
10            high = mid - 1
11    return -1

Time Complexity

  • Time Complexity: O(logn)O(\log n), due to the divide-and-conquer nature of binary search.
  • Space Complexity: O(1)O(1), 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 subarray
    • mid = 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 i is 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

ApproachTime ComplexitySpace ComplexitySuitable For
Naive Linear SearchO(n)O(n)O(1)O(1)Small arrays or when array is not sorted ideally
Binary SearchO(logn)O(\log n)O(1)O(1)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.


Course illustration
Course illustration

All Rights Reserved.