Archive / 2020 / February 2020
Siri Land's Presidential Election
There’s a problem with this question and submissions to this task won’t be counted for the final score. Read the notification for more info.
Siri Land is a peaceful and technologically advanced country. The Election Commissioner of Siri Land has called a Presidential Election. Since too many people are contesting, instead of a lengthy voting sheet the election commission has proposed an electronic system. As you are a young smart programmer the Election Commission seeks your help in implementing an algorithm which counts the votes and finds the winner fast.
Election rules
- There are n number of contestants
- Voters can choose first preference and second preference
- Firstly, the first preferences are counted. If one contestant is able to take more than 50% of votes he will be the winner
- If the first preference does not choose the winner, all the contestants except the highest two vote gainers(say A, B) will be eliminated.
- Then the second preference is counted. If the second choice of the eliminated votes are received by A or B that relevant counts are also being added to A,B contestants.
- If the above count also ties, then the winner is chosen by tossing a coin
For simplicity the names are ignored in our counting system. Contestants are numbered from 1 to n
Input Format
First line contains 2 integers, n (number of contestants) & m (number of voters).
m lines follow, each with two space separated integers p1 (first preference) and p2 (second preference)
Assume all the voters have given both preference
Output Format
If there’s a wining contestant, the program should out put the number of the winning contestants. If no one wins, the program should output “Toss a coin and find the winner”
Constraints
- 1 \(\leq\) n \(\leq\) m \(\leq\) 1016
- 1 \(\leq\) p1, p2 \(\leq\) n
Limits
- Time Limit: 1s
- Memory Limit: 256MB
Sample Input 0
5
10
1 2
1 3
1 4
2 1
3 1
1 5
2 3
2 5
3 2
4 3
Sample Output 0
1
Sample Input 1
4
4
1 2
1 2
3 2
4 3
Sample Output 1
Toss a coin and find the winner
Open on HackerRank ↗ · Markdown source · Back to the archive