Skip to main content

Exactly One Consecutive Set Bits Pair - Solution & Explanation

EasyBit Manipulation7 min read
Practice this problem

Problem Statement

You are given an integer n.

Return true if its binary representation contains exactly one pair of consecutive set bits, and false otherwise.

 

Example 1:

Input: nums = 6

Output: true

Explanation:

  • Binary representation of 6 is 110.
  • There is exactly one pair of consecutive set bits ("11"). Thus, the answer is true​​​​​​​.

Example 2:

Input: nums = 5

Output: false

Explanation:

  • Binary representation of 5 is 101.
  • There are no consecutive set bits. Thus, the answer is false​​​​​​​.

 

Constraints:

  • 0 <= n <= 105

Approach Overview

Problem Overview: You are given an integer and need to determine whether its binary representation contains exactly one pair of consecutive set bits (11). Multiple pairs or no pairs should return false. The challenge is detecting these adjacent bits efficiently without scanning unnecessary states.

Approach 1: Binary String Scan (O(b) time, O(b) space)

Convert the integer to its binary string form and scan for occurrences of the substring "11". Iterate through the string and increment a counter whenever two adjacent characters are both '1'. If the counter equals exactly one at the end, the condition is satisfied. This approach is easy to reason about and good for quick prototypes, but it allocates extra memory for the string and processes every bit explicitly. The time complexity is O(b) where b is the number of bits in the representation, and space complexity is also O(b).

Approach 2: Bit-by-Bit Scan (O(b) time, O(1) space)

A more typical low-level approach uses bit manipulation. Iterate through the bits using right shifts. At each step, check the lowest two bits using (n & 3). When the value equals 3 (binary 11), you found a consecutive pair. Count such occurrences while shifting the number right by one bit each iteration. If the count ever exceeds one, you can stop early. This avoids string conversion and keeps memory usage constant. Time complexity remains O(b) and space complexity is O(1).

Approach 3: Bitwise Pair Detection with Mask (O(1) time, O(1) space)

The cleanest solution uses a well-known trick from bitwise operations. Compute x = n & (n << 1). This operation aligns each bit with its neighbor, leaving set bits only where consecutive ones exist. For example, if n = 0b10110, shifting left produces 0b101100, and the AND result marks the positions of 11. If there is exactly one consecutive pair, x will contain exactly one set bit. You can verify this using the classic power-of-two check: x != 0 && (x & (x - 1)) == 0. This approach runs in constant time because it performs a fixed number of bit operations and uses constant space.

Recommended for interviews: Start by explaining the straightforward scan to show you understand the definition of consecutive bits. Then present the optimized bit trick using n & (n << 1). Interviewers usually expect the bitwise solution because it demonstrates strong understanding of bit manipulation patterns and constant-time reasoning.

Solution

We use a variable pre to record the digit of the previous bit, initialized to pre = 0, and another variable vis to record whether a pair of consecutive set bits has already been found, initialized to vis = false.

Iterate through each binary bit of n, and denote the current binary bit as cur. If pre = cur = 1, and if vis = true at this moment, it indicates that there are multiple pairs of consecutive set bits, so we directly return false. Otherwise, we set vis to true. Then, we update pre = cur and continue to iterate through the next binary bit.

After the iteration ends, if vis = true, return true; otherwise, return false.

The time complexity is O(log n), and the space complexity is O(1).

Code

Python

Java

C++

Go

TypeScript

Try this approach in the editor →

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Binary String ScanO(b)O(b)Simple implementations or when working with string-based binary operations
Bit-by-Bit Shift ScanO(b)O(1)General case when iterating through bits without extra memory
Bitwise Mask Trick (n & (n << 1))O(1)O(1)Optimal solution using bit manipulation patterns

Video Solution

LeetCode 3950: Exactly One Consecutive Set Bits Pair | C++ Easiest Explanation • AeXel • 40 views views

Watch 2 more video solutions →

Frequently Asked Questions

Is Exactly One Consecutive Set Bits Pair easy or hard?
Exactly One Consecutive Set Bits Pair is considered an easy problem because the core idea relies on a simple bit manipulation trick. Once you recognize that n & (n << 1) reveals adjacent set bits, verifying that there is exactly one pair becomes straightforward.
Exactly One Consecutive Set Bits Pair Python/Java solution
In Python or Java, compute x = n & (n << 1) and verify that x has exactly one set bit using x != 0 and (x & (x - 1)) == 0. This works because consecutive ones produce a shared overlap after shifting, and the power-of-two check ensures only one pair exists.
How to solve Exactly One Consecutive Set Bits Pair in O(1)?
Use the expression n & (n << 1) to detect consecutive set bits. This produces a number where each set bit represents a location of '11'. If the result contains exactly one set bit, there is exactly one consecutive pair. A power-of-two check (x & (x - 1)) == 0 confirms the single occurrence.
What is the best approach for Exactly One Consecutive Set Bits Pair?
The most efficient approach uses a bit manipulation trick: compute x = n & (n << 1). This operation highlights positions where consecutive 1 bits occur. If x has exactly one set bit (x != 0 and (x & (x - 1)) == 0), then the number contains exactly one consecutive pair. The method runs in O(1) time and O(1) space.
Is Exactly One Consecutive Set Bits Pair asked at Google/Amazon/Meta?
Bit manipulation questions like detecting consecutive set bits commonly appear in interviews at companies such as Amazon, Google, and Meta. Variants often test familiarity with patterns like shifting, masking, and power-of-two checks.
What data structure is used in Exactly One Consecutive Set Bits Pair?
No complex data structures are required. The optimal solution relies purely on bitwise operations such as AND and left shift. Some beginner implementations temporarily use strings to inspect binary digits, but efficient solutions operate directly on integers.
What is the time complexity of Exactly One Consecutive Set Bits Pair?
The optimal bitwise solution runs in O(1) time because it performs a constant number of bit operations regardless of input size. Simpler approaches that iterate through bits or scan a binary string take O(b) time, where b is the number of bits in the integer.

Ready to solve this problem?

Practice Exactly One Consecutive Set Bits Pair with our built-in code editor and test cases.

Practice on FleetCode