Find a Fixed Point in an array with duplicates allowed
Last Updated :
12 Jul, 2022
Given an array of n duplicates or 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 the 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 the array can be negative.
Examples :
Input : arr[] = {-10, -1, 3, 3, 10, 30, 30, 50, 100}
Output: 3
Note : arr[3] == 3
Input: arr[] = {0, 2, 5, 8, 17}
Output: 0
Input: arr[] = {-10, -5, 3, 4, 7, 9}
Output: -1
No Fixed Point
We have already discussed find a Fixed Point in a given array of n distinct integers.
If elements are not distinct, then previously discussed algorithm fails. Consider the following array:
// with duplicates value
Input : arr[] = {-10, -5, 2, 2, 2, 3, 4, 7, 9, 12, 13};
Wrong Output : -1 // but arr[2] == 2
When we see that A [mid] < mid, we cannot conclude which side the fixed index is on. It could be on the right side, as before. Or, it could be on the left side (as it, in fact, is).
Could it be anywhere on the left side? Not exactly. Since A[ 5] = 3, we know that A[ 4] couldn't be a fixed index. A[ 4] would need to be 4 to be the fixed index, but A[ 4] must be less than or equal to A[ 5].
In fact, when we see that A[ 5] = 3, we'll need to recursively search the right side as before. But, to search the left side, we can skip a bunch of elements and only recursively search elements A [ 0] through A [ 3]. A[ 3] is the first element that could be a fixed index.
The general pattern is that we compare mid Index and midValue for equality first. Then, if they are not equal, we recursively search the left and right sides as follows:
Implementation:
C++
// C++ implementation to find fixed
// index using binary search
#include<bits/stdc++.h>
using namespace std;
// Main Function to find fixed
// index using binary search
int binarySearch(int arr[], int low,
int high)
{
if (high < low)
return -1;
// low + (high - low) / 2
int mid = (low + high) / 2;
int midValue = arr[mid];
if (mid == arr[mid])
return mid;
// Search left
int leftindex = min(mid - 1, midValue);
int left = binarySearch(arr, low, leftindex);
if (left >= 0)
return left;
// Search right
int rightindex = max(mid + 1, midValue);
int right = binarySearch(arr, rightindex, high);
return right;
}
// Driver code
int main()
{
// input 1
int arr[] = {-10, -5, 2, 2, 2,
3, 4, 7, 9, 12, 13};
int n = sizeof(arr) / sizeof(arr[0]);
cout << "Fixed Point is "
<< binarySearch(arr, 0, n - 1);
// input 2
int arr1[] = {-10, -1, 3, 3, 10,
30, 30, 50, 100};
int n1 = sizeof(arr) / sizeof(arr1[0]);
cout << "\nFixed Point is "
<< binarySearch(arr1, 0, n1 - 1);
return 0;
}
Java
// Java implementation of find fixed
// index using binary search
class GFG
{
// Main Function to find fixed
// index using binary search
static int binarySearch(int arr[], int low,
int high)
{
if (high < low)
return -1;
// low + (high - low) / 2
int mid = (low + high) / 2;
int midValue = arr[mid];
if (mid == arr[mid])
return mid;
// Search left
int leftindex = Math.min(mid - 1, midValue);
int left = binarySearch(arr, low, leftindex);
if (left >= 0)
return left;
// Search right
int rightindex = Math.max(mid + 1, midValue);
int right = binarySearch(arr, rightindex, high);
return right;
}
// Driver code
public static void main(String[] args)
{
// input 1
int arr[] = {-10, -5, 2, 2, 2,
3, 4, 7, 9, 12, 13};
System.out.println("Fixed Point is " +
binarySearch(arr, 0, arr.length - 1));
// input 2
int arr1[] = {-10, -1, 3, 3, 10,
30, 30, 50, 100};
System.out.println("Fixed Point is " +
binarySearch(arr1, 0, arr1.length - 1));
}
}
Python3
# Python 3 implementation to find fixed
# index using binary search
# Main Function to find fixed
# index using binary search
def binarySearch(arr, low, high):
if (high < low):
return -1
# low + (high - low) / 2
mid = int((low + high) / 2)
midValue = arr[mid]
if (mid == arr[mid]):
return mid
# Search left
leftindex = min(mid - 1, midValue)
left = binarySearch(arr, low, leftindex)
if (left >= 0):
return left
# Search right
rightindex = max(mid + 1, midValue)
right = binarySearch(arr, rightindex, high)
return right
# Driver code
if __name__ == '__main__':
# input 1
arr = [-10, -5, 2, 2, 2, 3,
4, 7, 9, 12, 13]
n = len(arr)
print("Fixed Point is",
binarySearch(arr, 0, n - 1))
# input 2
arr1 = [-10, -1, 3, 3, 10,
30, 30, 50, 100]
n1 = len(arr)
print("Fixed Point is",
binarySearch(arr1, 0, n1 - 1))
# This code is contributed by
# Shashank_Sharma
C#
// C# implementation of find fixed
// index using binary search
using System;
class GFG
{
// Main Function to find fixed
// index using binary search
static int binarySearch(int []arr, int low,
int high)
{
if (high < low)
return -1;
// low + (high - low) / 2
int mid = (low + high) / 2;
int midValue = arr[mid];
if (mid == arr[mid])
return mid;
// Search left
int leftindex = Math.Min(mid - 1, midValue);
int left = binarySearch(arr, low, leftindex);
if (left >= 0)
return left;
// Search right
int rightindex = Math.Max(mid + 1, midValue);
int right = binarySearch(arr, rightindex, high);
return right;
}
// Driver Code
public static void Main()
{
// input 1
int []arr = {-10, -5, 2, 2, 2,
3, 4, 7, 9, 12, 13};
Console.WriteLine("Fixed Point is " +
binarySearch(arr, 0, arr.Length - 1));
// input 2
int []arr1 = {-10, -1, 3, 3, 10,
30, 30, 50, 100};
Console.Write("Fixed Point is " +
binarySearch(arr1, 0, arr1.Length - 1));
}
}
// This code is contributed by nitin mittal.
PHP
<?php
// PHP implementation to
// find fixed index using
// binary search
// Main Function to find fixed
// index using binary search
function binarySearch($arr,
$low,
$high)
{
if ($high < $low)
return -1;
// low + (high - low) / 2
$mid = floor(($low + $high) / 2);
$midValue = $arr[$mid];
if ($mid == $arr[$mid])
return $mid;
// Search left
$leftindex = min($mid - 1,
$midValue);
$left = binarySearch($arr, $low,
$leftindex);
if ($left >= 0)
return $left;
// Search right
$rightindex = max($mid + 1,
$midValue);
$right = binarySearch($arr,
$rightindex,
$high);
return $right;
}
// Driver code
// input 1
$arr = array(-10, -5, 2, 2, 2, 3,
4, 7, 9, 12, 13);
$n = sizeof($arr) / sizeof($arr[0]);
echo "Fixed Point is ",
binarySearch($arr, 0, $n - 1);
// input 2
$arr1 = array(-10, -1, 3, 3, 10,
30, 30, 50, 100);
$n1 = sizeof($arr) / sizeof($arr1[0]);
echo "\nFixed Point is ",
binarySearch($arr1, 0, $n1 - 1);
// This code is contributed by nitin mittal.
?>
JavaScript
<script>
// Javascript implementation to find fixed
// index using binary search
// Main Function to find fixed
// index using binary search
function binarySearch(arr, low, high)
{
if (high < low)
return -1;
// low + (high - low) / 2
var mid = parseInt((low + high) / 2);
var midValue = arr[mid];
if (mid == arr[mid])
return mid;
// Search left
var leftindex = Math.min(mid - 1, midValue);
var left = binarySearch(arr, low, leftindex);
if (left >= 0)
return left;
// Search right
var rightindex = Math.max(mid + 1, midValue);
var right = binarySearch(arr, rightindex, high);
return right;
}
// Driver code
// input 1
var arr = [-10, -5, 2, 2, 2,
3, 4, 7, 9, 12, 13];
var n = arr.length;
document.write("Fixed Point is "
+ binarySearch(arr, 0, n - 1));
// input 2
var arr1 = [-10, -1, 3, 3, 10,
30, 30, 50, 100];
var n1 = arr1.length;
document.write("<br>Fixed Point is "
+ binarySearch(arr1, 0, n1 - 1));
// This code is contributed by rrrtnx.
</script>
OutputFixed Point is 2
Fixed Point is 3
Algorithmic Paradigm : Divide & Conquer
Time Complexity : O(Logn)
Auxiliary Space: O(1)
This article is contributed by Mr. Somesh Awasthi.
Similar Reads
Find a Fixed Point (Value equal to index) in a given array | Duplicates Allowed
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 the 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 the array can be negative.
13 min read
Find Equal (or Middle) Point in a sorted array with duplicates
Given a sorted array of n size, the task is to find whether an element exists in the array from where the number of smaller elements is equal to the number of greater elements.If Equal Point appears multiple times in input array, return the index of its first occurrence. If doesn't exist, return -1.
9 min read
Find missing elements from an Array with duplicates
Given an array arr[] of size N having integers in the range [1, N] with some of the elements missing. The task is to find the missing elements. Note: There can be duplicates in the array. Examples: Input: arr[] = {1, 3, 3, 3, 5}, N = 5Output: 2 4Explanation: The numbers missing from the list are 2 a
12 min read
Search an element in a sorted and rotated array with duplicates
Given a sorted and rotated array arr[] and a key, the task is to find whether key exists in the rotated array (with duplicates).Examples: Input: arr[] = [3, 3, 3, 1, 2, 3], key = 3 Output: trueExplanation: arr[0] = 3Input: arr[] = {3, 3, 3, 1, 2, 3}, key = 11 Output: falseExplanation: 11 is not pres
6 min read
Find duplicates in an Array with values 1 to N using counting sort
Given a constant array of N elements which contain elements from 1 to N - 1, with any of these numbers appearing any number of times.Examples: Input: N = 5, arr[] = {1, 3, 4, 2, 2} Output: 2 Explanation: 2 is the number occurring more than once.Input: N = 5, arr[] = {3, 1, 3, 4, 2} Output: 3 Explana
11 min read
Find a Fixed Point (Value equal to index) in a given array
Given an array of n 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.Example
7 min read
Find Duplicates of array using bit array
You have an array of N numbers, where N is at most 32,000. The array may have duplicate entries and you do not know what N is. With only 4 Kilobytes of memory available, how would print all duplicate elements in the array ?. Examples: Input : arr[] = {1, 5, 1, 10, 12, 10} Output : 1 10 1 and 10 appe
8 min read
Find duplicate elements in an array
Given an array of n integers. The task is to find all elements that have more than one occurrences. The output should only be one occurrence of a number irrespective of the number of occurrences in the input array.Examples: Input: {2, 10, 10, 100, 2, 10, 11, 2, 11, 2}Output: {2, 10, 11}Input: {5, 40
11 min read
Check if all duplicate elements in the Array are adjacent or not
Given an array arr[]. The task is to check whether duplicate elements in arr[] are contiguous or not. Examples: Input: arr[] = {1, 2, 3, 4, 5, 6} Output: Yes Explanation: There is no duplicate element in arr[] so there is no need to check anything and answer is Yes. Input: arr[] = {1, 2, 2, 4} Outpu
4 min read
Find duplicates in constant array with elements 0 to N-1 in O(1) space
Given a constant array of n elements which contains elements from 1 to n-1, with any of these numbers appearing any number of times. Find any one of these repeating numbers in O(n) and using only constant memory space. Examples: Input : arr[] = {1, 2, 3, 4, 5, 6, 3} Output : 3 As the given array is
12 min read