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
Tournament Match Counter INPUT 7 Teams Tournament T1 T2 T3 T4 T5 T6 T7 n = 7 (number of teams) Tournament Rules: Even n: n/2 matches Odd n: (n-1)/2 matches + 1 team advances free Continue until 1 winner ALGORITHM STEPS 1 Round 1: n=7 (odd) Matches: (7-1)/2 = 3 Advance: 3+1 = 4 teams 2 Round 2: n=4 (even) Matches: 4/2 = 2 Advance: 2 teams 3 Round 3: n=2 (even) Matches: 2/2 = 1 Advance: 1 team (Winner!) 4 Sum All Matches Total = 3 + 2 + 1 = 6 Round | Teams | Matches 1 | 7 | 3 2 | 4 | 2 3 | 2 | 1 FINAL RESULT Total Matches 6 OK - Verified! After 6 matches total, 1 champion emerges from 7 teams! Optimal: n-1 = 7-1 = 6 Key Insight: In any elimination tournament, each match eliminates exactly 1 team. To go from n teams to 1 winner, we must eliminate n-1 teams. Therefore, the answer is always n-1 matches, regardless of odd/even rounds! Time: O(1) | Space: O(1) | Formula: return n - 1 TutorialsPoint - Count of Matches in Tournament | Optimal Solution
Asked in
Google 15 Amazon 12
35.6K Views
Medium Frequency
~5 min Avg. Time
890 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