edited by
36,300 views
79 votes
79 votes

The minimum number of comparisons required to determine if an integer appears more than $\frac{n}{2}$ times in a sorted array of $n$ integers is

  1. $\Theta(n)$
  2. $\Theta(\log n)$
  3. $\Theta(\log^*n)$
  4. $\Theta(1)$
edited by

7 Answers

Best answer
73 votes
73 votes

Answer is option B.

whenever there exists an element which is present in the array : more than $\frac{n}{2}$ times, then definitely it will be present at the middle index position; in addition to that it will also be present at anyone of the neighbourhood indices namely $i-1$ and $i+1$ 
No matter how we push that stream of More than $\frac{n}{2}$ times of elements of same value around the Sorted Array, it is bound to be present at the middle index $+$ atleast anyone of its neighbourhood

once we got the element which should have occurred more that $n/2$ times we count its total occurrences in $\mathcal{O}(\log n)$ time.

edited by
70 votes
70 votes
The starting position of required element can be anywhere between

1 to n/2-1(inclusive).

We have to search that elements starting index to do that Modified Binary Search.

Step1--ELE=A[n/2]

Step2:--Search for starting index of ELE with in range of 1 to n/2-1 which is sorted array. O(logn) time cost of modified binary search.

Now after find such index say i Now see if A[i] ==A[ i+ n/2 ] then it is the majority Ele (Cz Sorted array) else no majority Ele present. Above done in O(1).

So overall time complexity O(logn)+O(1)=O(logn)

Hence Option B is Ans.
54 votes
54 votes

To check whether a given number is repeated n/2 times in the array can be done in O(log n) time.

Algo

1. find the first occurrence (index i) of x(given number) in the array which can be done in O(log n) time (a variant of binary search).

2. check if A[i] == A[n/2+i]

         return true

3. else return false

3 votes
3 votes

Fool proof way to find is, to perform 2 binary searches, one for n-1 and other for n+1.

Even if n-1 and n+1 are not present, we can modify BST to return the index where min,max are equal.

Then check the difference between two resuls, if greater than n/2.

Hence 2logn comparisons required and answer is B.

Answer:

Related questions

41 votes
41 votes
7 answers
3
Kathleen asked Sep 12, 2014
21,497 views
We have a binary heap on $n$ elements and wish to insert $n$ more elements (not necessarily one after another) into this heap. The total time required for this is$\Theta(...
28 votes
28 votes
6 answers
4
Kathleen asked Sep 11, 2014
13,235 views
The most efficient algorithm for finding the number of connected components in an undirected graph on $n$ vertices and $m$ edges has time complexity$\Theta(n)$$\Theta(m)$...