Ctrl K

Knockout Match Count

Easy
A tournament runs as a single-elimination bracket: whoever loses is out. Each round, the remaining teams are split into pairs and every pair plays one match. If the number of teams is odd, one team gets a bye and moves on without playing. The tournament ends when only one team is left. Given the number of teams teams, return how many matches are played in total.
Example 1
teams=6 → 5

Rounds: 6 teams play 3 matches; 3 teams play 1 match and one gets a bye; 2 teams play the final. That is 3 + 1 + 1 = 5.

Example 2
teams=9 → 8

9 teams play 4 matches (5 advance), 5 play 2 (3 advance), 3 play 1 (2 advance), then the final: 4 + 2 + 1 + 1 = 8.

Example 3
teams=1 → 0

A single team wins outright with no matches played.

Constraints
  • 1 ≤ teams ≤ 10⁶
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsSum of Binary Strings EasyFold the Digits Down to One EasyCounting Off by Threes and Fives EasyAdding Long Numbers in Strings EasyGrowing Handouts Around the Circle Easy
loading Python…
Input
teams=6
Expected
5

Run checks the first 3 tests in your browser. Submit checks all 63 tests on the server, hidden ones included.