Count of Matches in Tournament - Problem
You are given an integer n, the number of teams in a tournament that has strange rules:
If the current number of teams is even, each team gets paired with another team. A total of n / 2 matches are played, and n / 2 teams advance to the next round.
If the current number of teams is odd, one team randomly advances in the tournament, and the rest gets paired. A total of (n - 1) / 2 matches are played, and (n - 1) / 2 + 1 teams advance to the next round.
Return the number of matches played in the tournament until a winner is decided.
Input & Output
Example 1 — Odd Number of Teams
$
Input:
n = 7
›
Output:
6
💡 Note:
Round 1: 7 teams (odd) → 3 matches, 4 teams advance. Round 2: 4 teams (even) → 2 matches, 2 teams advance. Round 3: 2 teams (even) → 1 match, 1 winner. Total: 3 + 2 + 1 = 6 matches.
Example 2 — Even Number of Teams
$
Input:
n = 14
›
Output:
13
💡 Note:
Mathematical insight: each match eliminates exactly 1 team. To go from 14 teams to 1 winner, we eliminate 13 teams, requiring 13 matches.
Example 3 — Minimum Case
$
Input:
n = 2
›
Output:
1
💡 Note:
Only 2 teams need just 1 match to determine the winner. Using formula: n - 1 = 2 - 1 = 1.
Constraints
- 1 ≤ n ≤ 200
Visualization
Tap to expand
Understanding the Visualization
1
Input
n teams enter tournament
2
Process
Each match eliminates exactly 1 team
3
Output
Total matches = n - 1
Key Takeaway
🎯 Key Insight: Each match eliminates exactly one team, so total matches always equals n-1
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code