Count tournament rounds
A single-elimination tournament keeps halving the field until one champion remains. How many rounds does that take?
- Each round pairs up as many teams as possible; odd teams get a bye.
- The number of teams per round is ceil(teams / 2).
- One team or fewer needs zero rounds.
tournamentRounds(teams: int) → int
Go needs a compiler and Drill does not host one yet, so this page is the reference rather than an exercise: the problem, worked examples, and the solution in full. To type it out, the same problem runs in Python.
Where you start
func tournamentRounds(teams int) int {
}
Worked examples
| Call | Result |
|---|---|
tournamentRounds(1) | 0 |
tournamentRounds(2) | 1 |
tournamentRounds(4) | 2 |
tournamentRounds(5) | 3 |
Hint
Repeatedly halve (rounding up) and count until one remains.
Reference solution in Go
func tournamentRounds(teams int) int {
if teams <= 1 {
return 0
}
rounds, t := 0, teams
for t > 1 {
t = (t + 1) / 2
rounds++
}
return rounds
}