You are not logged in. Please login at to post your questions!


What is the best solution to this problem

. You are given a file which contains a very bit sequence of 0 and 1 and it is sorted. Hence all the zeros are in front of the ones.One needs to find the first orrcurance of of 1 in the file(return the position).

The only method to access the file is through a method whose signature is — int getBitAtPosition(int position) — which returns the bit at the specified position in the file.

I thought of using a binary search till the 1 is not found ....once it was found we can keep on decreasing the position linearly and saving the previous value so that if the bit at pos-- is 0 then we can get the position as pos

asked 18 Jan '15, 12:36

nickzuck_007's gravatar image

accept rate: 14%

If you do a linear traversal after binary search you can still end up having the time complexity to be be of linear order. But if you do a lower bound binary search you can achieve a time complexity of log(n).

Check the below solution it will have a time complexity of log(n) even in the worst case



answered 28 Nov '17, 23:59

athulpai's gravatar image

accept rate: 0%

toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here



Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text]( "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported
  • mathemetical formulas in Latex between $ symbol

Question tags:


question asked: 18 Jan '15, 12:36

question was seen: 1,642 times

last updated: 28 Nov '17, 23:59