26. Majority Element II
β GFG solution to the Majority Element II problem: find all elements appearing more than n/3 times using Boyer-Moore Majority Vote algorithm. π
The problem can be found at the following link: π Question Link
π§© Problem Description
You are given an array of integer arr[]
where each number represents a vote to a candidate. Return the candidates that have votes greater than one-third of the total votes. If there's not a majority vote, return an empty array.
Note: The answer should be returned in an increasing format.
π Examples
Example 1
Input: arr[] = [2, 1, 5, 5, 5, 5, 6, 6, 6, 6, 6]
Output: [5, 6]
Explanation: 5 occurs 4 times and 6 occurs 5 times. Both appear more than n/3 = 11/3 = 3.67 times.
Hence, both 5 and 6 are majority elements.
Example 2
Input: arr[] = [1, 2, 3, 4, 5]
Output: []
Explanation: The total number of votes are 5. No candidate occurs more than floor(5/3) = 1 time.
Since we need elements appearing more than n/3 times, no element qualifies.
π Constraints
$1 \le \text{arr.size()} \le 10^6$
$1 \le \text{arr}[i] \le 10^5$
β
My Approach
The optimal approach uses the Boyer-Moore Majority Vote Algorithm extended for finding elements appearing more than n/3 times:
Boyer-Moore Voting Algorithm (Extended)
Key Insight:
At most 2 elements can appear more than n/3 times in an array.
We maintain two candidates and their counts during the voting phase.
Phase 1 - Candidate Selection:
Initialize two candidates
a
andb
with different values, and their countsca
andcb
.For each element
x
:If
x
equals candidatea
, incrementca
Else if
x
equals candidateb
, incrementcb
Else if
ca
is 0, seta = x
andca = 1
Else if
cb
is 0, setb = x
andcb = 1
Else decrement both
ca
andcb
(voting against both candidates)
Phase 2 - Verification:
Reset counts and verify if the selected candidates actually appear more than n/3 times.
Count actual occurrences of candidates
a
andb
in the array.
Result Formation:
Add candidates to result if their count > n/3.
Sort the result for increasing order output.
π Time and Auxiliary Space Complexity
Expected Time Complexity: O(n), where n is the size of the array. We make two passes through the array - one for candidate selection and one for verification.
Expected Auxiliary Space Complexity: O(1), as we only use a constant amount of additional space for storing two candidates and their counts.
π§βπ» Code (C++)
class Solution {
public:
vector<int> findMajority(vector<int>& arr) {
int n = arr.size(), a = 0, b = 1, ca = 0, cb = 0;
for (int x : arr) {
if (x == a) ca++;
else if (x == b) cb++;
else if (!ca) a = x, ca = 1;
else if (!cb) b = x, cb = 1;
else ca--, cb--;
}
ca = cb = 0;
for (int x : arr) {
if (x == a) ca++;
else if (x == b) cb++;
}
vector<int> res;
if (ca > n / 3) res.push_back(a);
if (cb > n / 3 && a != b) res.push_back(b);
sort(res.begin(), res.end());
return res;
}
};
β Code (Java)
class Solution {
public ArrayList<Integer> findMajority(int[] arr) {
int n = arr.length, a = 0, b = 1, ca = 0, cb = 0;
for (int x : arr) {
if (x == a) ca++;
else if (x == b) cb++;
else if (ca == 0) { a = x; ca = 1; }
else if (cb == 0) { b = x; cb = 1; }
else { ca--; cb--; }
}
ca = cb = 0;
for (int x : arr) {
if (x == a) ca++;
else if (x == b) cb++;
}
ArrayList<Integer> res = new ArrayList<>();
if (ca > n / 3) res.add(a);
if (cb > n / 3 && a != b) res.add(b);
Collections.sort(res);
return res;
}
}
π Code (Python)
class Solution:
def findMajority(self, arr):
n, a, b, ca, cb = len(arr), 0, 1, 0, 0
for x in arr:
if x == a: ca += 1
elif x == b: cb += 1
elif ca == 0: a, ca = x, 1
elif cb == 0: b, cb = x, 1
else: ca, cb = ca - 1, cb - 1
ca = cb = 0
for x in arr:
if x == a: ca += 1
elif x == b: cb += 1
res = []
if ca > n // 3: res.append(a)
if cb > n // 3 and a != b: res.append(b)
return sorted(res)
π§ Contribution and Support
For discussions, questions, or doubts related to this solution, feel free to connect on LinkedIn: π¬ Any Questions?. Let's make this learning journey more collaborative!
β If you find this helpful, please give this repository a star! β
πVisitor Count
Last updated