Count of Matches in Tournament

A tournament starts with n teams. In each round, if the number of teams is even, they pair up and n / 2 matches are played, with n / 2 winners advancing. If it is odd, one random team advances without playing, (n - 1) / 2 matches are played, and (n - 1) / 2 + 1 teams advance. Return the total number of matches played until a winner is decided.

30:00

Count of Matches in Tournament
easy
Free written solution
Topics
Companies

A tournament starts with n teams. In each round, if the number of teams is even, they pair up and n / 2 matches are played, with n / 2 winners advancing. If it is odd, one random team advances without playing, (n - 1) / 2 matches are played, and (n - 1) / 2 + 1 teams advance. Return the total number of matches played until a winner is decided.

Example 1:
Input: {"n":7}
Output: 6
Constraints:
  • 1≤n≤2001 \leq n \leq 200

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"n":7}

7 teams enter the tournament. Each match sends one team home.

7 teams at the start

rounds so far

none yet

teams knocked out = matches (0 of 6)

matches = 0

Variables
VariableValue
n7
DepthFunction Call
Stack empty
0/15