Drill

ProblemsC# › games

Count tournament rounds

easygamesMathC#

A single-elimination tournament keeps halving the field until one champion remains. How many rounds does that take?

TournamentRounds(teams: int) → int

C# 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.

Solve it in Python →

Where you start

public int TournamentRounds(int teams) {
    
}

Worked examples

CallResult
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 C#
public int TournamentRounds(int teams) {
    if (teams <= 1) return 0;
    int rounds = 0, t = teams;
    while (t > 1) { t = (t + 1) / 2; rounds++; }
    return rounds;
}

The same problem in another language

More games problems in C#