Knockout Bracket Check
You run sign-ups for a table tennis knockout tournament. Each round, players pair off and losers go home, so the field halves. That only works cleanly with 1, 2, 4, 8, 16, … players, and any other count means someone gets a free pass (a "bye") in some round.
The organiser can type any number, including mistakes like 0 or -4. Before printing a bracket, you need to check whether the count needs no byes at all.
Your task
Write isFullBracket(players). Return true if players is a power of two (1, 2, 4, 8, …), else false.
Zero and negative numbers return false. The number 1 returns true, because a single player is champion without playing. Try to answer without a loop.
isFullBracket(players: int) → bool
Examples
players = 16
true
16 halves to 8, 4, 2 and 1, so every round splits evenly. In binary, 16 is 10000, which has a single 1 bit.
players = 12
false
12 halves to 6 and then 3, and 3 players cannot pair off. Being even is not enough. In binary, 12 is 1100, which has two 1 bits.
players = 1
true
1 is 2 to the power 0. In binary it is just 1, which is a single 1 bit.
Constraints
- -2,147,483,648 ≤ players ≤ 2,147,483,647
Start with brute force
While the number is even, divide it by 2. If you end at exactly 1, it was a power of two. That takes at most about 31 steps, so speed is not the problem.
The edge cases are the problem. With 0, the loop "while even, halve" never ends, because 0 / 2 is still 0, and negatives need care too. The bit trick answers in one step, and the only edge case to handle is n > 0.
Hints
0 of 4 shown- Hint 1
Write 4, 8 and 16 in binary, then 6, 12 and 20. What do the first three share that the others do not?
- Hint 2
A power of two has exactly one 1 bit, so the question becomes whether
playershas exactly one bit set. - Hint 3
Write 8 and 7 in binary: 1000 and 0111. Then 12 and 11: 1100 and 1011. What does subtracting 1 do to the lowest 1 bit and the zeros below it? What does
n & (n - 1)give in each case? - Hint 4
n & (n - 1)erases the lowest 1 bit ofn. If that leaves 0, there was only one 1 bit. Checkn > 0first, because 0 and negatives are never powers of two and can fool the bit test.
Walkthrough and solution
Unlocks when you pass every test. Stuck for a while? Looking is fine too, as long as you then close it and write the code yourself.
How to solve it
In binary, a power of two is a single 1 followed by zeros, such as 1, 10, 100 and 1000. So the task is to check that a positive number has exactly one 1 bit.
Subtracting 1 turns the lowest 1 bit into 0 and every 0 below it into 1, and the higher bits stay the same. So n & (n - 1) is n with its lowest 1 bit removed.
- For
16 = 10000and15 = 01111, the AND gives00000, so 16 is a power of two. - For
12 = 1100and11 = 1011, the AND gives1000, which is 8 and not 0. A second bit survived, so 12 is not a power of two. - For
n = 0,0 & -1is 0, which would wrongly say yes. That is whyn > 0comes first.
function isFullBracket(players) {
// A power of two has exactly one 1 bit.
// players & (players - 1) clears the lowest 1 bit.
return players > 0 && (players & (players - 1)) === 0;
}def is_full_bracket(players):
# A power of two has exactly one 1 bit.
# players & (players - 1) clears the lowest 1 bit.
return players > 0 and (players & (players - 1)) == 0Run your code to see results here. Run checks the examples. Submit checks every test, including hidden edge cases.