Given an array arr[] of 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
Table of Content
[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:
#include <iostream>
#include <vector>
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;
}
#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;
}
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));
}
}
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))
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));
}
}
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
[Expected Approach] Using Binary Search - O(log n) Time and O(1) Space
The array is sorted in ascending order and contains distinct elements. This allows us to use Binary Search to efficiently locate a fixed point.
At any index i, compare the value arr[i] with i:
- If
arr[i] == i, theniis a fixed point. - If
arr[i] < i, then for all indices to the left,arr[j] < jwill also hold, so the fixed point (if any) must lie in the right half. - If
arr[i] > i, then for all indices to the right,arr[j] > jwill also hold, so the fixed point must lie in the left half.
To find the first fixed point, whenever a match is found, we store the index and continue searching on the left side to check if a smaller index also satisfies the condition.
#include <iostream>
#include <vector>
using namespace std;
int fixedPoint(vector<int>& arr) {
int low = 0, high = arr.size() - 1;
int ans = -1;
while (low <= high) {
int mid = low + (high - low) / 2;
// If fixed point is found, store it and search left for first occurrence
if (arr[mid] == mid) {
ans = mid;
high = mid - 1;
}
// If value is smaller than index, fixed point must be on the right
else if (arr[mid] < mid) {
low = mid + 1;
}
// If value is greater than index, fixed point must be on the left
else {
high = mid - 1;
}
}
return ans;
}
int main() {
vector<int> arr = { -10, -5, 0, 3, 7 };
cout << fixedPoint(arr);
return 0;
}
#include <stdio.h>
int fixedPoint(int arr[], int n) {
int low = 0, high = n - 1;
int ans = -1;
while (low <= high) {
int mid = low + (high - low) / 2;
// If fixed point is found, store it and search left for first occurrence
if (arr[mid] == mid) {
ans = mid;
high = mid - 1;
}
// If value is smaller than index, fixed point must be on the right
else if (arr[mid] < mid) {
low = mid + 1;
}
// If value is greater than index, fixed point must be on the left
else {
high = mid - 1;
}
}
return ans;
}
int main() {
int arr[] = { -10, -5, 0, 3, 7 };
int n = sizeof(arr) / sizeof(arr[0]);
printf("%d", fixedPoint(arr, n));
return 0;
}
class GfG {
static int fixedPoint(int[] arr) {
int low = 0, high = arr.length - 1;
int ans = -1;
while (low <= high) {
int mid = low + (high - low) / 2;
// If fixed point is found, store it and search left for first occurrence
if (arr[mid] == mid) {
ans = mid;
high = mid - 1;
}
// If value is smaller than index, fixed point must be on the right
else if (arr[mid] < mid) {
low = mid + 1;
}
// If value is greater than index, fixed point must be on the left
else {
high = mid - 1;
}
}
return ans;
}
public static void main(String[] args) {
int[] arr = { -10, -5, 0, 3, 7 };
System.out.print(fixedPoint(arr));
}
}
def fixedPoint(arr):
low, high = 0, len(arr) - 1
ans = -1
while low <= high:
mid = low + (high - low) // 2
# If fixed point is found, store it and search left for first occurrence
if arr[mid] == mid:
ans = mid
high = mid - 1
# If value is smaller than index, fixed point must be on the right
elif arr[mid] < mid:
low = mid + 1
# If value is greater than index, fixed point must be on the left
else:
high = mid - 1
return ans
arr = [-10, -5, 0, 3, 7]
print(fixedPoint(arr))
using System;
class GfG
{
static int fixedPoint(int[] arr)
{
int low = 0, high = arr.Length - 1;
int ans = -1;
while (low <= high)
{
int mid = low + (high - low) / 2;
// If fixed point is found, store it and search left for first occurrence
if (arr[mid] == mid)
{
ans = mid;
high = mid - 1;
}
// If value is smaller than index, fixed point must be on the right
else if (arr[mid] < mid)
{
low = mid + 1;
}
// If value is greater than index, fixed point must be on the left
else
{
high = mid - 1;
}
}
return ans;
}
static void Main()
{
int[] arr = { -10, -5, 0, 3, 7 };
Console.Write(fixedPoint(arr));
}
}
function fixedPoint(arr) {
let low = 0, high = arr.length - 1;
let ans = -1;
while (low <= high) {
let mid = Math.floor(low + (high - low) / 2);
// If fixed point is found, store it and search left for first occurrence
if (arr[mid] === mid) {
ans = mid;
high = mid - 1;
}
// If value is smaller than index, fixed point must be on the right
else if (arr[mid] < mid) {
low = mid + 1;
}
// If value is greater than index, fixed point must be on the left
else {
high = mid - 1;
}
}
return ans;
}
// Driver Code
const arr = [-10, -5, 0, 3, 7];
console.log(fixedPoint(arr));
Output
3