Check for Majority Element in a sorted array
Last Updated :
21 Dec, 2023
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++
#include<bits/stdc++.h>
using namespace std;
bool isMajority( int arr[], int n, int x)
{
int i;
int last_index = n % 2 ? (n / 2 + 1): (n / 2);
for (i = 0; i < last_index; i++)
{
if (arr[i] == x && arr[i + n / 2] == x)
return 1;
}
return 0;
}
int main()
{
int arr[] ={1, 2, 3, 4, 4, 4, 4};
int n = sizeof (arr)/ sizeof (arr[0]);
int x = 4;
if (isMajority(arr, n, x))
cout << x << " appears more than " <<
n/2 << " times in arr[]" << endl;
else
cout <<x << " does not appear more than" << n/2 << " times in arr[]" << endl;
return 0;
}
|
C
# include <stdio.h>
# include <stdbool.h>
bool isMajority( int arr[], int n, int x)
{
int i;
int last_index = n%2? (n/2+1): (n/2);
for (i = 0; i < last_index; i++)
{
if (arr[i] == x && arr[i+n/2] == x)
return 1;
}
return 0;
}
int main()
{
int arr[] ={1, 2, 3, 4, 4, 4, 4};
int n = sizeof (arr)/ sizeof (arr[0]);
int x = 4;
if (isMajority(arr, n, x))
printf ( "%d appears more than %d times in arr[]" ,
x, n/2);
else
printf ( "%d does not appear more than %d times in arr[]" ,
x, n/2);
return 0;
}
|
Java
import java.io.*;
class Majority {
static boolean isMajority( int arr[], int n, int x)
{
int i, last_index = 0 ;
last_index = (n% 2 == 0 )? n/ 2 : n/ 2 + 1 ;
for (i = 0 ; i < last_index; i++)
{
if (arr[i] == x && arr[i+n/ 2 ] == x)
return true ;
}
return false ;
}
public static void main (String[] args) {
int arr[] = { 1 , 2 , 3 , 4 , 4 , 4 , 4 };
int n = arr.length;
int x = 4 ;
if (isMajority(arr, n, x)== true )
System.out.println(x+ " appears more than " +
n/ 2 + " times in arr[]" );
else
System.out.println(x+ " does not appear more than " +
n/ 2 + " times in arr[]" );
}
}
|
Python3
def isMajority(arr, n, x):
last_index = (n / / 2 + 1 ) if n % 2 ! = 0 else (n / / 2 )
for i in range (last_index):
if arr[i] = = x and arr[i + n / / 2 ] = = x:
return 1
arr = [ 1 , 2 , 3 , 4 , 4 , 4 , 4 ]
n = len (arr)
x = 4
if (isMajority(arr, n, x)):
print ( "% d appears more than % d times in arr[]"
% (x, n / / 2 ))
else :
print ( "% d does not appear more than % d times in arr[]"
% (x, n / / 2 ))
|
C#
using System;
class GFG {
static bool isMajority( int [] arr,
int n, int x)
{
int i, last_index = 0;
last_index = (n % 2 == 0) ? n / 2 :
n / 2 + 1;
for (i = 0; i < last_index; i++) {
if (arr[i] == x && arr[i + n / 2] == x)
return true ;
}
return false ;
}
public static void Main()
{
int [] arr = { 1, 2, 3, 4, 4, 4, 4 };
int n = arr.Length;
int x = 4;
if (isMajority(arr, n, x) == true )
Console.Write(x + " appears more than " +
n / 2 + " times in arr[]" );
else
Console.Write(x + " does not appear more than " +
n / 2 + " times in arr[]" );
}
}
|
Javascript
<script>
function isMajority(arr, n, x)
{
let i, last_index = 0;
last_index = (n % 2 == 0) ?
parseInt(n / 2, 10) : parseInt(n / 2, 10) + 1;
for (i = 0; i < last_index; i++) {
if (arr[i] == x && arr[i +
parseInt(n / 2, 10)] == x)
return true ;
}
return false ;
}
let arr = [ 1, 2, 3, 4, 4, 4, 4 ];
let n = arr.length;
let x = 4;
if (isMajority(arr, n, x) == true )
document.write(x + " appears more than " +
parseInt(n / 2, 10) + " times in arr[]" );
else
document.write(x + " does not appear more than " +
parseInt(n / 2, 10) + " times in arr[]" );
</script>
|
PHP
<?php
function isMajority( $arr , $n , $x )
{
$i ;
$last_index = $n % 2? ( $n / 2 + 1): ( $n / 2);
for ( $i = 0; $i < $last_index ; $i ++)
{
if ( $arr [ $i ] == $x && $arr [ $i + $n / 2] == $x )
return 1;
}
return 0;
}
$arr = array (1, 2, 3, 4, 4, 4, 4);
$n = sizeof( $arr );
$x = 4;
if (isMajority( $arr , $n , $x ))
echo $x , " appears more than "
, floor ( $n / 2), " times in arr[]" ;
else
echo $x , "does not appear more than "
, floor ( $n / 2), "times in arr[]" ;
?>
|
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++
#include<bits/stdc++.h>
using namespace std;
int _binarySearch( int arr[], int low,
int high, int x);
bool isMajority( int arr[], int n, int x)
{
int i = _binarySearch(arr, 0, n - 1, x);
if (i == -1)
return false ;
if (((i + n / 2) <= (n - 1)) &&
arr[i + n / 2] == x)
return true ;
else
return false ;
}
int _binarySearch( int arr[], int low,
int high, int x)
{
if (high >= low)
{
int mid = (low + high)/2;
if ((mid == 0 || x > arr[mid - 1]) &&
(arr[mid] == x) )
return mid;
else if (x > arr[mid])
return _binarySearch(arr, (mid + 1),
high, x);
else
return _binarySearch(arr, low,
(mid - 1), x);
}
return -1;
}
int main()
{
int arr[] = { 1, 2, 3, 3, 3, 3, 10 };
int n = sizeof (arr) / sizeof (arr[0]);
int x = 3;
if (isMajority(arr, n, x))
cout << x << " appears more than "
<< n / 2 << " times in arr[]"
<< endl;
else
cout << x << " does not appear more than"
<< n / 2 << " times in arr[]" << endl;
return 0;
}
|
C
# include <stdio.h>
# include <stdbool.h>
int _binarySearch( int arr[], int low, int high, int x);
bool isMajority( int arr[], int n, int x)
{
int i = _binarySearch(arr, 0, n-1, x);
if (i == -1)
return false ;
if (((i + n/2) <= (n -1)) && arr[i + n/2] == x)
return true ;
else
return false ;
}
int _binarySearch( int arr[], int low, int high, int x)
{
if (high >= low)
{
int mid = (low + high)/2;
if ( (mid == 0 || x > arr[mid-1]) && (arr[mid] == x) )
return mid;
else if (x > arr[mid])
return _binarySearch(arr, (mid + 1), high, x);
else
return _binarySearch(arr, low, (mid -1), x);
}
return -1;
}
int main()
{
int arr[] = {1, 2, 3, 3, 3, 3, 10};
int n = sizeof (arr)/ sizeof (arr[0]);
int x = 3;
if (isMajority(arr, n, x))
printf ( "%d appears more than %d times in arr[]" ,
x, n/2);
else
printf ( "%d does not appear more than %d times in arr[]" ,
x, n/2);
return 0;
}
|
Java
import java.io.*;
class Majority {
static int _binarySearch( int arr[], int low, int high, int x)
{
if (high >= low)
{
int mid = (low + high)/ 2 ;
if ( (mid == 0 || x > arr[mid- 1 ]) && (arr[mid] == x) )
return mid;
else if (x > arr[mid])
return _binarySearch(arr, (mid + 1 ), high, x);
else
return _binarySearch(arr, low, (mid - 1 ), x);
}
return - 1 ;
}
static boolean isMajority( int arr[], int n, int x)
{
int i = _binarySearch(arr, 0 , n- 1 , x);
if (i == - 1 )
return false ;
if (((i + n/ 2 ) <= (n - 1 )) && arr[i + n/ 2 ] == x)
return true ;
else
return false ;
}
public static void main (String[] args) {
int arr[] = { 1 , 2 , 3 , 3 , 3 , 3 , 10 };
int n = arr.length;
int x = 3 ;
if (isMajority(arr, n, x)== true )
System.out.println(x + " appears more than " +
n/ 2 + " times in arr[]" );
else
System.out.println(x + " does not appear more than " +
n/ 2 + " times in arr[]" );
}
}
|
Python3
def isMajority(arr, n, x):
i = _binarySearch(arr, 0 , n - 1 , x)
if i = = - 1 :
return False
if ((i + n / / 2 ) < = (n - 1 )) and arr[i + n / / 2 ] = = x:
return True
else :
return False
def _binarySearch(arr, low, high, x):
if high > = low:
mid = (low + high) / / 2
if (mid = = 0 or x > arr[mid - 1 ]) and (arr[mid] = = x):
return mid
elif x > arr[mid]:
return _binarySearch(arr, (mid + 1 ), high, x)
else :
return _binarySearch(arr, low, (mid - 1 ), x)
return - 1
arr = [ 1 , 2 , 3 , 3 , 3 , 3 , 10 ]
n = len (arr)
x = 3
if (isMajority(arr, n, x)):
print ( "% d appears more than % d times in arr[]"
% (x, n / / 2 ))
else :
print ( "% d does not appear more than % d times in arr[]"
% (x, n / / 2 ))
|
C#
using System;
class GFG {
static int _binarySearch( int [] arr, int low,
int high, int x)
{
if (high >= low) {
int mid = (low + high) / 2;
if ((mid == 0 || x > arr[mid - 1]) &&
(arr[mid] == x))
return mid;
else if (x > arr[mid])
return _binarySearch(arr, (mid + 1),
high, x);
else
return _binarySearch(arr, low,
(mid - 1), x);
}
return -1;
}
static bool isMajority( int [] arr, int n, int x)
{
int i = _binarySearch(arr, 0, n - 1, x);
if (i == -1)
return false ;
if (((i + n / 2) <= (n - 1)) &&
arr[i + n / 2] == x)
return true ;
else
return false ;
}
public static void Main()
{
int [] arr = { 1, 2, 3, 3, 3, 3, 10 };
int n = arr.Length;
int x = 3;
if (isMajority(arr, n, x) == true )
Console.Write(x + " appears more than " +
n / 2 + " times in arr[]" );
else
Console.Write(x + " does not appear more than " +
n / 2 + " times in arr[]" );
}
}
|
Javascript
<script>
function _binarySearch(arr, low, high, x)
{
if (high >= low) {
let mid = parseInt((low + high) / 2, 10);
if ((mid == 0 || x > arr[mid - 1]) && (arr[mid] == x))
return mid;
else if (x > arr[mid])
return _binarySearch(arr, (mid + 1), high, x);
else
return _binarySearch(arr, low, (mid - 1), x);
}
return -1;
}
function isMajority(arr, n, x)
{
let i = _binarySearch(arr, 0, n - 1, x);
if (i == -1)
return false ;
if (((i + parseInt(n / 2, 10)) <= (n - 1)) && arr[i + parseInt(n / 2, 10)] == x)
return true ;
else
return false ;
}
let arr = [ 1, 2, 3, 3, 3, 3, 10 ];
let n = arr.length;
let x = 3;
if (isMajority(arr, n, x) == true )
document.write(x + " appears more than " + parseInt(n / 2, 10) + " times in arr[]" );
else
document.write(x + " does not appear more than " + parseInt(n / 2, 10) + " times in arr[]" );
</script>
|
Output
3 appears more than 3 times in arr[]
Time Complexity: O(log n)
Auxiliary Space: O(1)
Algorithmic Paradigm: Divide and Conquer
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...