C# Program to Check for Majority Element in a sorted array
Last Updated :
22 May, 2023
Problem Statement: 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[] ), the 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#
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[]" );
}
}
|
Output:
4 appears more than 3 times in arr[]
Time Complexity: O(n)
Auxiliary Space: O(1), as constant extra space is used.
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#
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[]" );
}
}
|
Output:
3 appears more than 3 times in arr[]
Time Complexity: O(Logn)
Auxiliary Space: O(1), as constant extra space is used.
Method 3: Divide and Conquer
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#
using System;
class GFG {
static bool isMajorityElement( int [] arr, int n, int key)
{
if (arr[n / 2] == key)
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 (isMajorityElement(arr, n, x))
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[]" );
}
}
|
Output
3 appears more than 3 times in arr[]
Time complexity: O(1)
Auxiliary Space: O(1)
Method 4: Using Moore’s Voting Algorithm
- Initialize the candidate element as the first element of the array setting count to 1.
- Traverse through the array from the second element to the last.
- If the current element is the same as the candidate element, increment the count otherwise decrement the count.
- If the count becomes 0, set the current element as the candidate element and reset the count to 1.
- Check if the candidate element is the majority element by traversing the array again and counting the number of occurrences of the candidate element.
- If the number of occurrences of the candidate element is greater than n/2, return the candidate element. Otherwise, return -1.
C#
using System;
class Program {
static int FindMajority( int [] arr, int n)
{
int candidate = arr[0];
int count = 1;
for ( int i = 1; i < n; i++) {
if (arr[i] == candidate)
count++;
else
count--;
if (count == 0) {
candidate = arr[i];
count = 1;
}
}
int countCandidate = 0;
for ( int i = 0; i < n; i++) {
if (arr[i] == candidate)
countCandidate++;
}
if (countCandidate > n / 2)
return candidate;
else
return -1;
}
static void Main()
{
int [] arr = { 1, 2, 3, 3, 3, 3, 10 };
int n = arr.Length;
int majorityElement = FindMajority(arr, n);
if (majorityElement != -1)
Console.Write(majorityElement
+ " is the majority element" );
else
Console.Write( "No majority element" );
}
}
|
Output
3 is the majority element
Time complexity is O(n)
Auxiliary Space: O(n)
Please refer complete article on Check for Majority Element in a sorted array for more details!
Similar Reads
C# Program for Ceiling in a sorted array
Given a sorted array and a value x, the ceiling of x is the smallest element in array greater than or equal to x, and the floor is the greatest element smaller than or equal to x. Assume than the array is sorted in non-decreasing order. Write efficient functions to find floor and ceiling of x. Examp
5 min read
C# Program for Search an element in a sorted and rotated array
An element in a sorted array can be found in O(log n) time via binary search. But suppose we rotate an ascending order sorted array at some pivot unknown to you beforehand. So for instance, 1 2 3 4 5 might become 3 4 5 1 2. Devise a way to find an element in the rotated array in O(log n) time. Exam
7 min read
C# Program for Median of two sorted arrays of same size
Write a C# program for a given 2 sorted arrays A and B of size n each. the task is to find the median of the array obtained by merging the above 2 arrays(i.e. array of length 2n). The complexity should be O(log(n)). Examples: Input: ar1[] = {1, 12, 15, 26, 38} ar2[] = {2, 13, 17, 30, 45}Output: 16Ex
7 min read
C# Program for Leaders in an array
Write a program to print all the LEADERS in the array. An element is leader if it is greater than all the elements to its right side. And the rightmost element is always a leader. For example int the array {16, 17, 4, 3, 5, 2}, leaders are 17, 5 and 2. Let the input array be arr[] and size of the ar
3 min read
C# - Perform Searching using Predefined Functions
Given an array, now our task is to perform searching an element in an array using predefined functions in C#. So, the best searching technique is Binary search and will search the given element in an array using predefined functions. Binary search is an efficient algorithm that will work only on sor
3 min read
C++ Program to Check for Majority Element in a sorted array
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 majorit
6 min read
C Program to Check for Majority Element in a sorted array
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 majorit
7 min read
Java Program for Check for Majority Element in a sorted array
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 majorit
5 min read
Check for Majority Element in a sorted array
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 i
15+ min read
Javascript Program to Check Majority Element in a sorted array
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 majorit
3 min read