Exactly One Consecutive Set Bits Pair - Solution & Explanation
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 istrueāāāāāāā.
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
Detailed Complexity Analysis
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Binary String Scan | O(b) | O(b) | Simple implementations or when working with string-based binary operations |
| Bit-by-Bit Shift Scan | O(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 Python/Java solution
How to solve Exactly One Consecutive Set Bits Pair in O(1)?
What is the best approach for Exactly One Consecutive Set Bits Pair?
Is Exactly One Consecutive Set Bits Pair asked at Google/Amazon/Meta?
What data structure is used in Exactly One Consecutive Set Bits Pair?
What is the time complexity of Exactly One Consecutive Set Bits Pair?
Ready to solve this problem?
Practice Exactly One Consecutive Set Bits Pair with our built-in code editor and test cases.
Practice on FleetCodeProblem Info
Table of Contents
Practice this problem
Open in Editor