AfterAcademy Tech
•
28 Oct 2019

Difficulty Level: Medium
Asked in: Microsoft, Google, Amazon, Yahoo
Problem Description: You are given an array A[] consisting of n elements. You need to find and return the number which appears more than n/2 times.
Note: Do not confuse the majority element as just the element with maximum frequency. A majority element occurs more than n/2 times.
For example :
Input: A[] = {3,9,1,3,5,3,3}
Output: 3
Input: A[] = {8,8,8,8,8,10,10}
Output: 8
Possible questions to ask the interviewer:-
We will be discussing five possible solutions for this problem:-
A simple brute force approach for this problem would be to use nested loops and count the frequency of each element. If we reach an element with a frequency greater than n/2, that’s our majority element.
Pseudo-Code
int findMajorityElement(int A[], int n)
{
for ( i = 0 to n-2 )
{
int count = 1
for ( j = i+1 to n-1 )
{
if ( A[i] == A[j] )
count = count + 1
}
if ( count > n/2 )
return A[i]
}
return -1
}
Complexity Analysis
Time Complexity: O(n²) (Why?)
Space Complexity: O(1)
Critical ideas to think!
If we sort the array, all similar elements will line up next to each other, it is now easy to check the frequency of each element and can be done in linear time.
Pseudo-Code
int findMajorityElement(int A[], int n)
{
sort(A, n)
int i = 1, count = 1
while ( i < n )
{
while ( i < n and A[i] == A[i-1] )
{
i = i + 1
count = count + 1
}
if ( count > n/2 )
return A[i-1]
count = 1
i = i + 1
}
return -1
}
Complexity Analysis
Time Complexity: Sorting the array + Linear Traversal (Each element is visited only once) = O(nlogn) + O(n) = O(nlogn)
Space Complexity: If we use merge sort, then O(n), else if we use Heap Sort, its O(1)
Critical ideas to think!
If we create an auxiliary Hash Table which will store the frequency of each element in linear time and space complexity. We could then traverse the Hash Table and see if any element has a frequency greater than n/2.
Pseudo-Code
int findMajorityElement(int A[], int n)
{
Create a HashTable H
for ( i = 0 to n-1 )
{
if ( A[i] in H )
H[A[i]] = H[A[i]] + 1
else
H[A[i]] = 1
if ( H[A[i]] > n/2 )
return A[i]
}
return -1
}
Complexity Analysis
Time Complexity: O(n) (Why?)
Space Complexity: O(n), for storing the auxiliary Hash Table
Critical ideas to think!
In this approach, we will deal with the binary representation of the numbers. The algorithm first determines the frequency of each bit in the input array. If there is a number with a majority in the input (i.e. it makes up more than half of the input), then the frequency of all its set bits will be in the majority, and the frequency of all its unset bits will be in the minority.
Solution Steps
We shall be assuming that the elements in the array are within the confines of a 32-bit integer.
Pseudo-Code
int findMajorityElement(int A[], int n)
{
int majority_element = 0, count = 0
for ( curr = 0 to 32 )
{
count = 0
for ( i = 0 to n-1 )
{
// Checks if the bit no. 'curr' is set or not in A[i]
if ( A[i] & (1<<curr) )
count = count + 1
}
if ( count > n/2 )
{
// Setting the bit no. 'curr' as 1 in the majority element
majority_element += (1 << curr)
}
}
count = 0
for( i = 0 to n-1 )
if ( A[i] == majority_element )
count += 1
if ( count > n/2 )
return majority_element
else
return -1
}
Complexity Analysis
Time Complexity: Checking each bit position for entire array + Finding frequency of majority_element = O(32*n) + O(n) = O(n)
Space Complexity: O(1)
Critical ideas to think!
The Boyer-Moore Majority Vote Algorithm finds the majority element if it's guaranteed that the majority element exists. We will just check the frequency of the element found by this algorithm in the end to confirm.
The intuition in the algorithm is that since the majority element occurs more than n/2 times, its frequency is greater than all other elements combined. Therefore, for each occurrence of the majority element, we can cross out one non-majority element.
Solution Steps
3. Check the frequency of the element at max_index via a linear traversal of the array
Pseudo-Code
int findMajorityElement(int A[], int n)
{
int max_index = 0, count = 0
for( i = 0 to n-1 )
{
if ( A[i] == A[max_index] )
count += 1
else
count -= 1
if ( count == 0 )
{
max_index = i
count = 1
}
}
count = 0
for ( i = 0 to n-1 )
if ( A[i] == A[max_index] )
count += 1
if ( count > n/2 )
return A[max_index]
else
return -1
}
Complexity Analysis
Time Complexity: Linear traversal of array + Finding frequency of A[max_index] = O(n) + O(n) = O(n)
Space Complexity: O(1)
Critical ideas to think!

If you have any more approaches or you find an error/bug in the above solutions, please comment down below.
Happy Coding! Enjoy Algorithms!
AfterAcademy Tech
Given an array A[] of size n, you need to find the next greater element for each element in the array. Return an array that consists of the next greater element of A[i] at index i.

AfterAcademy Tech
Given an array A[] of size n, find the most frequent element in the array, i.e. the element which occurs the most number of times.

AfterAcademy Tech
Given two integer array A[] and B[] of size m and n(n <= m) respectively. We have to check whether B[] is a subset of A[] or not. An array B is a subset of another array A if each element of B is present in A. (There are no repeated elements in both the arrays)

AfterAcademy Tech
Given an array A[] of size n, you need to find the maximum and minimum element present in the array.Your algorithm should make minimum number of comparisons.
