Watch 10 video solutions for Count of Matches in Tournament, a easy level problem involving Math, Simulation. This walkthrough by codestorywithMIK has 6,624 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
You are given an integer n, the number of teams in a tournament that has strange rules:
n / 2 matches are played, and n / 2 teams advance to the next round.(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.
Example 1:
Input: n = 7 Output: 6 Explanation: Details of the tournament: - 1st Round: Teams = 7, Matches = 3, and 4 teams advance. - 2nd Round: Teams = 4, Matches = 2, and 2 teams advance. - 3rd Round: Teams = 2, Matches = 1, and 1 team is declared the winner. Total number of matches = 3 + 2 + 1 = 6.
Example 2:
Input: n = 14 Output: 13 Explanation: Details of the tournament: - 1st Round: Teams = 14, Matches = 7, and 7 teams advance. - 2nd Round: Teams = 7, Matches = 3, and 4 teams advance. - 3rd Round: Teams = 4, Matches = 2, and 2 teams advance. - 4th Round: Teams = 2, Matches = 1, and 1 team is declared the winner. Total number of matches = 7 + 3 + 2 + 1 = 13.
Constraints:
1 <= n <= 200Problem Overview: You are given n teams in a knockout tournament. Each round pairs teams to play matches, and the winner advances to the next round. If the number of teams is odd, one team automatically advances. The goal is to count the total number of matches played until a single champion remains.
Approach 1: Iterative Reduction Approach (Simulation) (Time: O(log n), Space: O(1))
This method directly simulates how the tournament progresses. In each round, compute how many matches are played based on the number of teams. If n is even, n / 2 matches occur and n / 2 teams advance. If n is odd, (n - 1) / 2 matches occur and one team automatically advances, leaving (n - 1) / 2 + 1 teams for the next round. Repeat this process using a loop until only one team remains, accumulating the total number of matches along the way. This is a straightforward simulation approach that mirrors the real tournament structure and is useful when practicing iterative reasoning problems.
Approach 2: Direct Mathematical Calculation (Time: O(1), Space: O(1))
The key insight comes from observing what happens in every match: exactly one team is eliminated. Starting with n teams and ending with one champion means n - 1 teams must be eliminated. Since each match eliminates exactly one team, the total number of matches played must be n - 1. This eliminates the need for simulation entirely. The result can be returned immediately with a simple formula. This solution relies on basic math reasoning rather than step‑by‑step iteration, making it the most efficient and elegant approach.
Recommended for interviews: Start with the simulation approach to show you understand how the tournament rounds work. Then present the mathematical insight that each match removes one team, leading to the formula n - 1. Interviewers typically expect the constant-time math solution because it demonstrates problem simplification and analytical thinking.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Iterative Reduction (Simulation) | O(log n) | O(1) | When you want to simulate tournament rounds or demonstrate step-by-step reasoning |
| Direct Mathematical Calculation | O(1) | O(1) | Best choice when recognizing that each match eliminates exactly one team |