Check if a number has bits in alternate pattern | Set-2 O(1) Approach
Last Updated :
15 Jul, 2022
Given a positive integer n. The problem is to check whether this integer has an alternate pattern in its binary representation or not. Here alternate pattern means that the set and unset bits in n occur in alternate order. For example- 5 has an alternate pattern i.e. 101.
Print “Yes” if it has an alternate pattern otherwise “No”.
Note: 0 < n.
Examples :
Input : 10
Output : Yes
(10)10 = (1010)2, has an alternate pattern.
Input : 12
Output : No
(12)10 = (1100)2, does not have an alternate pattern.
Simple Approach: It has been discussed in this post having a time complexity of O(n).
Efficient Approach: Following are the steps:
- Calculate num = n ^ (n >> 1). If n has an alternate pattern, then n ^ (n >> 1) operation will produce a number having set bits only. ‘^’ is a bitwise XOR operation.
- Check whether all the bits in num are set or not. Refer this post.
C++
#include <bits/stdc++.h>
using namespace std;
bool allBitsAreSet(unsigned int n)
{
if (((n + 1) & n) == 0)
return true ;
return false ;
}
bool bitsAreInAltOrder(unsigned int n)
{
unsigned int num = n ^ (n >> 1);
return allBitsAreSet(num);
}
int main()
{
unsigned int n = 10;
if (bitsAreInAltOrder(n))
cout << "Yes" ;
else
cout << "No" ;
return 0;
}
|
C
#include <stdio.h>
#include <stdbool.h>
bool allBitsAreSet(unsigned int n)
{
if (((n + 1) & n) == 0)
return true ;
return false ;
}
bool bitsAreInAltOrder(unsigned int n)
{
unsigned int num = n ^ (n >> 1);
return allBitsAreSet(num);
}
int main()
{
unsigned int n = 10;
if (bitsAreInAltOrder(n))
printf ( "Yes" );
else
printf ( "No" );
return 0;
}
|
Java
class AlternateSetBits
{
static boolean allBitsAreSet( int n)
{
if (((n + 1 ) & n) == 0 )
return true ;
return false ;
}
static boolean bitsAreInAltOrder( int n)
{
int num = n ^ (n >>> 1 );
return allBitsAreSet(num);
}
public static void main(String args[])
{
int n = 10 ;
if (bitsAreInAltOrder(n))
System.out.println( "Yes" );
else
System.out.println( "No" );
}
}
|
Python3
def allBitsAreSet(n):
if (((n + 1 ) & n) = = 0 ):
return True ;
return False ;
def bitsAreInAltOrder(n):
num = n ^ (n >> 1 );
return allBitsAreSet(num);
n = 10 ;
if (bitsAreInAltOrder(n)):
print ( "Yes" );
else :
print ( "No" );
|
C#
using System;
class GFG {
static bool allBitsAreSet( int n)
{
if (((n + 1) & n) == 0)
return true ;
return false ;
}
static bool bitsAreInAltOrder( int n)
{
int num = n ^ (n >> 1);
return allBitsAreSet(num);
}
public static void Main()
{
int n = 10;
if (bitsAreInAltOrder(n))
Console.WriteLine( "Yes" );
else
Console.WriteLine( "No" );
}
}
|
PHP
<?php
function allBitsAreSet( $n )
{
if ((( $n + 1) & $n ) == 0)
return true;
return false;
}
function bitsAreInAltOrder( $n )
{
$num = $n ^ ( $n >> 1);
return allBitsAreSet( $num );
}
$n = 10;
if (bitsAreInAltOrder( $n ))
echo "Yes" ;
else
echo "No" ;
?>
|
Javascript
<script>
function allBitsAreSet(n)
{
if (((n + 1) & n) == 0)
return true ;
return false ;
}
function bitsAreInAltOrder(n)
{
let num = n ^ (n >>> 1);
return allBitsAreSet(num);
}
let n = 10;
if (bitsAreInAltOrder(n))
document.write( "Yes" );
else
document.write( "No" );
</script>
|
Output :
Yes
Time Complexity : O(1)
Auxiliary Space : O(1)
Share your thoughts in the comments
Please Login to comment...