Given an integer n, determine whether its binary representation contains at least one pair of adjacent set bits.
Return true if such a pair exists; otherwise, return false.
Examples :
Input: n = 1
Output: false
Explanation: There is no pair of adjacent set bit in the binary representation of 1.Input: n = 3
Output: true
Explanation: There is pair of adjacent set bit present in the binary representation of 3(0011).
Table of Content
Previous Bit Tracking - O(log n) Time and O(1) Space
The idea is to traverse the binary representation of n from right to left and keep track of the previously visited bit. If both the current bit and the previous bit are 1, then we have found two adjacent set bits.
- Initialize prev = 0 to store the previously processed bit.
- Extract the current least significant bit using n & 1.
- If both curr and prev are 1, return true.
- Update prev to the current bit and right-shift n by one position.
- If the entire binary representation is processed without finding two consecutive 1s, return false.
#include <bits/stdc++.h>
using namespace std;
bool adjacentBits(int n)
{
// Store the previously processed bit
int prev = 0;
while (n > 0)
{
// Extract the current least significant bit
int curr = n & 1;
// If current and previous bits are both 1,
// then adjacent set bits exist
if (curr == 1 && prev == 1)
return true;
// Update previous bit
prev = curr;
// Move to the next bit
n >>= 1;
}
// No adjacent set bits were found
return false;
}
int main()
{
int n = 3;
cout << (adjacentBits(n) ? "true" : "false");
return 0;
}
import java.util.*;
class GFG {
static boolean adjacentBits(int n)
{
// Store the previously processed bit
int prev = 0;
while (n > 0) {
// Extract the current least significant bit
int curr = n & 1;
// If current and previous bits are both 1,
// then adjacent set bits exist
if (curr == 1 && prev == 1)
return true;
// Update previous bit
prev = curr;
// Move to the next bit
n >>= 1;
}
// No adjacent set bits were found
return false;
}
public static void main(String[] args)
{
int n = 3;
System.out.println(adjacentBits(n) ? "true"
: "false");
}
}
def adjacentBits(n):
# Store the previously processed bit
prev = 0
while n > 0:
# Extract the current least significant bit
curr = n & 1
# If current and previous bits are both 1,
# then adjacent set bits exist
if curr == 1 and prev == 1:
return True
# Update previous bit
prev = curr
# Move to the next bit
n >>= 1
# No adjacent set bits were found
return False
# Driver Code
if __name__ == "__main__":
n = 3
print("true" if adjacentBits(n) else "false")
using System;
class GFG {
static bool adjacentBits(int n)
{
// Store the previously processed bit
int prev = 0;
while (n > 0) {
// Extract the current least significant bit
int curr = n & 1;
// If current and previous bits are both 1,
// then adjacent set bits exist
if (curr == 1 && prev == 1)
return true;
// Update previous bit
prev = curr;
// Move to the next bit
n >>= 1;
}
// No adjacent set bits were found
return false;
}
public static void Main()
{
int n = 3;
Console.WriteLine(adjacentBits(n) ? "true"
: "false");
}
}
function adjacentBits(n)
{
// Store the previously processed bit
let prev = 0;
while (n > 0) {
// Extract the current least significant bit
let curr = n & 1;
// If current and previous bits are both 1,
// then adjacent set bits exist
if (curr === 1 && prev === 1)
return true;
// Update previous bit
prev = curr;
// Move to the next bit
n >>= 1;
}
// No adjacent set bits were found
return false;
}
// Driver Code
let n = 3;
console.log(adjacentBits(n) ? "true" : "false");
Output
true
Bitwise AND with Right Shift - O(1) Time and O(1) Space
Instead of checking each pair separately, we can make all adjacent bits overlap by right-shifting n by one position. If n contains adjacent set bits, then n & (n >> 1) will contain a set bit.
- Right-shift n by one position to align each bit with its adjacent bit.
- Perform n & (n >> 1).
- If the result is non-zero, at least one pair of adjacent set bits exists.
- Return true if the result is non-zero; otherwise, return false.
Let's consider the following example for better understanding:
For n = 3:
- Binary representation of 3 is: 11
- n = 11
- (n >> 1) = 01
- n AND (n >> 1) = 01 -> non-zero. Hence, the output is true.
#include <bits/stdc++.h>
using namespace std;
bool adjacentBits(int n)
{
// Right shift n by one position so that
// adjacent bits become aligned
int shifted = n >> 1;
// If n and shifted have any common set bit,
// then n contains adjacent set bits
return (n & shifted) != 0;
}
int main()
{
int n = 3;
cout << (adjacentBits(n) ? "true" : "false");
return 0;
}
import java.util.*;
class GFG {
static boolean adjacentBits(int n)
{
// Right shift n by one position so that
// adjacent bits become aligned
int shifted = n >> 1;
// If n and shifted have any common set bit,
// then n contains adjacent set bits
return (n & shifted) != 0;
}
public static void main(String[] args)
{
int n = 3;
System.out.println(adjacentBits(n) ? "true"
: "false");
}
}
def adjacentBits(n):
# Right shift n by one position so that
# adjacent bits become aligned
shifted = n >> 1
# If n and shifted have any common set bit,
# then n contains adjacent set bits
return (n & shifted) != 0
# Driver Code
if __name__ == "__main__":
n = 3
print("true" if adjacentBits(n) else "false")
using System;
class GFG {
static bool adjacentBits(int n)
{
// Right shift n by one position so that
// adjacent bits become aligned
int shifted = n >> 1;
// If n and shifted have any common set bit,
// then n contains adjacent set bits
return (n & shifted) != 0;
}
public static void Main()
{
int n = 3;
Console.WriteLine(adjacentBits(n) ? "true"
: "false");
}
}
function adjacentBits(n)
{
// Right shift n by one position so that
// adjacent bits become aligned
let shifted = n >> 1;
// If n and shifted have any common set bit,
// then n contains adjacent set bits
return (n & shifted) !== 0;
}
// Driver Code
let n = 3;
console.log(adjacentBits(n) ? "true" : "false");
Output
true