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++
// C++ program to check fixed point // in an array using linear search #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; } /* If no fixed point present then return -1 */ return -1; } /* Driver code */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; } // This is code is contributed by rathbhupendra |
C
// C program to check fixed point // in an array using linear search #include<stdio.h> int linearSearch(int arr[], int n) { int i; for(i = 0; i < n; i++) { if(arr[i] == i) return i; } /* If no fixed point present then return -1 */ return -1; } /* Driver program to check above functions */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
// Java program to check fixed point // in an array using linear search class Main { static int linearSearch(int arr[], int n) { int i; for(i = 0; i < n; i++) { if(arr[i] == i) return i; } /* If no fixed point present then return -1 */ return -1; } //main function 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)); } } |
Python
# Python program to check fixed point # in an array using linear search def linearSearch(arr, n): for i in range(n): if arr[i] is i: return i # 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 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; } /* If no fixed point present then return -1 */ return -1; } // Driver code 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)); } } // This code is contributed by Sam007 |
PHP
<?php // PHP program to check fixed point // in an array using linear search function linearSearch($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.
C++
// C++ program to check fixed point // in an array using binary search #include <bits/stdc++.h> using namespace std; int binarySearch(int arr[], int low, int high) { if(high >= low) { int mid = (low + high)/2; /*low + (high - low)/2;*/ if(mid == arr[mid]) return mid; if(mid > arr[mid]) return binarySearch(arr, (mid + 1), high); else return binarySearch(arr, low, (mid -1)); } /* Return -1 if there is no Fixed Point */ return -1; } /* Driver code */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; } // This code is contributed by rathbhupendra |
C
// C program to check fixed point // in an array using binary search #include<stdio.h> int binarySearch(int arr[], int low, int high) { if(high >= low) { int mid = (low + high)/2; /*low + (high - low)/2;*/ if(mid == arr[mid]) return mid; if(mid > arr[mid]) return binarySearch(arr, (mid + 1), high); else return binarySearch(arr, low, (mid -1)); } /* Return -1 if there is no Fixed Point */ return -1; } /* Driver program to check above functions */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
// Java program to check fixed point // in an array using binary search class Main { static int binarySearch(int arr[], int low, int high) { if(high >= low) { /* low + (high - low)/2; */ int mid = (low + high)/2; if(mid == arr[mid]) return mid; if(mid > arr[mid]) return binarySearch(arr, (mid + 1), high); else return binarySearch(arr, low, (mid -1)); } /* Return -1 if there is no Fixed Point */ return -1; } //main function 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)); } } |
Python
# Python program to check fixed point # in an array using binary search def binarySearch(arr, low, high): if high >= low: mid = (low + high)//2 if mid is arr[mid]: return mid if mid > arr[mid]: return binarySearch(arr, (mid + 1), high) else: return binarySearch(arr, low, (mid -1)) # Return -1 if there is no Fixed Point 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(binarySearch(arr, 0, n-1))) # This code is contributed by Pratik Chhajer |
C#
// C# program to check fixed point // in an array using binary search using System; class GFG { static int binarySearch(int []arr, int low, int high) { if(high >= low) { // low + (high - low)/2; int mid = (low + high)/2; if(mid == arr[mid]) return mid; if(mid > arr[mid]) return binarySearch(arr, (mid + 1), high); else return binarySearch(arr, low, (mid -1)); } /* Return -1 if there is no Fixed Point */ return -1; } // Driver code 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)); } } // This code is contributed by Sam007 |
PHP
<?php // PHP program to check fixed po // in an array using binary search function binarySearch($arr, $low, $high) { if($high >= $low) { /*low + (high - low)/2;*/ $mid = (int)(($low + $high) / 2); if($mid == $arr[$mid]) return $mid; if($mid > $arr[$mid]) return binarySearch($arr, ($mid + 1), $high); else return binarySearch($arr, $low, ($mid - 1)); } /* Return -1 if there is no Fixed Po*/ return -1; } // Driver Code $arr = array(-10, -1, 0, 3, 10, 11, 30, 50, 100); $n = count($arr); echo "Fixed Point is: " . binarySearch($arr, 0, $n - 1); // This code is contributed by Anuj_67 ?> |
Output:
Fixed Point is 3
Algorithmic Paradigm: Divide & Conquer
Time Complexity: O(Logn)
Find a Fixed Point (Value equal to index) in a given array | Duplicates Allowed
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.
Recommended Posts:
- Find a Fixed Point (Value equal to index) in a given array | Duplicates Allowed
- Find a Fixed Point in an array with duplicates allowed
- Find Equal (or Middle) Point in a sorted array with duplicates
- Find an equal point in a string of brackets
- Minimum value to add to arr[i] so that an array can be split at index i with equal sum
- Count of index pairs with equal elements in an array
- Count of pairs having each element equal to index of the other from an Array
- Minimum index i such that all the elements from index i to given index are equal
- Minimize operations required to make each element of Array equal to it's index value
- Check if every index i has an index j such that sum of elements in both directions are equal
- Number of permutations such that sum of elements at odd index and even index are equal
- Find a partition point in array
- Find a partition point in array to maximize its xor sum
- Find the transition point in a binary array
- Find starting index for every occurrence of given array B in array A using Z-Algorithm
- How to find last index of a number in an Array in C++
- Find the index of first 1 in a sorted array of 0's and 1's
- Find the index of an array element in Java
- Find the index of first 1 in an infinite sorted array of 0s and 1s
- Find an element in array such that sum of left array is equal to sum of right array

