Elections Winners
Description
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.
Example
-
For
votes = [2, 3, 5, 2]
andk = 3
, the output should beelectionsWinners(votes, k) = 2
. - The first candidate got
2
votes. Even if all of the remaining3
candidates vote for him, he will still have only5
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, only2
candidates can win (the second and the third), which is the answer.
Input/Output
-
[execution time limit] 4 seconds (js)
-
[input] array.integer votes
A non-empty array of non-negative integers. Its ith element denotes the number of votes cast for the i^{th}
candidate.
Guaranteed constraints:
4 \leq votes.length \leq 10^5
,
0 \leq votes[i] \leq 10^4
.
- [input] integer k
The number of voters who haven’t cast their vote yet.
Guaranteed constraints:
0 ≤ k ≤ 10^5
.
- [output] integer
[JavaScript (ES6)] Syntax Tips
1
2
3
4
5
6
// Prints help message to the console
// Returns a string
function helloWorld(name) {
console.log("This prints to the console when you Run Tests");
return "Hello, " + name;
}
Solution
1
2
3
4
5
6
7
8
9
10
11
function electionsWinners(votes, k) {
var max = Math.max(...votes);
var count = 0;
if (k==0 && votes.filter((a)=>a==max).length>1)
return 0
for(var i = 0; i < votes.length; i++) {
if(votes[i] + k > max || k === 0 && votes[i] == max)
count++;
}
return count;
}