Open In App
Related Articles

GATE | GATE CS 2008 | Question 40

Improve Article
Improve
Save Article
Save
Like Article
Like

The minimum number of comparisons required to determine if an integer appears more than n/2 times in a sorted array of n integers is

(A)

Θ(n)

(B)

Θ(logn)

(C)

Θ(n*logn)

(D)

Θ(1)



Answer: (B)

Explanation:

If you answered Theta(1), then think of examples {1, 2, 2, 2, 4, 4}, {1, 1, 2, 2, 2, 2, 3, 3} The Best way to find out whether an integer appears more than n/2 times in a sorted array(Ascending Order) of n integers, would be binary search approach.

  1. The First occurrence of an element can be found out in O(log(n)) time using divide and conquer technique,lets say it is i.
  2. The Last occurrence of an element can be found out in O(log(n)) time using divide and conquer technique,lets say it is j.
  3. Now number of occurrence of that element(count) is (j-i+1). Overall time complexity = log n +log n +1 = O(logn)

See Check for Majority Element in a sorted array This solution is contributed by Nirmal Bharadwaj


Quiz of this Question
Please comment below if you find anything wrong in the above post

Level Up Your GATE Prep!
Embark on a transformative journey towards GATE success by choosing Data Science & AI as your second paper choice with our specialized course. If you find yourself lost in the vast landscape of the GATE syllabus, our program is the compass you need.

Last Updated : 02 Nov, 2022
Like Article
Save Article
Previous
Next
Similar Reads