retagged by
547 views
3 votes
3 votes

You are given a bit-array $A[1 \ldots n]$ (i.e., $A[i] \in\{0,1\}$ for each $i$ ) and told that this is a "$0$ -to-$1$” bit-array. This means that for some (unknown) index $1 \leq j\lt n, A[1], \ldots, A[j]$ are all $0$ 's and $A[j+1], \ldots, A[n]$ are all $1$ 's. The index $j$ for such an array is called the transition index. We design a divide-and-conquer algorithm for finding the transition index for a given $0$ -to- $1$ bit-array. The input to the algorithm is an array $A$ and the size $n$ of the array $A$.

What will be the worst-case time complexity of the best divide-and-conquer algorithm? 

  1. $\Theta(\log n)$
  2. $\Theta(n \log n)$
  3. $\Theta(n)$
  4. $\Theta\left(n^{2}\right)$
retagged by

1 Answer

7 votes
7 votes
As it is a divide and conquer approach, the first thought of applying binary search comes to mind and definitely it works here,

let me explain.

Consider an array of having 0’s followed by 1’s and we apply binary search over this, after finding the middle index just check its previous index value and its next index value.

if both are 0 then move to upper half of sub array

if both are 1 then move to lower half of sub array

and finally if previous is 0 and next is 1 that the transition index

So the overall process is same as binary search hence its time complexity is theta(log n)  Hence the correct option is A
Answer:

Related questions

5 votes
5 votes
1 answer
4