• retagged by
1,057 views
0 0 votes
We have a run of 0's followed by the run of 1's and we have to find the point where first 1 will be present. We only know the start of the sequence but we have no idea about the end.

what should the best case complexity of this problem? Describe your approach.

1 Answer

Best answer
3 3 votes

It can be done in O(log n)

first we search a[2] if not equal to 1 then a[4],a[8], a[16]....

we use two variables for this one which stores a[2n-1] and another which stores a[2n]

let us say that at a[1024]=0 and a[2048]=1

now we have a low and a high now we can apply binary search

complexity

traversing in power of 2 = (log n)

binary search  = (log n)

total complexity(log n + log n)=O(log n)

• edited by
Position:
Show:

Related questions

1 1 vote
1 1 answer
195
195 views
GO Classes asked Aug 26
195 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
206
206 views
GO Classes asked Aug 12
206 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
226
226 views
GO Classes asked Aug 4
226 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...
0 0 votes
2 2 answers
1.9k
1.9k views
dhruba asked Jun 5, 2023
1,899 views
Binary search is performed on a sorted array of n elements. The search key is not in the array and falls between the elements at positions m and m+1 (where 1 ≤ m < n). Ho...