Given a non-negative integer p, find the smallest integer greater than p whose binary representation does not contain the substring "11" (two consecutive 1s).
For example, the number 6 in binary is "110", which contains "11". The number 5 in binary is "101", which does not contain "11".
Return the smallest integer strictly greater than p that satisfies this condition.
Function Signature
def smallest_without_consecutive_ones(p: int) -> int:
Rules
-
The binary representation is the standard base-2 form without leading zeros.
-
The answer must be strictly greater than
p
, even when
p
itself has no
"11"
. For
p = 5
, the answer is 8, because 6 (
110
) and 7 (
111
) both contain
"11"
.
Constraints
Examples
Input: p = 6
Output: 8
The only candidate below 8 is 7, which is 111 in binary and contains "11". 8 is 1000, which has no two adjacent 1s.
Input: p = 4
Output: 5
5 is 101 in binary, which has no two adjacent 1s, so the first candidate already works.
Input: p = 10
Output: 16
11 (1011), 12 (1100), 13 (1101), 14 (1110) and 15 (1111) all contain "11". 16 is 10000, the first value above 10 that does not.