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 arrayInput: arr[] = [1, 1, 2, 4, 4, 4, 6, 6]
Output: false
Explanation: There is no majority in the array.
Table of Content
[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.
#include <bits/stdc++.h>
using namespace std;
// Function to check whether
// middle element is majority element
bool isMajority(vector<int>& arr) {
int n = arr.size();
// middle element
int x = arr[n / 2];
int cnt = 0;
// count frequency of middle element
for (int val : arr) {
if (val == x)
cnt++;
}
return cnt > n / 2;
}
int main() {
vector<int> arr = {1, 2, 3, 3, 3, 3, 10};
if (isMajority(arr))
cout << "true";
else
cout << "false";
return 0;
}
class GFG {
// Function to check whether
// middle element is majority element
static boolean isMajority(int[] arr)
{
int n = arr.length;
// middle element
int x = arr[n / 2];
int cnt = 0;
// count frequency of middle element
for (int val : arr) {
if (val == x)
cnt++;
}
return cnt > n / 2;
}
public static void main(String[] args)
{
int[] arr = { 1, 2, 3, 3, 3, 3, 10 };
if (isMajority(arr))
System.out.print("true");
else
System.out.print("false");
}
}
# Function to check whether
# middle element is majority element
def isMajority(arr):
n = len(arr)
# middle element
x = arr[n // 2]
cnt = 0
# count frequency of middle element
for val in arr:
if val == x:
cnt += 1
return cnt > n // 2
if __name__ == "__main__":
arr = [1, 2, 3, 3, 3, 3, 10]
if isMajority(arr):
print("true")
else:
print("false")
using System;
class GFG {
// Function to check whether
// middle element is majority element
static bool isMajority(int[] arr) {
int n = arr.Length;
// middle element
int x = arr[n / 2];
int cnt = 0;
// count frequency of middle element
foreach (int val in arr) {
if (val == x)
cnt++;
}
return cnt > n / 2;
}
static void Main() {
int[] arr = {1, 2, 3, 3, 3, 3, 10};
if (isMajority(arr))
Console.Write("true");
else
Console.Write("false");
}
}
// Function to check whether
// middle element is majority element
function isMajority(arr) {
let n = arr.length;
// middle element
let x = arr[Math.floor(n / 2)];
let cnt = 0;
// count frequency of middle element
for (let val of arr) {
if (val === x)
cnt++;
}
return cnt > Math.floor(n / 2);
}
let arr = [1, 2, 3, 3, 3, 3, 10];
if (isMajority(arr))
console.log("true");
else
console.log("false");
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
#include <bits/stdc++.h>
using namespace std;
// Function to find first occurrence
int firstOccurrence(vector<int> &arr,
int low, int high, int x) {
while (low <= high) {
int mid = low + (high - low) / 2;
// check if mid is first occurrence
if (arr[mid] == x &&
(mid == 0 || arr[mid - 1] < x)) {
return mid;
}
// move left
if (arr[mid] >= x)
high = mid - 1;
// move right
else
low = mid + 1;
}
return -1;
}
// Function to check whether
// middle element is majority element
bool isMajority(vector<int> &arr) {
int n = arr.size();
// middle element
int x = arr[n / 2];
// search first occurrence
int i = firstOccurrence(arr, 0, n / 2, x);
// if not found
if (i == -1)
return false;
// check majority condition
return (i + n / 2 < n &&
arr[i + n / 2] == x);
}
int main() {
vector<int> arr = {1, 2, 3, 3, 3, 3, 10};
if (isMajority(arr))
cout << "true";
else
cout << "false";
return 0;
}
class GFG {
// Function to find first occurrence
static int firstOccurrence(int[] arr,
int low, int high, int x) {
while (low <= high) {
int mid = low + (high - low) / 2;
// check if mid is first occurrence
if (arr[mid] == x &&
(mid == 0 || arr[mid - 1] < x)) {
return mid;
}
// move left
if (arr[mid] >= x)
high = mid - 1;
// move right
else
low = mid + 1;
}
return -1;
}
// Function to check whether
// middle element is majority element
static boolean isMajority(int[] arr) {
int n = arr.length;
// middle element
int x = arr[n / 2];
// search first occurrence
int i = firstOccurrence(arr, 0, n / 2, x);
// if not found
if (i == -1)
return false;
// check majority condition
return (i + n / 2 < n &&
arr[i + n / 2] == x);
}
public static void main(String[] args) {
int[] arr = {1, 2, 3, 3, 3, 3, 10};
if (isMajority(arr))
System.out.print("true");
else
System.out.print("false");
}
}
# Function to find first occurrence
def firstOccurrence(arr, low, high, x):
while low <= high:
mid = low + (high - low) // 2
# check if mid is first occurrence
if arr[mid] == x and \
(mid == 0 or arr[mid - 1] < x):
return mid
# move left
if arr[mid] >= x:
high = mid - 1
# move right
else:
low = mid + 1
return -1
# Function to check whether
# middle element is majority element
def isMajority(arr):
n = len(arr)
# middle element
x = arr[n // 2]
# search first occurrence
i = firstOccurrence(arr, 0, n // 2, x)
# if not found
if i == -1:
return False
# check majority condition
return (i + n // 2 < n and
arr[i + n // 2] == x)
if __name__ == "__main__":
arr = [1, 2, 3, 3, 3, 3, 10]
if isMajority(arr):
print("true")
else:
print("false")
using System;
class GFG {
// Function to find first occurrence
static int firstOccurrence(int[] arr,
int low, int high, int x) {
while (low <= high) {
int mid = low + (high - low) / 2;
// check if mid is first occurrence
if (arr[mid] == x &&
(mid == 0 || arr[mid - 1] < x)) {
return mid;
}
// move left
if (arr[mid] >= x)
high = mid - 1;
// move right
else
low = mid + 1;
}
return -1;
}
// Function to check whether
// middle element is majority element
static bool isMajority(int[] arr) {
int n = arr.Length;
// middle element
int x = arr[n / 2];
// search first occurrence
int i = firstOccurrence(arr, 0, n / 2, x);
// if not found
if (i == -1)
return false;
// check majority condition
return (i + n / 2 < n &&
arr[i + n / 2] == x);
}
static void Main() {
int[] arr = {1, 2, 3, 3, 3, 3, 10};
if (isMajority(arr))
Console.Write("true");
else
Console.Write("false");
}
}
// Function to find first occurrence
function firstOccurrence(arr, low, high, x) {
while (low <= high) {
let mid = low + Math.floor((high - low) / 2);
// check if mid is first occurrence
if (arr[mid] === x &&
(mid === 0 || arr[mid - 1] < x)) {
return mid;
}
// move left
if (arr[mid] >= x)
high = mid - 1;
// move right
else
low = mid + 1;
}
return -1;
}
// Function to check whether
// middle element is majority element
function isMajority(arr) {
let n = arr.length;
// middle element
let x = arr[Math.floor(n / 2)];
// search first occurrence
let i = firstOccurrence(arr, 0,
Math.floor(n / 2), x);
// if not found
if (i === -1)
return false;
// check majority condition
return (i + Math.floor(n / 2) < n &&
arr[i + Math.floor(n / 2)] === x);
}
// Drive code
let arr = [1, 2, 3, 3, 3, 3, 10];
if (isMajority(arr))
console.log("true");
else
console.log("false");
Output
true