Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Program to list of candidates who have got majority vote in python
Suppose we have a list of numbers called nums where each number represents a vote to a candidate. We have to find the ids of the candidates that have greater than floor(n/3) votes, in non-decreasing order.
So, if the input is like nums = [3, 2, 6, 6, 6, 6, 7, 7, 7, 7, 7], then the output will be [6, 7], as 6 and 7 have more than 33% of the votes (4 out of 11 and 5 out of 11 respectively).
Algorithm
To solve this, we will follow these steps ?
- ans := a new empty set
- sort the list nums
- i := 0
- n := size of nums
- while i < size of nums, do
- if occurrences of nums[i] in nums > (n / 3), then
- insert nums[i] into ans
- i := i + (n // 3) + 1
- if occurrences of nums[i] in nums > (n / 3), then
- return ans in sorted order
Example
class Solution:
def solve(self, nums):
ans = set()
nums.sort()
i = 0
n = len(nums)
while i < len(nums):
if nums.count(nums[i]) > n // 3:
ans.add(nums[i])
# Skip to next potentially different candidate
count = nums.count(nums[i])
i += count
return sorted(list(ans))
# Test the solution
ob = Solution()
nums = [3, 2, 6, 6, 6, 6, 7, 7, 7, 7, 7]
result = ob.solve(nums)
print("Candidates with majority votes:", result)
Candidates with majority votes: [6, 7]
Optimized Approach Using Counter
A more efficient approach uses Python's Counter to count occurrences in one pass ?
from collections import Counter
def find_majority_candidates(nums):
n = len(nums)
threshold = n // 3
# Count occurrences of each candidate
vote_counts = Counter(nums)
# Find candidates with votes > n//3
majority_candidates = []
for candidate, count in vote_counts.items():
if count > threshold:
majority_candidates.append(candidate)
return sorted(majority_candidates)
# Test with example
nums = [3, 2, 6, 6, 6, 6, 7, 7, 7, 7, 7]
result = find_majority_candidates(nums)
print("Input votes:", nums)
print("Majority candidates:", result)
# Test with another example
nums2 = [1, 1, 1, 2, 2, 3, 3, 3, 3]
result2 = find_majority_candidates(nums2)
print("\nInput votes:", nums2)
print("Majority candidates:", result2)
Input votes: [3, 2, 6, 6, 6, 6, 7, 7, 7, 7, 7] Majority candidates: [6, 7] Input votes: [1, 1, 1, 2, 2, 3, 3, 3, 3] Majority candidates: [1, 3]
How It Works
The algorithm works by:
- Counting votes: Count how many times each candidate appears
- Checking threshold: Compare each count against n//3 (floor division)
- Filtering results: Keep only candidates exceeding the threshold
- Sorting output: Return candidates in non-decreasing order
Comparison
| Method | Time Complexity | Space Complexity | Best For |
|---|---|---|---|
| Sorting + Iteration | O(n log n) | O(1) | When space is limited |
| Counter Approach | O(n) | O(k) | Better performance, k = unique candidates |
Conclusion
The Counter approach is more efficient with O(n) time complexity. Both methods correctly identify candidates with more than floor(n/3) votes and return them in sorted order.
