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
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!
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code