C Program to Check for Majority Element in a sorted array
Last Updated :
31 May, 2023
Question: Write a function to find if a given integer x appears more than n/2 times in a sorted array of n integers.
Basically, we need 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 element at index i + n/2. If element is present at i+n/2 then return 1 else 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;
}
|
Output:
4 appears more than 3 times in arr[]
Time Complexity: O(n)
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 <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;
}
|
Output:
3 appears more than 3 times in arr[]
Time Complexity: O(Logn)
Algorithmic Paradigm: Divide and Conquer
METHOD 3: If it is already given that the array is sorted and there exists a majority element, checking if a particular element is as easy as checking if the middle element of the array is the number we are checking against.
Since a majority element occurs more than n/2 times in an array, it will always be the middle element. We can use this logic to check if the given number is the majority element.
C
C
#include <stdio.h>
#include <stdbool.h>
bool isMajorityElement( int arr[], int n, int key)
{
if (arr[n / 2] == key)
return true ;
else
return false ;
}
int main()
{
int arr[] = { 1, 2, 3, 3, 3, 3, 10 };
int n = sizeof (arr) / sizeof (arr[0]);
int x = 3;
if (isMajorityElement(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;
}
|
Output
3 appears more than 3 times in arr[]
Time complexity: O(1)
Auxiliary Space: O(1)
Algorithm
- Initialize a variable candidate to the first element of the input array nums, and a variable count to 1.
- Loop through the elements of the input array nums starting from the second element.
- If the current element is equal to the candidate, increment count by 1.
- If the current element is not equal to the candidate, decrement count by 1. If count becomes 0, set the candidate to the current element and set count to 1.
- After the loop, loop through the elements of the input array nums again, counting how many times the candidate appears.
- If the count of the candidate is greater than n/2 where n is the size of the input array nums, return the candidate as the majority element.
- Otherwise, return -1 to indicate that there is no majority element.
C
#include <stdio.h>
int findMajorityElement( int * nums, int n) {
int candidate = nums[0], count = 1;
for ( int i = 1; i < n; i++) {
if (nums[i] == candidate) {
count++;
} else {
count--;
if (count == 0) {
candidate = nums[i];
count = 1;
}
}
}
count = 0;
for ( int i = 0; i < n; i++) {
if (nums[i] == candidate) {
count++;
}
}
if (count > n/2) {
return candidate;
} else {
return -1;
}
}
int main() {
int nums[] = {1, 2, 3, 4, 4, 4, 4};
int n = sizeof (nums)/ sizeof (nums[0]);
int majority = findMajorityElement(nums, n);
if (majority != -1) {
printf ( "The majority element is %d\n" , majority);
} else {
printf ( "There is no majority element\n" );
}
return 0;
}
|
Output
The majority element is 4
The time complexity is O(n), and the auxiliary space is O(1)
Please refer complete article on Check for Majority Element in a sorted array for more details!
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...