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.
Examples:
Input: N = 2, K = 4
Output: 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
Output: 4Input: N = 9, K = 5
Output: 20
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.
C++
// C++ code to implement the approach#include <bits/stdc++.h>using namespace std;Â
// Function to calculate point differenceint pointDiff(int N, int K){Â
  // If N is odd  if (N % 3 != 0)    return ((N - 1) / 2) * K;Â
  // If N is even  return (N / 2) * K;}Â
// Driver codeint main(){Â Â int N = 9, K = 5;Â
  // Function call  cout << pointDiff(N, K);Â
  return 0;}Â
// This code is contributed by rakeshsahni |
Java
// Java code to implement the approachÂ
import java.util.*;Â
class GFG {Â
    // Function to calculate point difference    public static int pointDiff(int N, int K)    {        // If N is odd        if (N % 3 != 0)            return ((N - 1) / 2) * K;Â
        // If N is even        return (N / 2) * K;    }Â
    // Driver code    public static void main(String[] args)    {        int N = 9, K = 5;Â
        // Function call        System.out.println(pointDiff(N, K));    }} |
Python3
# Python code to implement the approachÂ
# Function to calculate point differencedef pointDiff(N, K):       # If N is odd    if N % 3 != 0:        return ((N - 1) // 2) * K           # If N is even    return (N // 2) * KÂ
# Driver codeif __name__ == "__main__":    N = 9    K = 5    # Function call    print(pointDiff(N, K))Â
# This code is contributed by Rohit Pradhan |
C#
// C# code to implement the approachusing System;using System.Linq;Â
public class GFG{  // Function to calculate point difference  public static int pointDiff(int N, int K)  {    // If N is odd    if (N % 3 != 0)      return ((N - 1) / 2) * K;Â
    // If N is even    return (N / 2) * K;  }Â
  // Driver Code  public static void Main(string[] args)  {    int N = 9, K = 5;Â
    // Function call    Console.WriteLine(pointDiff(N, K));  }}Â
// This code is contributed by code_hunt. |
Javascript
<script>Â
// JavaScript code to implement the approachÂ
Â
// Function to calculate point differencefunction pointDiff(N, K){Â
  // If N is odd  if (N % 3 != 0)    return Math.floor((N - 1) / 2) * K;Â
  // If N is even  return Math.floor(N / 2) * K;}Â
// Driver codeÂ
let N = 9, K = 5;Â
// Function calldocument.write(pointDiff(N, K),"</br>");Â
Â
// This code is contributed by shinjanpatraÂ
</script> |
20
Time Complexity: O(1)
Auxiliary Space: O(1)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!


