LeetCode-in-Java

3950. Exactly One Consecutive Set Bits Pair

Easy

You are given an integer n.

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

Example 1:

Input: n = 6

Output: true

Explanation:

Example 2:

Input: n = 5

Output: false

Explanation:

Constraints:

Solution

public class Solution {
    public boolean consecutiveSetBits(int n) {
        int cnt = 0;
        while (n != 0) {
            if ((n & 3) == 3) {
                cnt++;
            }
            n = n >> 1;
        }
        return cnt == 1;
    }
}