Ctrl K

More Than a Third of the Votes

Medium
The array votes lists the candidate numbers from the ballots, one number per ballot. There are n ballots in total. Return every candidate that received more than ⌊n / 3⌋ votes, in any order. Try to do it in linear time with O(1) extra memory.
Example 1
votes=[4, 7, 4, 2, 7, 4] → [4]

There are 6 votes, so a candidate needs more than 2. Candidate 4 has three, 7 has two and 2 has one: only 4 qualifies.

Example 2
votes=[-5, 8] → [-5, 8]

The bar is more than 0 votes, so both qualify.

Example 3
votes=[1, 2, 3, 1, 2, 3] → []

Each has exactly a third of the votes, but strictly more is needed, so the list is empty.

Constraints
  • 1 ≤ len(votes) ≤ 5·10⁴
  • -10⁹ ≤ votes[i] ≤ 10⁹
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsMost Common Values MediumJobs With a Cooldown MediumMost Searched Words MediumTriples Hitting the Target MediumSpread Out the Labels Medium
loading Python…
Input
votes=[4, 7, 4, 2, 7, 4]
Expected
[4]

Run checks the first 3 tests in your browser. Submit checks all 80 tests on the server, hidden ones included.