Skip to main content

Majority Element (n/3 times)

Problem Statement:โ€‹

Given an array of N integers. Find the elements that appear more thanย N/3ย times in the array. If no such element exists, return an empty vector.

  • Example:

    Example 1:
    Input Format: N = 5, array[] = {1,2,2,3,2}
    Result: 2
    Explanation: Here we can see that the Count(1) = 1, Count(2) = 3 and Count(3) = 1.Therefore, the count of 2 is greater than N/3 times. Hence, 2 is the answer.

    Example 2:
    Input Format: N = 6, array[] = {11,33,33,11,33,11}
    Result: 11 33
    Explanation: Here we can see that the Count(11) = 3 and Count(33) = 3. Therefore, the count of both 11 and 33 is greater than N/3 times. Hence, 11 and 33 is the answer.


โœ… Solution: Extended Boyer-Moore Voting Algorithm (for n/3 Majority Elements)โ€‹

vector<int> majorityElement(vector<int> nums) {
int el1 = INT_MIN, el2 = INT_MIN;
int cnt1 = 0, cnt2 = 0;

// Phase 1: Find candidates for majority elements
for (int num : nums) {
if (cnt1 == 0 && num != el2) {
el1 = num;
cnt1 = 1;
}
else if (cnt2 == 0 && num != el1) {
el2 = num;
cnt2 = 1;
}
else if (num == el1) cnt1++;
else if (num == el2) cnt2++;
else {
cnt1--;
cnt2--;
}
}

// Phase 2: Verify the actual counts of the candidates
cnt1 = cnt2 = 0;
for (int num : nums) {
if (num == el1) cnt1++;
if (num == el2) cnt2++;
}

// Collect results if they appear more than n/3 times
vector<int> result;
int threshold = nums.size() / 3;
if (cnt1 > threshold) result.push_back(el1);
if (el2 != el1 && cnt2 > threshold) result.push_back(el2);

return result;
}


๐Ÿ“ How It Worksโ€‹

  • Goal: Find elements that appear more than โŒŠn/3โŒ‹ times in an array.
  • At most 2 elements can satisfy this condition (otherwise total count > n).
  • The algorithm works in 2 phases:
    1. Candidate Selection:
      • Use two counters (cnt1, cnt2) and two candidate variables (el1, el2).
      • Simulate a โ€œvotingโ€ system to find potential majority candidates.
    2. Candidate Verification:
      • Recount how many times each candidate actually appears.
      • Add to the result only if the frequency > โŒŠn/3โŒ‹.

๐Ÿงฉ Key Insightโ€‹

If an element appears more than n/3 times, it will survive the voting (will not be eliminated during the count down phase).


โฑ๏ธ Time & Space Complexityโ€‹

MetricComplexity
TimeO(N)
SpaceO(1)
  • Two linear passes over the array.
  • Constant extra space used for counters and candidates.

โš ๏ธ Edge Casesโ€‹

  • Multiple elements appearing exactly โŒŠn/3โŒ‹ times โ†’ not included.
  • Less than 2 valid candidates.
  • Duplicates like [2,2,2,2] โ†’ only one candidate.

๐Ÿ’ก Other Approachesโ€‹

ApproachTimeSpaceNotes
HashMap CountO(N)O(N)Easier to implement, but not space efficient
Boyer-MooreO(N)O(1)Optimal solution โœ…


๐Ÿ› ๏ธ Other Notesโ€‹

  • This is a generalized version of Boyer-Moore Voting Algorithm.
  • Works for any n/k type majority search by extending to k - 1 counters.
  • Analogy: Like political candidatesโ€”if there's too much opposition, the weak ones drop out.
๐Ÿ’ฌ

Discussion & Doubts