2

I have an array which only has values 0 and 1. They are stored separately in the array. For example, the array may have first 40% as 0 and remaining 60% as 1. I want to find out the split point between 0 and 1. One algorithm I have in mind is binary search. Since performance is important for me, not sure if binary search could give me the best performance. The split point is randomly distributed. The array is given in the format of 0s and 1s splitted.

6
  • 5
    Why not just keep a count of the number of zeroes and the number of ones, rather than sorting and searching the array? Commented Nov 12, 2015 at 20:26
  • 2
    If all you have is an array for storage, binary search is the fastest you can get. Why would you think that is too slow? Commented Nov 12, 2015 at 20:28
  • 1
    If the position of the split point is distributed randomly (i.e. the probability of getting a 40/60 split is the same as getting a 1/99, 99/1, 50/50 etc. split) then binary search is the fastest way to solve this. Commented Nov 12, 2015 at 20:29
  • Hey @Fuser97381, what if you are given the array? This approach becomes sub optimal. Please see my answer. Commented Nov 12, 2015 at 20:34
  • 1
    This sounds like a bad implementation to begin with. Why even have such an array? You can easily replace it with 2 integers (zeroCount & oneCount). There's nothing you can use that array for that you can't do with these 2 counters. Commented Nov 12, 2015 at 20:44

1 Answer 1

12

The seemingly clever answer of keeping the counts doesn't hold when you are given the array.

Counting is O(n), and so is linear search. Thus, counting is not optimal!

Binary search is your friend, and can get things done in O(lg n) time, which as you may know is way better.

Of course, if you have to process the array anyways (reading from a file, user input etc.), make use of that time to just count the number of 1s and 0s and be done with it (you don't even have to store any of it, just keep the counts).

To drive the point home, if you are writing a library, which has a function called getFirstOneIndex(sortZeroesOnesArr: Array[Integer]): Integer that takes a sorted array of ones and zeroes and returns the position of the first 1, do not count, binary search.

Sign up to request clarification or add additional context in comments.

1 Comment

Unless the array in question is from an external source, whatever code generated the array could have, with no loss of information and with no wasted memory, kept the counts. It sounds like a classic XY problem. Still -- your answer is a good one so I'll upvote it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.