Find a Fixed Point in a given array | GeeksforGeeks

preview_player
Показать описание

This video is contributed by Harshit Jain.
Рекомендации по теме
Комментарии
Автор

class Solution{
public:

vector<int> valueEqualToIndex(int arr[], int n)
{
// int l=0;
// int r=n-1;
vector<int>v;
for(int i=0;i<n;i++)
{
if(arr[i]==i+1)
{
v.push_back(i+1);
}
}
return v;
}
};

wecan
Автор

This solutio#2 doesn't work when there are more than 1 satisfying indexes e.g. [-5, -3, 2, 3, 7, 9, 10], as per the problem statement we need to return first index for which value matches the index.

anshulsharma
Автор

2nd method doesn't hold good for all condition

yogeshvaishnav
Автор

what if an array contain multiple fixed point in it eg: arr = [0, 1, 2]

Then which method we should use ?

karanganwani
Автор

IN RUBY
arr.each_with_index do |a, i|
if arr[i] == i
res = i
break
else
res = -1
end
end

narendrakanojia
Автор

lets assume mid =7 and a[mid] = 8 there is possibility for a[10] = 10 but you completely omitted that part of array ...

goldbalaji
Автор

If there are duplicates values in the array then will this algorithm (binary search) work ?

zaffariqbal
Автор

If array integers are randomly arranged can we still able to get better solution than linear search???0(n)

vishwaskumar
Автор

what if an array have two same value at same index than ???
{1, 1, 2, 5, 8}
now does it work ??? reply please

najisheqbal
Автор

if array is of {0, 1, 2} then method 2 doesn't work....

saikiranreddymekala