Open In App

Check for Majority Element in a sorted array

Last Updated : 23 Jul, 2025
Comments
Improve
Suggest changes
46 Likes
Like
Report

Given an array arr of N elements,  A majority element in an array arr of size N is an element that appears more than N/2 times in the array. The task is to write a function say isMajority() that takes an array (arr[] ), array’s size (n) and a number to be searched (x) as parameters and returns true if x is a majority element (present more than n/2 times).

Examples: 

Input: arr[] = {1, 2, 3, 3, 3, 3, 10}, x = 3
Output: True (x appears more than n/2 times in the given array)
Input: arr[] = {1, 1, 2, 4, 4, 4, 6, 6}, x = 4
Output: False (x doesn't appear more than n/2 times in the given array)
Input: arr[] = {1, 1, 1, 2, 2}, x = 1
Output: True (x appears more than n/2 times in the given array)

METHOD 1 (Using Linear Search): Linearly search for the first occurrence of the element, once you find it (let at index i), check the element at index i + n/2. If the element is present at i+n/2 then return 1 else return 0.

C++
C Java Python3 C# JavaScript PHP

Output
4 appears more than 3 times in arr[]

Time Complexity: O(n)
Auxiliary Space: O(1)

METHOD 2 (Using Binary Search): Use binary search methodology to find the first occurrence of the given number. The criteria for binary search is important here. 

C++
C Java Python3 C# JavaScript

Output
3 appears more than 3 times in arr[]

Time Complexity: O(log n)
Auxiliary Space: O(1)

Algorithmic Paradigm: Divide and Conquer


Similar Reads