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.
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++
// C++ program to check fixed point
// in an array using linear search
#include <bits/stdc++.h>
usingnamespacestd;
intlinearSearch(intarr[], intn)
{
inti;
for(i = 0; i < n; i++)
{
if(arr[i] == i)
returni;
}
/* If no fixed point present then return -1 */
return-1;
}
/* Driver code */
intmain()
{
intarr[] = {-10, -1, 0, 3, 10, 11, 30, 50, 100};
intn = sizeof(arr)/sizeof(arr[0]);
cout << "Fixed Point is "<< linearSearch(arr, n);
return0;
}
// This is code is contributed by rathbhupendra
C
// C program to check fixed point
// in an array using linear search
#include<stdio.h>
intlinearSearch(intarr[], intn)
{
inti;
for(i = 0; i < n; i++)
{
if(arr[i] == i)
returni;
}
/* If no fixed point present then return -1 */
return-1;
}
/* Driver program to check above functions */
intmain()
{
intarr[] = {-10, -1, 0, 3, 10, 11, 30, 50, 100};
intn = sizeof(arr)/sizeof(arr[0]);
printf("Fixed Point is %d", linearSearch(arr, n));
getchar();
return0;
}
Java
// Java program to check fixed point
// in an array using linear search
classMain
{
staticintlinearSearch(intarr[], intn)
{
inti;
for(i = 0; i < n; i++)
{
if(arr[i] == i)
returni;
}
/* If no fixed point present
then return -1 */
return-1;
}
//main function
publicstaticvoidmain(String args[])
{
intarr[] = {-10, -1, 0, 3, 10, 11, 30, 50, 100};
intn = arr.length;
System.out.println("Fixed Point is "
+ linearSearch(arr, n));
}
}
Python
# Python program to check fixed point
# in an array using linear search
deflinearSearch(arr, n):
fori inrange(n):
ifarr[i] isi:
returni
# If no fixed point present then return -1
return-1
# Driver program to check above functions
arr =[-10, -1, 0, 3, 10, 11, 30, 50, 100]
n =len(arr)
print("Fixed Point is "+str(linearSearch(arr,n)))
# This code is contributed by Pratik Chhajer
C#
// C# program to check fixed point
// in an array using linear search
usingSystem;
classGFG
{
staticintlinearSearch(int[]arr, intn)
{
inti;
for(i = 0; i < n; i++)
{
if(arr[i] == i)
returni;
}
/* If no fixed point present
then return -1 */
return-1;
}
// Driver code
publicstaticvoidMain()
{
int[]arr = {-10, -1, 0, 3, 10, 11, 30, 50, 100};
intn = arr.Length;
Console.Write("Fixed Point is "+ linearSearch(arr, n));
}
}
// This code is contributed by Sam007
PHP
<?php
// PHP program to check fixed point
// in an array using linear search
functionlinearSearch($arr, $n)
{
for($i= 0; $i< $n; $i++)
{
if($arr[$i] == $i)
return$i;
}
// If no fixed point present then
// return -1
return-1;
}
// Driver Code
$arr= array(-10, -1, 0, 3, 10,
11, 30, 50, 100);
$n= count($arr);
echo"Fixed Point is ".
linearSearch($arr,$n);
// This code is contributed by Sam007
?>
Output:
Fixed Point is 3
Time Complexity: O(n)
Method 2 (Binary Search)
First check whether middle element is Fixed Point or not. If it is, then return it; otherwise check whether index of middle element is greater than value at the index. If index is greater, then Fixed Point(s) lies on the right side of the middle point (obviously only if there is a Fixed Point). Else the Fixed Point(s) lies on left side.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy