You are given an integer n, representing the total number of teams participating in a knockout-style tournament.
The tournament follows unusual rules:
If the number of teams is even, all teams are paired evenly, resulting in n / 2 matches, and exactly n / 2 teams qualify for the next round.
If the number of teams is odd, one team automatically moves to the next round without playing, while the remaining teams form pairs. This results in (n − 1) / 2 matches, and (n − 1) / 2 + 1 teams continue.
Your task is to determine the total number of matches played until only one champion remains.
Input: n = 10
Output: 9
Explanation:
Round 1: 10 teams → 5 matches, 5 advance Round 2: 5 teams → 2 matches, 3 advance Round 3: 3 teams → 1 match, 2 advance Round 4: 2 teams → 1 match, 1 winner Total matches = 5 + 2 + 1 + 1 = 9
Input: n = 5
Output: 4
Explanation:
Round 1: 5 teams → 2 matches, 3 advance Round 2: 3 teams → 1 match, 2 advance Round 3: 2 teams → 1 match, 1 winner Total matches = 2 + 1 + 1 = 4
Accepted:
Submission: