edited by
1,420 views
12 votes
12 votes
You are given a array $A$ of size $n$. Your are told that $A$ comprises three consecutive runs - first a run of $a$'s, then a run of $b$'s and finally a run of $c$'s. Moreover, you are provided an index of $i$ such that $A[i] = b$. Design an $O(\log n)$ time algorithm to determine the number of $b$'s (i.e., length of the second run) in $A$.
edited by

2 Answers

Best answer
18 votes
18 votes

Hint: Modify Binary Search.

Solution:

You should use binary search twice first to find the first occurrence of $b$. Then apply it again to find the last occurrence.

The following code does this.

BinarySearchForFirst


int binarySearchForFirst(int l, int r){
    int mid;
    while(l <= r){
        mid = l+(r-l)/2;
        if(array[mid]=='b' && array[mid-1]=='a' ){//the desired case
            return mid;
        }
        if(array[mid-1]=='b'){//we're to the right of required index
            r = mid;
            continue;
        }
        if(array[mid]=='a'){//we're to the left or required index
            l = mid;
            continue;
        }
    }
    
}


This returns the first index say $i$ 

Similarly find the second index $j$.

Then the answer is $j-i+1$

edited by
14 votes
14 votes

The array looks something like this.
a.......a b...........b c.......c

We are provided an index of i such that A[i] = b
But that is which b ? Is that the first b ? Is that the last b ? Is that the middle b ? We do not know.

So we are given index to  'b'
Now do binary search in it's left & right side for 'b'

In left side binary search, if you found 'a' then move right, else move left .
In this way you can find the first b.

Similarly in right side of index i,  up to index n, do a binary search for b,  
if you found c, then move left otherwise move right.
In this way you can find the last b.

(You know how we do binary search by finding mid which is (high+low)/2  )


Now subtract the index of first b from last b. Thats it.

Related questions

12 votes
12 votes
1 answer
1
Devasish Ghosh asked Mar 9, 2017
1,389 views
Design a context free grammar for the language consisting of all strings over $\mathbf{\{a,b\}}$ that are not the form $\mathbf{ww}$ for any string $\mathbf{w}$
3 votes
3 votes
1 answer
2
Devasish Ghosh asked Mar 4, 2017
526 views
Find all real solutions of the equation $x^{2} - |x-1| - 3 = 0$
13 votes
13 votes
2 answers
4
neha.i asked May 11, 2017
2,840 views
Suppose $X$ is distributed as Poisson with mean $λ.$ Then $E(1/(X + 1))$ is$\frac{e^{\lambda }-1}{\lambda }$$\frac{e^{\lambda }-1}{\lambda +1}$$\frac{1-e^{-\lambda }}{\l...