Knockout Bracket Check

Easybit manipulationAlso known as Power of Two

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

Example 1
Input
players = 16
Output
true
Why

16 halves to 8, 4, 2 and 1, so every round splits evenly. In binary, 16 is 10000, which has a single 1 bit.

Example 2
Input
players = 12
Output
false
Why

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.

Example 3
Input
players = 1
Output
true
Why

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

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.

Ctrl+Enter runs

Run your code to see results here. Run checks the examples. Submit checks every test, including hidden edge cases.

Write code