# C++ Program to Check for Majority Element in a sorted array

• Last Updated : 13 Dec, 2021

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++

 `/* C++ Program to check for majority element in a sorted array */` `#include ` `using` `namespace` `std; ` ` `  `bool` `isMajority(``int` `arr[], ``int` `n, ``int` `x) ` `{ ` `    ``int` `i; ` ` `  `    ``/* get last index according to n (even or odd) */` `    ``int` `last_index = n % 2 ? (n / 2 + 1): (n / 2); ` ` `  `    ``/* search for first occurrence of x in arr[]*/` `    ``for` `(i = 0; i < last_index; i++) ` `    ``{ ` `       `  `        ``/* check if x is present and is present more than n/2 ` `        ``times */` `        ``if` `(arr[i] == x && arr[i + n / 2] == x) ` `            ``return` `1; ` `    ``} ` `    ``return` `0; ` `} ` ` `  `/* Driver code */` `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 <

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++

 `// C++ program to check for majority ` `// element in a sorted array  ` `#include ` `using` `namespace` `std; ` ` `  `// If x is present in arr[low...high]  ` `// then returns the index of first ` `// occurrence of x, otherwise returns -1  ` `int` `_binarySearch(``int` `arr[], ``int` `low,  ` `                  ``int` `high, ``int` `x); ` ` `  `// This function returns true if the x ` `// is present more than n/2 times in ` `// arr[] of size n  ` `bool` `isMajority(``int` `arr[], ``int` `n, ``int` `x) ` `{ ` `     `  `    ``// Find the index of first occurrence  ` `    ``// of x in arr[]  ` `    ``int` `i = _binarySearch(arr, 0, n - 1, x); ` ` `  `    ``// If element is not present at all, ` `    ``// return false ` `    ``if` `(i == -1) ` `        ``return` `false``; ` ` `  `    ``// Check if the element is present  ` `    ``// more than n/2 times  ` `    ``if` `(((i + n / 2) <= (n - 1)) && ` `      ``arr[i + n / 2] == x) ` `        ``return` `true``; ` `    ``else` `        ``return` `false``; ` `} ` ` `  `// If x is present in arr[low...high] then  ` `// returns the index of first occurrence  ` `// of x, otherwise returns -1  ` `int` `_binarySearch(``int` `arr[], ``int` `low,  ` `                  ``int` `high, ``int` `x) ` `{ ` `    ``if` `(high >= low) ` `    ``{ ` `        ``int` `mid = (low + high)/2; ``/*low + (high - low)/2;*/` ` `  `        ``/* Check if arr[mid] is the first occurrence of x. ` `            ``arr[mid] is first occurrence if x is one of  ` `            ``the following is true: ` `            ``(i) mid == 0 and arr[mid] == x ` `            ``(ii) arr[mid-1] < x and arr[mid] == x ` `        ``*/` `        ``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; ` `} ` ` `  `// Driver code ` `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; ` `} ` ` `  `// This code is contributed by shivanisinghss2110`

Output:

`3 appears more than 3 times in arr[]`

Time Complexity: O(Logn)

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++

 `#include ` `using` `namespace` `std; ` ` `  `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)) ` `        ``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; ` `} ` ` `  `// This code is contributed by shivanisinghss2110 `

Output

`3 appears more than 3 times in arr[]`

Time complexity: O(1)
Auxiliary Space: O(1)

Please refer complete article on Check for Majority Element in a sorted array for more details!

My Personal Notes arrow_drop_up
Related Articles