Before attempting this problem, you should be comfortable with:
Basic Math - Understanding that each match eliminates exactly one team
Simulation - Iteratively modeling the tournament rounds until a winner emerges
Logical Reasoning - Recognizing the invariant that n-1 matches are needed to eliminate n-1 teams
1. Simulation
Intuition
We can simulate the tournament round by round. In each round, teams are paired up. If the number of teams is even, half play and half are eliminated. If odd, one team gets a bye and the rest pair up. We continue until only one team remains.
Algorithm
Initialize a counter for the total matches played.
While more than one team remains:
Add n / 2 matches (the number of pairings).
Update n to (n + 1) / 2 (winners plus possibly one bye team).
classSolution:defnumberOfMatches(self, n:int)->int:
res =0while n >1:
res += n //2
n =(n +1)//2return res
publicclassSolution{publicintnumberOfMatches(int n){int res =0;while(n >1){
res += n /2;
n =(n +1)/2;}return res;}}
classSolution{public:intnumberOfMatches(int n){int res =0;while(n >1){
res += n /2;
n =(n +1)/2;}return res;}};
classSolution{/**
* @param {number} n
* @return {number}
*/numberOfMatches(n){let res =0;while(n >1){
res += Math.floor(n /2);
n = Math.ceil(n /2);}return res;}}
publicclassSolution{publicintNumberOfMatches(int n){int res =0;while(n >1){
res += n /2;
n =(n +1)/2;}return res;}}
funcnumberOfMatches(n int)int{
res :=0for n >1{
res += n /2
n =(n +1)/2}return res
}
class Solution {funnumberOfMatches(n: Int): Int {var n = n
var res =0while(n >1){
res += n /2
n =(n +1)/2}return res
}}
classSolution{funcnumberOfMatches(_ n:Int)->Int{var n = n
var res =0while n >1{
res += n /2
n =(n +1)/2}return res
}}
Time & Space Complexity
Time complexity: O(logn)
Space complexity: O(1)
2. Math
Intuition
Every match eliminates exactly one team. To go from n teams to 1 winner, we need to eliminate n - 1 teams. Therefore, exactly n - 1 matches are played regardless of the tournament bracket structure.
classSolution:defnumberOfMatches(self, n:int)->int:return n -1
publicclassSolution{publicintnumberOfMatches(int n){return n -1;}}
classSolution{public:intnumberOfMatches(int n){return n -1;}};
classSolution{/**
* @param {number} n
* @return {number}
*/numberOfMatches(n){return n -1;}}
publicclassSolution{publicintNumberOfMatches(int n){return n -1;}}
funcnumberOfMatches(n int)int{return n -1}
class Solution {funnumberOfMatches(n: Int): Int {return n -1}}
classSolution{funcnumberOfMatches(_ n:Int)->Int{return n -1}}
Time & Space Complexity
Time complexity: O(1)
Space complexity: O(1)
Common Pitfalls
Overcomplicating with Simulation
Many beginners implement a full round-by-round simulation when the mathematical insight n - 1 gives the answer directly. Each match eliminates exactly one team, and to get from n teams to 1 winner requires eliminating n - 1 teams.
Off-by-One in Winner Calculation
When simulating, incorrectly calculating the number of teams advancing to the next round. The correct formula is (n + 1) / 2 (integer division), not n / 2, because the bye team also advances.
# Wrong: loses the bye team
n = n //2# Correct: includes bye team when n is odd
n =(n +1)//2