VOOZH about

URL: https://www.geeksforgeeks.org/dsa/check-for-majority-element-in-a-sorted-array/

⇱ Check for Majority in a Sorted Array - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Check for Majority in a Sorted Array

Last Updated : 19 May, 2026

Given a sorted array arr[] of size n, find if there is a majority element in the array or not. An element is called a majority element if it appears more than n/2 times in the array.

Examples:

Input: arr[] = [1, 2, 3, 3, 3, 3, 10]
Output: true
Explanation: 3 is majority in the array

Input: arr[] = [1, 1, 2, 4, 4, 4, 6, 6]
Output: false
Explanation: There is no majority in the array.

[Naive Approach] Linearly Count Middle - O(n) Time and O(1) Space

The idea is based on the property that if there is a majority in a sorted array, then it must be present at the mid index. So, we first take the middle element arr[n/2] and count how many times it appears in the array. If its count is greater than n/2, then it is a majority element; otherwise, it is not.


Output
true

[Expected Approach] Binary Search - O(log n) Time and O(1) Space

We first take the middle element x = arr[n/2] and use Binary Search to find its first occurrence. Let the first occurrence index be i. If x is a majority element, then the element at index i + n/2 must also be equal to x. Otherwise, x cannot appear more than n/2 times.

  • Find the middle element x = arr[n/2]
  • Apply binary search to find the first occurrence of x
  • If x is not found, return false
  • Let i be the first occurrence index
  • Check if: i + n/2 < n && arr[i + n/2] == x
  • If true, return true, otherwise return false

Consider: arr[] = [1, 2, 3, 3, 3, 3, 10]

Middle element: x = arr[7/2] = arr[3] = 3

Step 1: Find First Occurrence of x

Initial range: low = 0, high = 3

Iteration 1

  • mid = (0 + 3) / 2 = 1
  • arr[mid] = 2
  • Since arr[mid] < x, move right: low = mid + 1 = 2

Iteration 2

  • mid = (2 + 3) / 2 = 2
  • arr[mid] = 3
  • Check first occurrence: arr[mid - 1] = arr[1] = 2 < 3
  • So, first occurrence is found at index: i = 2

Step 2: Check Majority Condition

  • i + n/2 = 2 + 3 = 5
  • arr[5] = 3
  • Since arr[i + n/2] == x, the middle element appears more than n/2 times.

Final Result: true


Output
true
Comment