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

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

Lower_bound