The Gateway to Computer Science Excellence
First time here? Checkout the FAQ!
+40 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)$

asked in Algorithms by Veteran (52.1k points) | 6.8k views
Find the first index ,say $i$ and last index , say $j$ of the element in array using using binary Search in $O(log(n))$ time and then $j-i+1$ is equal to the occurence of the element in array.

Now check if it is greater than $n/2$ or not.
this ques is purely based on how efficiently you read the question.

@ mcjoshi

if array is if 1233346 , will log n complexity works?



once we got the element which should have occurred more that n/2 times we count its total occurrences in O(logn) time.

this is what  given as the best answer

so in your example number 3 is found at n/2 and then we count its occurence which will take log n and then declare that it doesnt occure more than n/2

though how that count takes O(log n) is not clear to me

i think becasue it can be maximum upto n/2 times only so logn

We can proceed the algo like this....1st we have to check for the presence of the requested element in log n time ..then we have to chech in the either sides of that array index that we get in the first attemt to check whether that same element is present in either of the list or not...where either of those recursive cases it takes log n time each time complexity required to check those sub arrays are log n + log n + log asymptotically it takes theta (log n) time...and here we can make use of static var which can counts the number of times we get the examined element in that array ...and finally we can compare that value with n/2 ...and if its value is more than that then we return 1 otherwise we return 0....

5 Answers

+41 votes
Best answer

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.

answered by Boss (30.5k points)
edited by
Thanks for valuable reference and help. I was not thinking properly.
But sir we know that if a number has to present and occur more than n/2 it must have to present at middle position. And the given array is sorted so why we can't just directly go to the middle position and check for the number there and it's neighbor. In this way it should be O(1)


Let us take an ex -  1 1 2 2 3 3 3. So looking at 2 can we it is present n/2 times.  Look top comments you will get idea. 


Chhotu  @Vs

Why are we searching for x in the middle position ?

As per question,If an integer appears more than n/2 times,so does this mean our function will take integer and check if it is occurring more than n/2. Or does this mean we need to check if there exists such integer?I interpreted as the former way but the answer assume as per later way.Please clear this

here binary search will be apply on both side of the middle element to count the >=(n/2) occurence

so time will be = 0(1) + 2(logn)           {0(1) for finding middle element}

                        =0(1) + 0(logn)

Question is about minimum number of comparisons right? so why are we not considering 1st element it self for getting best results....means by checking occurrences of 1st element it will take only O(1) ans should be O(1) ....
this give O(n) not O(logn)
In question they haven't asked the count of the i think answer is constamt time...

What is the meaning of option (c) log* n ?


Sir doesn't minimum number of comparisons mean that we have to consider the best case.In best case  we have to check the middle element,both the first and last element.Suppose  the element is present in last index then we simply need to check the element before the middle element to see if the element is occurring more than n/2 or not.So the best case number of comparisons should be,omega(1).correct me if I am wrong.

+33 votes

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


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

answered by Boss (13.3k points)
its An integer not The integer, so we should determine first what is the Integer $x$
+30 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.


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.
answered by Boss (23.4k points)
indeed the best one.
easyly understandable.
Why are we determining position of x.?Isn't x input to the algorithm?
Best Answer. ++ Vote
+2 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.

answered by Active (3.5k points)
0 votes
answered by Junior (585 points)

Related questions

Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true
49,807 questions
54,504 answers
74,885 users