- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

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 40% of the votes.

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)

- if occurrences of nums[i] in nums > (n / 3), then
- return ans in sorted order

Let us see the following implementation to get better understanding −

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]) i += n // 3 return sorted(list(ans)) ob = Solution() nums = [3, 2, 6, 6, 6, 6, 7, 7, 7, 7, 7] print(ob.solve(nums))

[3, 2, 6, 6, 6, 6, 7, 7, 7, 7, 7]

[6, 7]

- Related Questions & Answers
- Program to find id of candidate who have hot majority vote in Python
- Majority Element in Python
- When are python objects candidates for garbage collection?
- MySQL Select to get users who have logged in today?
- Program to find elements from list which have occurred at least k times in Python
- Python program to find tuples which have all elements divisible by K from a list of tuples
- Majority Element in C++
- Majority Element in Java
- Program to find starting index of the child who receives last balloon in Python?
- Check if all people can vote on two machines in Python
- Python program to extract only the numbers from a list which have some specific digits
- Program to find total amount of money we have in bank in Python
- Python program to find sum of elements in list
- Program to count minimum number of animals which have no predator in Python
- Program to find minimum number of flips required to have alternating values in Python

Advertisements