Majority Element Moore's Voting Algorithm in DSA C++ - Time & Space Complexity
We want to understand how fast Moore's Voting Algorithm finds the majority element in a list.
How does the time needed grow as the list gets bigger?
Analyze the time complexity of the following code snippet.
int findMajorityElement(const std::vector<int>& nums) {
int count = 0, candidate = 0;
for (int num : nums) {
if (count == 0) {
candidate = num;
}
count += (num == candidate) ? 1 : -1;
}
return candidate;
}
This code finds the majority element by counting and updating a candidate as it scans the list once.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: One single loop that goes through each element in the list once.
- How many times: Exactly once for each element, so n times if the list has n elements.
As the list gets bigger, the number of steps grows directly with the number of elements.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 steps |
| 100 | About 100 steps |
| 1000 | About 1000 steps |
Pattern observation: The steps increase in a straight line as the list size grows.
Time Complexity: O(n)
This means the time to find the majority element grows directly with the size of the list.
[X] Wrong: "The algorithm checks every pair of elements, so it must be O(n²)."
[OK] Correct: The algorithm only scans the list once, updating counts without nested loops, so it is much faster than checking pairs.
Knowing this algorithm's time complexity shows you can find efficient solutions that scan data just once, a skill valued in real-world coding.
"What if we added a second pass to verify the candidate's count? How would the time complexity change?"
Practice
Solution
Step 1: Understand the problem Moore's Voting Algorithm solves
It is designed to find the majority element, which is the element that appears more than half the times in the array.Step 2: Compare other options
Sorting, finding smallest element, or counting unique elements are unrelated to Moore's Voting Algorithm's goal.Final Answer:
To find the element that appears more than half the times -> Option BQuick Check:
Majority element = element with > n/2 occurrences [OK]
- Thinking it sorts the array
- Confusing majority element with smallest element
- Assuming it counts unique elements
Solution
Step 1: Check initialization for candidate and count
Candidate is usually initialized to an invalid value like -1 or first element, count starts at 0.Step 2: Validate options
int candidate = -1, count = 0; initializes candidate to -1 and count to 0, which is standard and safe before iteration.Final Answer:
int candidate = -1, count = 0; -> Option AQuick Check:
Candidate = -1, Count = 0 initialization [OK]
- Not initializing count to 0
- Setting count to negative value
- Leaving candidate uninitialized
int arr[] = {2, 2, 1, 1, 1, 2, 2};
int candidate = -1, count = 0;
for (int i = 0; i < 7; i++) {
if (count == 0) candidate = arr[i];
if (arr[i] == candidate) count++;
else count--;
}
std::cout << candidate << std::endl;Solution
Step 1: Trace the loop and update candidate and count
Start with candidate=-1, count=0.
i=0: count=0, candidate=2, arr[0]=2 matches candidate, count=1
i=1: arr[1]=2 matches candidate, count=2
i=2: arr[2]=1 not match, count=1
i=3: arr[3]=1 not match, count=0
i=4: count=0, candidate=1, arr[4]=1 matches candidate, count=1
i=5: arr[5]=2 not match, count=0
i=6: count=0, candidate=2, arr[6]=2 matches candidate, count=1Step 2: Final candidate after loop
The candidate is 2 after the loop ends.Final Answer:
2 -> Option DQuick Check:
Final candidate = 2 [OK]
- Not updating candidate when count is zero
- Confusing increment and decrement conditions
- Assuming candidate is last element always
int candidate = 0, count = 0;
for (int i = 0; i < n; i++) {
if (arr[i] == candidate) count++;
else count--;
if (count == 0) candidate = arr[i];
}Solution
Step 1: Analyze order of candidate update and count changes
The candidate must be updated when count is zero before comparing arr[i] with candidate to decide increment or decrement.Step 2: Identify the error in code order
Here, count is changed before candidate update, which causes wrong counting because candidate may be outdated.Final Answer:
Candidate update should happen before count increment/decrement -> Option AQuick Check:
Update candidate first when count=0 [OK]
- Updating candidate after changing count
- Initializing candidate incorrectly
- Wrong loop range
arr = {3, 3, 4, 2, 4, 4, 2, 4, 4}, apply Moore's Voting Algorithm to find the majority element. What is the correct majority element and why?Solution
Step 1: Apply Moore's Voting Algorithm to find candidate
Start candidate=-1, count=0.
i=0: count=0, candidate=3, arr[0]=3 matches candidate, count=1
i=1: arr[1]=3 matches candidate, count=2
i=2: arr[2]=4 not match, count=1
i=3: arr[3]=2 not match, count=0
i=4: count=0, candidate=4, arr[4]=4 matches candidate, count=1
i=5: arr[5]=4 matches candidate, count=2
i=6: arr[6]=2 not match, count=1
i=7: arr[7]=4 matches candidate, count=2
i=8: arr[8]=4 matches candidate, count=3Step 2: Verify candidate frequency
Candidate is 4. Count occurrences of 4 in array: 5 times.
Since 5 > 9/2 (4.5), 4 is majority element.Final Answer:
4, because it appears 5 times which is more than half of 9 -> Option CQuick Check:
Count(4) = 5 > 9/2 [OK]
- Assuming first element is majority
- Not verifying candidate frequency
- Ignoring that majority must be > n/2
Ready to prove it?
Pass the test and this pattern moves unknown
