• retagged by
1,782 views
1 1 vote
Given a sorted array of distinct interger A[1...N] , you want to find out whether there is an index "i" for which A[i] =i . if this problem is solved using divide and conquer method,then find time complexity.

1 Answer

5 5 votes

We can use modification of binary search here :

Basically we have to find index i such that a[i] = i in other a[i] - i = 0.

Now as the array is sorted , so all the indices j which is less than i will satisfy the property : a[j] - j <= 0 as the array is sorted so a[j] will be at least 1 less than a[i] and left index will also be at least 1 less than i hence the conclusion.

Similarly to the right of the ith index element , we have a[j] - j >= 0 .

So the key idea is just we find :

mid = left + right / 2 where left and right are the boundary indices and then check whether mid is the ith index we are looking for using the above mentioned property.For this we compare only its immediate left and right neighbours.

If it satisfies , well and good and we will report our final answer which is index i as mid only else we apply binary search recursively like normal one.

So time complexity remains the same i.e. O(logn)

Reference : http://stackoverflow.com/questions/4101141/algorithm-to-find-if-there-is-any-i-so-that-arrayi-equals-i

Position:
Show:

Related questions

1 1 vote
1 1 answer
210
210 views
GO Classes asked Aug 26
210 views
Which of the following cannot be a sequence of keys compared during a binary search for some target key?$500,200,450,180$ $500,450,200,180$ $180,500,200,450$ $180,200,500...
2 2 votes
2 2 answers
224
224 views
GO Classes asked Aug 12
224 views
Suppose Binary Search is used in Insertion Sort to locate where the $i$th element should be inserted among the first $i-1$ elements.What is the worst-case running time of...
2 2 votes
1 1 answer
235
235 views
GO Classes asked Aug 4
235 views
A sorted table contains $2000$ distinct elements in increasing order. A key is searched using binary search, and it is guaranteed that the key exists in the table.What is...
5 5 votes
2 2 answers
1.0k
1.0k views
gatecse asked Feb 23
1,003 views
Let A be a sorted array containing $1000$ distinct integers. You perform a recursive binary search on $\text{A}$ to find an element $\text{y}$. Suppose each comparison ch...