in Algorithms retagged by
300 views
2 votes
2 votes

.........

in Algorithms retagged by
300 views

1 Answer

1 vote
1 vote
Best answer

The best way to solve such a problem is by using Binary Search. Search the sorted array by repeatedly dividing the search interval in half. Begin with an interval covering the whole array. If the value of the search key is less than the item in the middle of the interval, narrow the interval to the lower half. Otherwise, narrow it to the upper half.

Find mid element

  • Is mid = 1 ?
  • Is mid >1?(not possible here)
  • Is mid < 1 ?

Proceed accordingly, the Worst case of this problem will be 1 at the end of the array i.e 00000…..1 OR 1…….0000. It will take log n time worst case.
n=31, Hence log 231 = 5.

Therefore, answer will be 5.

selected by