Open In App

Find a Fixed Point (Value equal to index) in a given array

Last Updated : 14 Feb, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Given an array of n distinct integers sorted in ascending order, the task is to find the First Fixed Point in the array. Fixed Point in an array is an index i such that arr[i] equals i. Note that integers in the array can be negative

Note: If no Fixed Point is present in the array, print -1.

Examples

Input: arr[] = [-10, -5, 0, 3, 7]
Output: 3
Explanation: The value at index 3 of array arr[] is 3, which is equal to the index.

Input: arr[] = [0, 2, 5, 8, 17]
Output: 0
Explanation: The value at index 0 of array arr[] is 0, which is equal to the index.

Input: arr[] = [-10, -5, 3, 4, 7, 9]
Output: -1
Explanation: No Fixed Point

[Naive Approach] Using Linear Search – O(n) Time and O(1) Space

The idea is to iterate through the given array and find the index of the first fixed point. To do so, start iterating from the 0th index, and for each index i, check if arr[i] == i, if so return i, else traverse through other indices. If no fixed point is present in the array arr[], return -1.

Below is the implementation of the above approach:

C++
// C++ program to check fixed point
// in an array using linear search
#include <bits/stdc++.h>
using namespace std;

int fixedPoint(vector<int> &arr) {
    for (int i = 0; i < arr.size(); i++) {
        if (arr[i] == i)
            return i;
    }

    // If no fixed point is found
    return -1;
}

int main() {
    vector<int> arr = { -10, -5, 0, 3, 7};
    cout<<fixedPoint(arr);
    return 0;
}
C
// C program to check fixed point
// in an array using linear search
#include <stdio.h>

int fixedPoint(int arr[], int size) {
    for (int i = 0; i < size; i++) {
        if (arr[i] == i)
            return i;
    }

    // If no fixed point is found
    return -1;
}

int main() {
    int arr[] = { -10, -5, 0, 3, 7 };
    int size = sizeof(arr) / sizeof(arr[0]);
    printf("%d", fixedPoint(arr, size));
    return 0;
}
Java
// Java program to check fixed point
// in an array using linear search

class GfG {

    static int fixedPoint(int[] arr) {
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] == i)
                return i;
        }

        // If no fixed point is found
        return -1;
    }

    public static void main(String[] args) {
        int[] arr = { -10, -5, 0, 3, 7 };
        System.out.println(fixedPoint(arr));
    }
}
Python
# Python program to check fixed point
# in an array using linear search

def fixedPoint(arr):
    for i in range(len(arr)):
        if arr[i] == i:
            return i

    # If no fixed point is found
    return -1
    
if __name__ == "__main__":
    arr = [-10, -5, 0, 3, 7]
    print(fixedPoint(arr))
C#
// C# program to check fixed point
// in an array using linear search
using System;

class GfG {

    static int fixedPoint(int[] arr) {
        for (int i = 0; i < arr.Length; i++) {
            if (arr[i] == i)
                return i;
        }

        // If no fixed point is found
        return -1;
    }

    static void Main() {
        int[] arr = { -10, -5, 0, 3, 7 };
        Console.WriteLine(fixedPoint(arr));
    }
}
JavaScript
// JavaScript program to check fixed point
// in an array using linear search

function fixedPoint(arr) {
    for (let i = 0; i < arr.length; i++) {
        if (arr[i] === i)
            return i;
    }

    // If no fixed point is found
    return -1;
}

// Driver Code
let arr = [-10, -5, 0, 3, 7];
console.log(fixedPoint(arr));

Output
3

Time Complexity: O(n), to traverse the array, where n is the size of the array.
Auxiliary Space: O(1) 

[Expected Approach] Using Binary Search – O(log n) Time and O(1) Space

The idea is to use Binary Search to find the index of the fixed point. To do so, take the 0th index as starting point and the last index n – 1 as ending point. Now perform binary search until start <= end, firstly calculate the mid value (start + end / 2). For each loop, there are three possibilities:

  • if mid == arr[mid]: return the index mid.
  • if mid > arr[mid]: Then all the indices in left of mid (i.e. 0 <= mid) will follow this relation, thus move search space to right by making start = mid + 1.
  • if mid < arr[mid]: Then all the indices in right of mid (i.e. mid + 1 <= i < n) will follow this relation, thus move search space to left by making end = mid – 1.

If start > end, return -1 as no fixed point is found.

Below is given the implementation:

C++
// C++ program to check fixed point
// in an array using binary search
#include <bits/stdc++.h>
using namespace std;

int fixedPoint(vector<int> &arr) {

    int low = 0, high = arr.size() - 1;

    while (low <= high) {
        int mid = low + (high - low) / 2;

        if (arr[mid] == mid)
            return mid;
        else if (arr[mid] < mid)
            low = mid + 1;
        else
            high = mid - 1;
    }

    // If no fixed point is found
    return -1;
}

int main() {
    vector<int> arr = { -10, -5, 0, 3, 7};
    cout<<fixedPoint(arr);
    return 0;
}
C
// C program to check fixed point
// in an array using binary search
#include <stdio.h>

int fixedPoint(int arr[], int size) {

    int low = 0, high = size - 1;

    while (low <= high) {
        int mid = low + (high - low) / 2;

        if (arr[mid] == mid)
            return mid;
        else if (arr[mid] < mid)
            low = mid + 1;
        else
            high = mid - 1;
    }

    // If no fixed point is found
    return -1;
}

int main() {
    int arr[] = { -10, -5, 0, 3, 7 };
    int size = sizeof(arr) / sizeof(arr[0]);
    printf("%d", fixedPoint(arr, size));
    return 0;
}
Java
// Java program to check fixed point
// in an array using binary search

class GfG {

    static int fixedPoint(int[] arr) {

        int low = 0, high = arr.length - 1;

        while (low <= high) {
            int mid = low + (high - low) / 2;

            if (arr[mid] == mid)
                return mid;
            else if (arr[mid] < mid)
                low = mid + 1;
            else
                high = mid - 1;
        }

        // If no fixed point is found
        return -1;
    }

    public static void main(String[] args) {
        int[] arr = { -10, -5, 0, 3, 7 };
        System.out.println(fixedPoint(arr));
    }
}
Python
# Python program to check fixed point
# in an array using binary search

def fixedPoint(arr):

    low, high = 0, len(arr) - 1

    while low <= high:
        mid = low + (high - low) // 2

        if arr[mid] == mid:
            return mid
        elif arr[mid] < mid:
            low = mid + 1
        else:
            high = mid - 1

    # If no fixed point is found
    return -1

if __name__ == "__main__":
    arr = [-10, -5, 0, 3, 7]
    print(fixedPoint(arr))
C#
// C# program to check fixed point
// in an array using binary search
using System;

class GfG {

    static int fixedPoint(int[] arr) {

        int low = 0, high = arr.Length - 1;

        while (low <= high) {
            int mid = low + (high - low) / 2;

            if (arr[mid] == mid)
                return mid;
            else if (arr[mid] < mid)
                low = mid + 1;
            else
                high = mid - 1;
        }

        // If no fixed point is found
        return -1;
    }

    static void Main() {
        int[] arr = { -10, -5, 0, 3, 7 };
        Console.WriteLine(fixedPoint(arr));
    }
}
JavaScript
// JavaScript program to check fixed point
// in an array using binary search

function fixedPoint(arr) {

    let low = 0, high = arr.length - 1;

    while (low <= high) {
        let mid = low + Math.floor((high - low) / 2);

        if (arr[mid] === mid)
            return mid;
        else if (arr[mid] < mid)
            low = mid + 1;
        else
            high = mid - 1;
    }

    // If no fixed point is found
    return -1;
}

// Driver Code
let arr = [-10, -5, 0, 3, 7];
console.log(fixedPoint(arr));

Output
3

Time Complexity: O(log n), as we are dividing the search space to half after each iteration.
Space Complexity: O(1)

Related Articles:



Next Article

Similar Reads