Skip to content

Elections Winners

Elections are in progress!

Given an array of the numbers of votes given to each of the candidates so far, and an integer k equal to the number of voters who haven't cast their vote yet, find the number of candidates who still have a chance to win the election.

The winner of the election must secure strictly more votes than any other candidate. If two or more candidates receive the same (maximum) number of votes, assume there is no winner at all.


For votes = [2, 3, 5, 2] and k = 3, the output should be

elections_winners(votes, k) = 2
  • The first candidate got 2 votes. Even if all of the remaining 3 candidates vote for him, he will still have only 5 votes, i.e. the same number as the third candidate, so there will be no winner.
  • The second candidate can win if all the remaining candidates vote for him (3 + 3 = 6 > 5).
  • The third candidate can win even if none of the remaining candidates vote for him. For example, if each of the remaining voters cast their votes for each of his opponents, he will still be the winner (the votes array will thus be [3, 4, 5, 3]).
  • The last candidate can't win no matter what (for the same reason as the first candidate).

Thus, only 2 candidates can win (the second and the third), which is the answer.


  • [execution time limit] 4 seconds (py3)

  • [input] array.integer votes

    A non-empty array of non-negative integers. Its ith element denotes the number of votes cast for the ith candidate.

    Guaranteed constraints:4 ≤ votes.length ≤ 105, 0 ≤ votes[i] ≤ 104.

  • [input] integer k

    The number of voters who haven't cast their vote yet.

    Guaranteed constraints:0 ≤ k ≤ 105.

  • [output] integer


def elections_winners(votes, k):
    max_vote = max(votes)

    if k == 0 and votes.count(max_vote) == 1:
        return 1

    return len([vote for vote in votes if vote + k > max_vote])

print(elections_winners([1, 1, 1, 1, 1], 1))
function electionsWinners(votes, k) {
  let maxVote = Math.max(...votes);
  if (k === 0 && votes.filter((vote) => vote === maxVote).length === 1) {
    return 1;
  return votes.filter((vote) => vote + k > maxVote).length;

console.log(electionsWinners([1, 1, 1, 1, 1], 1));

my thoughts are neither my employer's nor my wife's