0

Is it possible to go through a boolean array to find a false value in O(logn) running time? The array's indices run from 0 to n-1.

If it is, how would we do it in java? Pseudo code is fine.

Vimzy
  • 1,871
  • 8
  • 30
  • 56

3 Answers3

1

No. This is not possible without any further information about the array.

The best you can do is O(n), which is traversing the array from left to right and checking each item.

If the array is sorted you would need two checks, a false value could be on either end, which is O(1).

A common proof by contradiction:

Assume that the algorithm works correctly. Then, after inspecting less than all elements, the algorithm yields a correct answer. Now assume the algorithm has seen only trues, it yields an answer x. By now only changing values the algorithm has not inspected, I can always construct a test case in which the algorithm fails. Therefore, the algorithm must inspect all elements of an (unsorted) boolean array to determine if all are true.

ThreeFx
  • 7,250
  • 1
  • 27
  • 51
1

In general, the answer is "NO": you cannot go through an array in search of a single value in less than O(N), unless you know something about the order of array items.

For example, if the array is sorted, you can find the right spot in O(log N).

For boolean array being sorted means having all falses, if any, at the beginning, and all trues, if any, at the the end. If this is the case, you can use binary search to find the "demarcation point" in logarithmic time.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • 1
    Nit: for a binary sorted array it would just be checking the first and last elements, or O(1). – user2864740 Oct 06 '14 at 20:13
  • It is sorted. It runs from 0 to n-1. – Vimzy Oct 06 '14 at 20:14
  • @Vimzy Those are the indices of the array. Sortedness involves the values in the array. –  Oct 06 '14 at 20:15
  • @Rhymoid Right, sorry, brain fart. Additionally, we know that there is only one false value in the array as well. All the others are true. Any way? – Vimzy Oct 06 '14 at 20:16
  • @vimzy No other than looking at every element, seeing if that one false value is there. –  Oct 06 '14 at 20:17
  • @Vimzy If there's only one element that is `false`, then linear search is the only choice, unfortunately. The reason the binary search works is that by probing one element you can get information about many other elements. If there's only one `false` in the array, there is not much you can find about its location by probing the remaining elements. – Sergey Kalinichenko Oct 06 '14 at 20:20
-1

Whatever you do, you will have to check all the values to ensure there is no false value. So linear time, with n/2 checks in average.

Nilexys
  • 633
  • 1
  • 5
  • 10
  • This answer makes assumptions about the distribution of the values in the boolean array, which does more harm than good. Moreover, assuming independent variables with a probability of 0.5, you have to inspect way less than n/2 elements in average. – ThreeFx Oct 30 '16 at 22:23