Talentd
FleetCode

3950. Exactly One Consecutive Set Bits Pair

Easy50.4% acceptance

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

Hints open one at a time

Hint 1
Do as told.
6:40
Still stuck?Read the written solution, or watch a walkthrough.
Open solution
Loading editor…
1class Solution:
2 def consecutiveSetBits(self, n: int) -> bool:
3
Starter codePython 3Spaces: 4
Examples
Input
6
Input
5
Your code is saved on this device.Solution