FunctionRacerProblemsRace

Block of Ones

EasyBitsPython, JavaScript, TypeScript, Java, C++

Return True if the binary form of the positive integer n consists of one unbroken run of 1 bits, possibly followed by some 0 bits (like 111, 1110000 or 1), and False otherwise. Equivalently, n can be written as 2a - 2b for some whole numbers a > b >= 0.

Example 1

n 6
returns true

6 is 110: a run of two 1 bits followed by a 0 bit.

Example 2

n 5
returns false

5 is 101: the two 1 bits are separated by a 0.

Constraints

Race itor take your time

Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.

More Bits problems