-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathElectionWinners.java
More file actions
31 lines (29 loc) · 839 Bytes
/
ElectionWinners.java
File metadata and controls
31 lines (29 loc) · 839 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
/*
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.
*/
int electionsWinners(int[] votes, int k) {
int a[]=votes.clone();
Arrays.sort(a);
int c=0,i;
if(k==0){
for(i=0;i<votes.length;i++)
{
if(votes[i]==a[a.length-1])
c++;
}
if(c>=2)
return 0;
else
return c;
}
else{
for(i=0;i<votes.length;i++)
{
if((votes[i]+k)>a[a.length-1])
c++;
}
return c;
}
}