Skip to content
Related Articles

Related Articles

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

View Discussion
Improve Article
Save Article
  • Difficulty Level : Medium
  • Last Updated : 02 Aug, 2022
View Discussion
Improve Article
Save Article

Given an array of n integers sorted in ascending order, write a function that returns a Fixed Point in the array, if there is any Fixed Point present in array, else returns -1. Fixed Point in an array is an index i such that arr[i] is equal to i. Note that integers in array can be negative. 

Examples: 

  Input: arr[] = {-10, -5, 0, 3, 7}
  Output: 3  // arr[3] == 3 

  Input: arr[] = {-10, -5, 2, 2, 2, 3, 4, 7, 9, 12, 13}
  Output: 2  // arr[2] == 2 

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

We have a solution to find fixed point in an array of distinct elements. In this post, solution for array with duplicate values is discussed.
Consider the arr[] = {-10, -5, 2, 2, 2, 3, 4, 7, 9, 12, 13}, arr[mid] = 3
If elements are not distinct, then we see arr[mid] < mid, we cannot conclude which side the fixed is on. It could be on left side or on the right side.
We know for sure that since arr[5] = 3, arr[4] couldn’t be magic index because arr[4] must be less than or equal to arr[5] (the array is Sorted).
So, the general pattern of our search would be: 

  • Left Side: start = start, end = min(arr[midIndex], midIndex-1)
  • Right Side: start = max(arr[midIndex], midIndex+1), end = end

Below is the code for above Algorithm.

C++




// CPP Program to find magic index.
#include <bits/stdc++.h>
using namespace std;
 
int magicIndex(int* arr, int start, int end)
{
    // If No Magic Index return -1;
    if (start > end)
        return -1;
 
    int midIndex = (start + end) / 2;
    int midValue = arr[midIndex];
 
    // Magic Index Found, return it.
    if (midIndex == midValue)
        return midIndex;
 
    // Search on Left side
    int left = magicIndex(arr, start, min(midValue,
                                     midIndex - 1));
 
    // If Found on left side, return.
    if (left >= 0)
        return left;
 
    // Return ans from right side.
    return magicIndex(arr, max(midValue, midIndex + 1),
                                                  end);
}
 
// Driver program
int main()
{
    int arr[] = { -10, -5, 2, 2, 2, 3, 4, 7,
                                 9, 12, 13 };
    int n = sizeof(arr) / sizeof(arr[0]);
    int index = magicIndex(arr, 0, n - 1);
    if (index == -1)
        cout << "No Magic Index";
    else
        cout << "Magic Index is : " << index;
    return 0;
}

Java




// Java Program to find magic index.
 
class GFG {
     
    static int magicIndex(int arr[], int start, int end)
    {
        // If No Magic Index return -1;
        if (start > end)
            return -1;
     
        int midIndex = (start + end) / 2;
        int midValue = arr[midIndex];
     
        // Magic Index Found, return it.
        if (midIndex == midValue)
            return midIndex;
     
        // Search on Left side
        int left = magicIndex(arr, start, Math.min(midValue,
                                            midIndex - 1));
     
        // If Found on left side, return.
        if (left >= 0)
            return left;
     
        // Return ans from right side.
        return magicIndex(arr, Math.max(midValue,
                                midIndex + 1),end);
    }
 
    // Driver code
    public static void main (String[] args)
    {
        int arr[] = { -10, -5, 2, 2, 2, 3, 4, 7,
                    9, 12, 13 };
        int n = arr.length;
        int index = magicIndex(arr, 0, n - 1);
        if (index == -1)
            System.out.print("No Magic Index");
        else
            System.out.print("Magic Index is : "+index);
    }
}
 
// This code is contributed by Anant Agarwal.

Python 3




# Python 3 Program to find
# magic index.
 
def magicIndex(arr, start, end):
 
    # If No Magic Index return -1
    if (start > end):
        return -1
 
    midIndex = int((start + end) / 2)
    midValue = arr[midIndex]
 
    # Magic Index Found, return it.
    if (midIndex == midValue):
        return midIndex
 
    # Search on Left side
    left = magicIndex(arr, start, min(midValue,
                                midIndex - 1))
 
    # If Found on left side, return.
    if (left >= 0):
        return left
 
    # Return ans from right side.
    return magicIndex(arr, max(midValue,
                        midIndex + 1),
                                    end)
 
# Driver program
arr = [-10, -5, 2, 2, 2, 3, 4, 7, 9, 12, 13]
n = len(arr)
 
index = magicIndex(arr, 0, n - 1)
 
if (index == -1):
    print("No Magic Index")
else:
    print("Magic Index is :", index)
 
# This code is contributed by Smitha Dinesh Semwal

C#




// C# Program to find magic index.
using System;
 
class GFG {
     
    static int magicIndex(int []arr, int start,
                                    int end)
    {
        // If No Magic Index return -1;
        if (start > end)
            return -1;
     
        int midIndex = (start + end) / 2;
        int midValue = arr[midIndex];
     
        // Magic Index Found, return it.
        if (midIndex == midValue)
            return midIndex;
     
        // Search on Left side
        int left = magicIndex(arr, start, Math.Min(midValue,
                                            midIndex - 1));
     
        // If Found on left side, return.
        if (left >= 0)
            return left;
     
        // Return ans from right side.
        return magicIndex(arr, Math.Max(midValue,
                                midIndex + 1),end);
    }
 
    // Driver code
    public static void Main ()
    {
        int []arr = { -10, -5, 2, 2, 2, 3,
                        4, 7, 9, 12, 13 };
         
        int n = arr.Length;
         
        int index = magicIndex(arr, 0, n - 1);
         
        if (index == -1)
            Console.WriteLine("No Magic Index");
        else
            Console.WriteLine("Magic Index is : " +
                                            index);
    }
}
 
// This code is contributed by vt_m.

PHP




<?php
// PHP Program to find magic index.
 
function magicIndex($arr, $start, $end)
{
     
    // If No Magic Index return -1;
    if ($start > $end)
        return -1;
 
    $midIndex = floor(($start + $end) / 2);
    $midValue = $arr[$midIndex];
 
    // Magic Index Found, return it.
    if ($midIndex == $midValue)
        return $midIndex;
 
    // Search on Left side
    $left = magicIndex($arr, $start,
            min($midValue, $midIndex - 1));
 
    // If Found on left side, return.
    if ($left >= 0)
        return $left;
 
    // Return ans from right side.
    return magicIndex($arr, max($midValue,
                     $midIndex + 1), $end);
}
     
    // Driver Code
    $arr = array(-10, -5, 2, 2, 2, 3,
                     4, 7, 9, 12, 13);
    $n = sizeof($arr);
    $index = magicIndex($arr, 0, $n - 1);
    if ($index == -1)
        echo "No Magic Index";
    else
        echo "Magic Index is : " , $index;
     
// This code is contributed by nitin mittal
?>

Javascript




<script>
// JavaScript Program to find magic index.
 
function magicIndex(arr, start, end)
{
    // If No Magic Index return -1;
    if (start > end)
        return -1;
 
    let midIndex = Math.floor((start + end) / 2);
    let midValue = arr[midIndex];
 
    // Magic Index Found, return it.
    if (midIndex == midValue)
        return midIndex;
 
    // Search on Left side
    let left = magicIndex(arr, start, Math.min(midValue,
                                    midIndex - 1));
 
    // If Found on left side, return.
    if (left >= 0)
        return left;
 
    // Return ans from right side.
    return magicIndex(arr, Math.max(midValue, midIndex + 1),
                                                end);
}
 
// Driver program
    let arr = [ -10, -5, 2, 2, 2, 3, 4, 7,
                                9, 12, 13 ];
    let n = arr.length;
    let index = magicIndex(arr, 0, n - 1);
    if (index == -1)
        document.write("No Magic Index");
    else
        document.write("Magic Index is : " + index);
 
 
 
 
// This code is contributed by Surbhi Tyagi.
</script>

Output

Magic Index is : 2

Time Complexity: O(N)
Auxiliary Space: O(1)


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!