• edited by
55,307 views
101 101 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)$

9 Answers

Best answer
106 106 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
97 97 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.
63 63 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

4 4 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.

3 3 votes

Since it is a sorted array, we can use binary search to identify the position of the first occurrence of given integer in  $log(n)$. 

now if integer (appear more than once)repeats, its appearance has to be continuous because array is sorted.

So this is a clear case for asking and applying Binary search.

Answer:
Position:
Show:

Related questions

74 74 votes
4 answers 4 answers
35.6k
35.6k views
Kathleen asked Sep 12, 2014
35,645 views
Which of the following are NOT true in a pipelined processor?Bypassing can handle all RAW hazardsRegister renaming can eliminate all register carried WAR hazardsControl h...
43 43 votes
6 answers 6 answers
21.9k
21.9k views
go_editor asked Apr 23, 2016
21,852 views
Consider the following C functions:int f1 (int n) { if(n == 0 || n == 1) return n; else return (2 * f1(n-1) + 3 * f1(n-2)); } int f2(int n) { int i; int X[N], Y[N], Z[N];...
51 51 votes
7 answers 7 answers
30.2k
30.2k views
Kathleen asked Sep 12, 2014
30,245 views
Consider the following C functions:int f1 (int n) { if(n == 0 || n == 1) return n; else return (2 * f1(n-1) + 3 * f1(n-2)); } int f2(int n) { int i; int X[N], Y[N], Z[N];...
70 70 votes
9 answers 9 answers
34.2k
34.2k views
Kathleen asked Sep 12, 2014
34,152 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(...