Skip to content
Official PYQ · 2008

GATE CS 2008 Q40 — Asymptotic Worst Case Time and Space Complexity

Explain with AI

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

Choose one option
View worked solution

Answer: B

To determine if any integer appears more than n2\frac{n}{2} times in a sorted array AA of size nn, we can use the following approach:

  1. Identify the Candidate: If an element appears more than n2\frac{n}{2} times in a sorted array, it must occupy the middle position of the array. Let this candidate element be x=A[⌊n/2⌋]x = A[\lfloor n/2 \rfloor].
  2. Find the Boundaries: To check if xx actually appears more than n2\frac{n}{2} times, we need to find its first occurrence (index LL) and its last occurrence (index RR) in the array.
  3. Binary Search: Since the array is sorted, we can find both LL and RR using binary search. Each binary search takes Θ(log⁡n)\Theta(\log n) comparisons.
  4. Verify the Count: If (R−L+1)>n2(R - L + 1) > \frac{n}{2}, then xx is the majority element. Otherwise, no such element exists.

Since binary search is optimal for searching in a sorted array, the minimum number of comparisons required in the worst case is Θ(log⁡n)\Theta(\log n).

Free practice · Your answers on this page are not saved.

Published
Report a correction
Keep exploringMore Asymptotic Worst Case Time and Space Complexity questions