Count of Matches in Tournament - Problem

Imagine you're organizing a tournament with some unusual rules! You start with n teams, and in each round, teams either get paired up to compete or one lucky team gets a bye (free pass to the next round).

Tournament Rules:

  • Even number of teams: All teams pair up perfectly. If there are 8 teams, we get 4 matches and 4 winners advance.
  • Odd number of teams: One team gets a free pass, the rest pair up. If there are 7 teams, 1 advances automatically, 6 teams play 3 matches, so 4 teams total advance (1 + 3 = 4).

The tournament continues until only one champion remains! Your job is to count the total number of matches played throughout the entire tournament.

Goal: Given the initial number of teams n, return the total matches played until we have a winner.

Input & Output

example_1.py โ€” Small Tournament
$ Input: n = 7
โ€บ Output: 6
๐Ÿ’ก Note: Round 1: 7 teams โ†’ 3 matches (6 teams play, 1 gets bye) โ†’ 4 teams advance. Round 2: 4 teams โ†’ 2 matches โ†’ 2 teams advance. Round 3: 2 teams โ†’ 1 match โ†’ 1 winner. Total: 3 + 2 + 1 = 6 matches.
example_2.py โ€” Power of 2
$ Input: n = 8
โ€บ Output: 7
๐Ÿ’ก Note: Round 1: 8 teams โ†’ 4 matches โ†’ 4 teams advance. Round 2: 4 teams โ†’ 2 matches โ†’ 2 teams advance. Round 3: 2 teams โ†’ 1 match โ†’ 1 winner. Total: 4 + 2 + 1 = 7 matches.
example_3.py โ€” Minimum Case
$ Input: n = 1
โ€บ Output: 0
๐Ÿ’ก Note: With only 1 team, they're automatically the winner. No matches needed! This confirms our formula: 1 - 1 = 0 matches.

Constraints

  • 1 โ‰ค n โ‰ค 200
  • n is a positive integer representing the number of teams
  • Time limit: 1 second per test case

Visualization

Tap to expand
Tournament Elimination PrincipleExample: n = 7 teams7 teams initiallyAfter Round 1: 3 matchesW1W2W3Bye4 teams remain (3 eliminated)After Round 2: 2 matchesW4W52 teams remain (5 eliminated)After Round 3: 1 match๐Ÿ†Champion! (6 eliminated total)Key InsightTeams eliminated: 6Matches played: 3 + 2 + 1 = 6Pattern: n - 1 = 7 - 1 = 6Each match eliminatesexactly 1 teamโˆด Answer = n - 1
Understanding the Visualization
1
Initial State
Tournament starts with n teams competing for the championship
2
Match Outcome
Each match has exactly one winner and one loser (eliminating 1 team)
3
Elimination Count
To get 1 winner from n teams, exactly n-1 teams must be eliminated
4
Final Answer
Since each match eliminates 1 team, we need exactly n-1 matches
Key Takeaway
๐ŸŽฏ Key Insight: The tournament format doesn't matter - whether teams get byes or all play simultaneously, we always need exactly n-1 matches because each match eliminates exactly one team, and we need to eliminate n-1 teams to crown 1 champion!
Asked in
Amazon 15 Google 12 Microsoft 8 Meta 6
42.0K Views
Medium Frequency
~8 min Avg. Time
1.9K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen