We use cookies (including Google cookies) to personalize ads and analyze traffic. By continuing to use our site, you accept our Privacy Policy.

Count of Matches in Tournament

Number: 1806

Difficulty: Easy

Paid? No

Companies: Amazon


Problem Description

Given n teams in a tournament where, in each round, if n is even then n/2 matches are played (each match eliminates one team) and if n is odd then (n-1)/2 matches are played (with one team receiving a bye), count the total number of matches played until a single winner remains.


Key Insights

  • Each match eliminates exactly one team.
  • The tournament stops when only one team remains.
  • Thus, the total number of matches played is equal to n - 1.
  • Although a simulation of rounds is possible, the mathematical insight provides an O(1) solution.

Space and Time Complexity

Time Complexity: O(1) Space Complexity: O(1)


Solution

The solution leverages the observation that each match reduces the number of teams by one, regardless of whether the round has an odd or even number of teams. By the end of the tournament, all teams except the winner have been eliminated. Therefore, the total number of matches played will be n - 1.

Data Structure(s): None required. Algorithmic Approach: Mathematical observation that each match eliminates one team, leading directly to the answer. Tricks/Mental Leaps: Recognizing that simulating each round is unnecessary since the invariant (eliminating one team per match) directly yields the result.


Code Solutions

# Function to count matches in tournament using mathematical observation
def number_of_matches(n):
    # Each match eliminates one team, so total matches = n - 1
    return n - 1

# Example usage:
n = 7
result = number_of_matches(n)  # Expected output: 6
print(result)  # Print the result
← Back to All Questions