Maximum points difference between the winner and runner up of Tournament
Given integer N and K, denoting the number of teams participating in a football tournament where each team plays only one match with each other team, the task is to find the maximum point difference between the winner and the runner-up (second place holder) of the tournament where the winner of a match gets K points.
Input: N = 2, K = 4
Explanation: If there are 2 team A and B then either A will win or lose.
If A wins the match it scores 4 points and team B score is 0.
Hence maximum possible difference of points between the winning team and the second-placed team is 4 .
Input: N = 3, K = 4
Input: N = 9, K = 5
Approach: the problem can be solved based on the following mathematical observation:
The difference will be maximum when the winner wins all the matches it plays and all the other teams win near equal matches each. As each time is playing against others only once so the total number of matches is N * (N – 1)/2.
If the winner wins all matches (i.e., (N-1) matches that it plays) the remaining number of matches are:
(N * (N – 1))/2 – (N – 1) = (N – 1) * (N – 2) / 2.
If each team wins near equal matches, the runner-up will win ceil[ (N – 1)*(N – 2) / (2 * (N – 1)) ] = ceil[ (N – 2)/2 ]
If N is odd: The value is (N – 2 + 1)/2 = (N – 1)/2
If N is even: The value is (N – 2)/2.
Matches difference when N is odd: (N – 1) – (N – 1)/2 = (N – 1)/2. So points difference = K * ((N – 1)/2).
Matches difference when N is even: (N – 1) – (N – 2)/2 = N/2. So points difference = K * (N / 2).
Follow the steps mentioned below to implement the idea:
- Check if N is odd or even.
- If N is odd the required difference is K*((N-1)/2).
- If N is even the required difference is K*(N/2).
Below is the implementation of the above approach.
Time Complexity: O(1)
Auxiliary Space: O(1)
Please Login to comment...