FunctionRacerProblemsRace

Binary Gap

EasyBitsPython, JavaScript, TypeScript, Java, C++

Write n in binary. Return the largest distance between two neighbouring 1 bits (two 1s with only 0s between them). The distance between adjacent positions is 1. If the binary form has fewer than two 1s, return 0.

Example 1

n 22
returns 2

10110: the 1s at positions are 1 apart and 2 apart, so 2.

Example 2

n 8
returns 0

1000 has only one 1.

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