Drill

ProblemsTypeScript › games

Count tournament rounds

easygamesTypeScript

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

tournamentRounds(teams: int) → int

Solve it in the editor →

Where you start

function tournamentRounds(teams: number): number {
  
}

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 TypeScript
function tournamentRounds(teams: number): number {
  if (teams <= 1) return 0;
  let rounds = 0;
  let t = teams;
  while (t > 1) { t = Math.floor((t + 1) / 2); rounds++; }
  return rounds;
}

The same problem in another language

More games problems in TypeScript