Find a Fixed Point (Value equal to index) in a given array
Last Updated :
03 Jul, 2023
Given an array of n distinct 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[] = {0, 2, 5, 8, 17}
Output: 0 // arr[0] == 0
Input: arr[] = {-10, -5, 3, 4, 7, 9}
Output: -1 // No Fixed Point
Method 1 (Linear Search)
Linearly search for an index i such that arr[i] == i. Return the first such index found. Thanks to pm for suggesting this solution.
C++
#include <bits/stdc++.h>
using namespace std;
int linearSearch( int arr[], int n)
{
int i;
for (i = 0; i < n; i++) {
if (arr[i] == i)
return i;
}
return -1;
}
int main()
{
int arr[] = { -10, -1, 0, 3, 10, 11, 30, 50, 100 };
int n = sizeof (arr) / sizeof (arr[0]);
cout << "Fixed Point is " << linearSearch(arr, n);
return 0;
}
|
C
#include <stdio.h>
int linearSearch( int arr[], int n)
{
int i;
for (i = 0; i < n; i++) {
if (arr[i] == i)
return i;
}
return -1;
}
int main()
{
int arr[] = { -10, -1, 0, 3, 10, 11, 30, 50, 100 };
int n = sizeof (arr) / sizeof (arr[0]);
printf ( "Fixed Point is %d" , linearSearch(arr, n));
getchar ();
return 0;
}
|
Java
class Main {
static int linearSearch( int arr[], int n)
{
int i;
for (i = 0 ; i < n; i++) {
if (arr[i] == i)
return i;
}
return - 1 ;
}
public static void main(String args[])
{
int arr[] = { - 10 , - 1 , 0 , 3 , 10 , 11 , 30 , 50 , 100 };
int n = arr.length;
System.out.println( "Fixed Point is "
+ linearSearch(arr, n));
}
}
|
Python3
def linearSearch(arr, n):
for i in range (n):
if arr[i] is i:
return i
return - 1
arr = [ - 10 , - 1 , 0 , 3 , 10 , 11 , 30 , 50 , 100 ]
n = len (arr)
print ( "Fixed Point is " + str (linearSearch(arr, n)))
|
C#
using System;
class GFG {
static int linearSearch( int [] arr, int n)
{
int i;
for (i = 0; i < n; i++) {
if (arr[i] == i)
return i;
}
return -1;
}
public static void Main()
{
int [] arr = { -10, -1, 0, 3, 10, 11, 30, 50, 100 };
int n = arr.Length;
Console.Write( "Fixed Point is " + linearSearch(arr, n));
}
}
|
PHP
<?php
function linearSearch( $arr , $n )
{
for ( $i = 0; $i < $n ; $i ++)
{
if ( $arr [ $i ] == $i )
return $i ;
}
return -1;
}
$arr = array (-10, -1, 0, 3, 10,
11, 30, 50, 100);
$n = count ( $arr );
echo "Fixed Point is " .
linearSearch( $arr , $n );
?>
|
Javascript
<script>
function linearSearch(arr, n)
{
let i;
for (i = 0; i < n; i++)
{
if (arr[i] == i)
return i;
}
return -1;
}
let arr = [-10, -1, 0, 3, 10, 11, 30, 50, 100];
let n = arr.length;
document.write( "Fixed Point is "
+ linearSearch(arr, n));
</script>
|
Output:
Fixed Point is 3
Time Complexity: O(n)
Auxiliary Space: O(1)
Method 2 (Binary Search)
First check whether middle element is Fixed Point or not. If it is, then return it; otherwise if the index of middle + 1 element is less than or equal to the value at the high index, then Fixed Point(s) might lie on the right side of the middle point (obviously only if there is a Fixed Point). Similarly, check if the index of middle – 1 element is greater than or equal to the value at the low index, then Fixed Point(s) might lie on the left side of the middle point.
C++
#include <bits/stdc++.h>
using namespace std;
int binarySearch( int arr[], int low, int high)
{
if (high >= low) {
int mid = low + (high - low) / 2;
if (mid == arr[mid])
return mid;
int res = -1;
if (mid + 1 <= arr[high])
res = binarySearch(arr, (mid + 1), high);
if (res != -1)
return res;
if (mid - 1 >= arr[low])
return binarySearch(arr, low, (mid - 1));
}
return -1;
}
int main()
{
int arr[10] = { -10, -1, 0, 3, 10, 11, 30, 50, 100 };
int n = sizeof (arr) / sizeof (arr[0]);
cout << "Fixed Point is " << binarySearch(arr, 0, n - 1);
return 0;
}
|
C
#include <stdio.h>
int binarySearch( int arr[], int low, int high)
{
if (high >= low) {
int mid = low + (high - low) / 2;
if (mid == arr[mid])
return mid;
int res = -1;
if (mid + 1 <= arr[high])
res = binarySearch(arr, (mid + 1), high);
if (res != -1)
return res;
if (mid - 1 >= arr[low])
return binarySearch(arr, low, (mid - 1));
}
return -1;
}
int main()
{
int arr[10] = { -10, -1, 0, 3, 10, 11, 30, 50, 100 };
int n = sizeof (arr) / sizeof (arr[0]);
printf ( "Fixed Point is %d" , binarySearch(arr, 0, n - 1));
getchar ();
return 0;
}
|
Java
class Main {
static int binarySearch( int arr[], int low, int high)
{
if (high >= low) {
int mid = low + (high - low) / 2 ;
if (mid == arr[mid])
return mid;
int res = - 1 ;
if (mid + 1 <= arr[high])
res = binarySearch(arr, (mid + 1 ), high);
if (res != - 1 )
return res;
if (mid - 1 >= arr[low])
return binarySearch(arr, low, (mid - 1 ));
}
return - 1 ;
}
public static void main(String args[])
{
int arr[] = { - 10 , - 1 , 0 , 3 , 10 , 11 , 30 , 50 , 100 };
int n = arr.length;
System.out.println( "Fixed Point is "
+ binarySearch(arr, 0 , n - 1 ));
}
}
|
Python3
def binarySearch(arr, low, high):
if high > = low :
mid = low + (high - low) / / 2
if mid = = arr[mid]:
return mid
res = - 1
if mid + 1 < = arr[high]:
res = binarySearch(arr, (mid + 1 ), high)
if res ! = - 1 :
return res
if mid - 1 > = arr[low]:
return binarySearch(arr, low, (mid - 1 ))
return - 1
arr = [ - 10 , - 1 , 0 , 3 , 10 , 11 , 30 , 50 , 100 ]
n = len (arr)
print ( "Fixed Point is " + str (binarySearch(arr, 0 , n - 1 )))
|
C#
using System;
class GFG {
static int binarySearch( int [] arr, int low, int high)
{
if (high >= low) {
int mid = low + (high - low) / 2;
if (mid == arr[mid])
return mid;
int res = -1;
if (mid + 1 <= arr[high])
res = binarySearch(arr, (mid + 1), high);
if (res != -1)
return res;
if (mid - 1 >= arr[low])
return binarySearch(arr, low, (mid - 1));
}
return -1;
}
public static void Main()
{
int [] arr = { -10, -1, 0, 3, 10, 11, 30, 50, 100 };
int n = arr.Length;
Console.Write( "Fixed Point is " + binarySearch(arr, 0, n - 1));
}
}
|
PHP
<?php
function binarySearch( $arr , $low , $high )
{
if ( $high >= $low )
{
$mid = (int)( $low + ( $high - $low )/2);
if ( $mid == $arr [ $mid ])
return $mid ;
$res = -1;
if ( $mid +1 <= $arr [ $high ])
$res = binarySearch( $arr , ( $mid + 1), $high );
if ( $res !=-1)
return $res ;
if ( $mid -1 >= $arr [ $low ])
return binarySearch( $arr , $low , ( $mid -1));
}
return -1;
}
$arr = array (-10, -1, 0, 3, 10,
11, 30, 50, 100);
$n = count ( $arr );
echo "Fixed Point is: "
. binarySearch( $arr , 0, $n - 1);
?>
|
Javascript
<script>
function binarySearch(arr,low,high)
{
if (high >= low)
{
let mid = math.floor(low + (high - low)/2);
if (mid == arr[mid])
return mid;
let res = -1;
if (mid+1 <= arr[high])
res = binarySearch(arr, (mid + 1), high);
if (res!=-1)
return res;
if (mid-1 >= arr[low])
return binarySearch(arr, low, (mid -1));
}
return -1;
}
let arr = [-10, -1, 0, 3, 10, 11, 30, 50, 100];
let n = arr.length;
document.write( "Fixed Point is " + binarySearch(arr, 0, n-1));
</script>
|
Output:
Fixed Point is 3
Algorithmic Paradigm: Divide & Conquer
Time Complexity: O(log n)
Auxiliary Space: O(log n) (As implicit stack is used for recursive calls)
Find a Fixed Point (Value equal to index) in a given array | Duplicates Allowed
Please Login to comment...