Quick Overview

Given a non-negative integer p, find the smallest integer strictly greater than p whose binary representation has no two adjacent 1 bits. The problem tests bit manipulation, reasoning about binary carries, and finding a search fast enough for inputs up to one billion, along with an argument that it stays correct.

Smallest Integer Greater Than p With No Consecutive 1s in Binary

Company: Airbnb

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

Given a non-negative integer `p`, find the smallest integer greater than `p` whose binary representation does not contain the substring `"11"` (two consecutive `1`s). 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 ```python 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 - `0 <= p <= 10^9` ### Examples ```text 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. ```text Input: p = 4 Output: 5 ``` 5 is `101` in binary, which has no two adjacent 1s, so the first candidate already works. ```text 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.

Overview: Given a non-negative integer p, find the smallest integer strictly greater than p whose binary representation has no two adjacent 1 bits. The problem tests bit manipulation, reasoning about binary carries, and finding a search fast enough for inputs up to one billion, along with an argument that it stays correct.

Given a non-negative integer `p`, return the smallest integer strictly greater than `p` whose binary representation does not contain the substring `"11"`, meaning it has no two adjacent `1` bits. The binary representation is the standard base-2 form without leading zeros. For example, `6` is `"110"`, which contains `"11"`, and `5` is `"101"`, which does not. 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"`. Implement `smallest_without_consecutive_ones(p)`, which receives the integer `p` and returns that smallest integer. ### Example 1 ```text 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 `1` bits. ### Example 2 ```text 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. ### Constraints - `0 <= p <= 10^9` - The answer is at most `2^30 = 1,073,741,824` (a power of two has a single `1` bit, and `2^30` is above `10^9`), so it never exceeds `2^31 - 1` and fits in a signed 32-bit integer: `int` in Java and C++ is enough.

Constraints

  • 0 <= p <= 10^9
  • The answer is at most 2^30 = 1,073,741,824, so it fits in a signed 32-bit integer (int in Java and C++).

Examples

Input: (0,)

Expected Output: 1

Explanation: Smallest input: 1 (binary 1) is the first value above 0 and has no 11.

Input: (1,)

Expected Output: 2

Explanation: 2 is 10 in binary, so the first candidate already works.

Hints

  1. The answer must be larger than p even when p has no 11 of its own, so the first value worth checking is p + 1.
  2. p can be as large as 10^9, and the gap between p and the answer can exceed 2 * 10^8, so checking every integer after p one by one can be too slow.
  3. A power of two has a single 1 bit, so the answer never needs more than one bit beyond the binary length of p + 1.

Smallest Larger Integer Without Adjacent 1 Bits

mediumAirbnbSoftware Engineer

Given a non-negative integer p, return the smallest integer strictly greater than p whose binary representation does not contain the substring "11", meaning it has no two adjacent 1 bits.

The binary representation is the standard base-2 form without leading zeros. For example, 6 is "110", which contains "11", and 5 is "101", which does not.

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".

Implement smallest_without_consecutive_ones(p), which receives the integer p and returns that smallest integer.

Example 1

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 1 bits.

Example 2

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.

Constraints

  • 0 <= p <= 10^9
  • The answer is at most 2^30 = 1,073,741,824 (a power of two has a single 1 bit, and 2^30 is above 10^9), so it never exceeds 2^31 - 1 and fits in a signed 32-bit integer: int in Java and C++ is enough.

Examples

Example 1
Input
(0,)
Output
1
Notes
Smallest input: 1 (binary 1) is the first value above 0 and has no 11.
Example 2
Input
(1,)
Output
2
Notes
2 is 10 in binary, so the first candidate already works.

Constraints

  • •0 <= p <= 10^9
  • •The answer is at most 2^30 = 1,073,741,824, so it fits in a signed 32-bit integer (int in Java and C++).

Comments (0)

No comments yet. Be the first to comment!

Submit your answer

Approved first answer today earns 30 XP
Bold⌘BItalic⌘IInline code⌘`Bullet listTab to nest (○ ■)Numbered listTab to nest (a. i.)QuoteCode block
Formatting help
Signature
def smallest_without_consecutive_ones(p: int) -> int:
Sample call
smallest_without_consecutive_ones(0)
Language
Case 1
Input
(0,)
Expected
1
Case 2
Input
(1,)
Expected
2

Browse More Questions

Show the approach

Approach

Start with n = p + 1, the smallest value strictly greater than p. If n has no two adjacent 1 bits (n & (n >> 1) is 0), it is the answer. Otherwise let j be the lower bit of the highest adjacent pair, which is the highest set bit of n & (n >> 1). Every integer from n up to the end of the block that keeps the bits of n at positions j and above unchanged still contains that pair, so none of them can be the answer. The first integer past that block is ((n >> j) + 1) << j: add 1 at bit j and clear every lower bit. Replace n with it and repeat.

Invariant: every integer from p + 1 up to n - 1 contains 11, and n never passes the answer, because the loop only skips values that share a prefix containing 11. Adding 1 at bit j when bits j and j + 1 are both set carries at least into bit j + 2, so after each step the lowest set bit of n is at position j + 2 or higher, and any pair found next sits at least two positions higher. With values below 2^31 the loop runs at most 15 times.

Edge cases: p = 0 gives 1. When p has no 11 of its own the answer is still larger than p (p = 5 gives 8). A carry can create a new pair higher up (p = 10: 1011 becomes 1100, then 10000). For every p from 715827882 (binary 1010...10 with 30 bits, the largest 30-bit value without 11) up to 10^9 the answer is 2^30 = 1,073,741,824, which still fits in a signed 32-bit integer; the Java and C++ references compute in 64 bits anyway.

Time complexity:
O(log p)
Space complexity:
O(1)