Check if a number has bits in alternate pattern | Set 1
Last Updated :
30 Mar, 2023
Given an integer n > 0, the task is to find whether this integer has an alternate pattern in its bits representation. For example- 5 has an alternate pattern i.e. 101.
Print “Yes” if it has an alternate pattern otherwise “No”. Here alternate patterns can be like 0101 or 1010.
Examples:
Input : 15
Output : No
Explanation: Binary representation of 15 is 1111.
Input : 10
Output : Yes
Explanation: Binary representation of 10 is 1010.
A naive approach to check if a number has bits in alternate patterns:
A simple approach is to find its binary equivalent and then check its bits.
C++
#include <bits/stdc++.h>
using namespace std;
bool findPattern( int n)
{
int prev = n % 2;
n = n / 2;
while (n > 0) {
int curr = n % 2;
if (curr == prev)
return false ;
prev = curr;
n = n / 2;
}
return true ;
}
int main()
{
int n = 10;
if (findPattern(n))
cout << "Yes" ;
else
cout << "No" ;
return 0;
}
|
C
#include <stdbool.h>
#include <stdio.h>
bool findPattern( int n)
{
int prev = n % 2;
n = n / 2;
while (n > 0) {
int curr = n % 2;
if (curr == prev)
return false ;
prev = curr;
n = n / 2;
}
return true ;
}
int main()
{
int n = 10;
if (findPattern(n))
printf ( "Yes" );
else
printf ( "No" );
return 0;
}
|
Java
class Test {
static boolean findPattern( int n)
{
int prev = n % 2 ;
n = n / 2 ;
while (n > 0 ) {
int curr = n % 2 ;
if (curr == prev)
return false ;
prev = curr;
n = n / 2 ;
}
return true ;
}
public static void main(String args[])
{
int n = 10 ;
System.out.println(findPattern(n) ? "Yes" : "No" );
}
}
|
Python3
def findPattern(n):
prev = n % 2
n = n / / 2
while (n > 0 ):
curr = n % 2
if (curr = = prev):
return False
prev = curr
n = n / / 2
return True
n = 10
print ( "Yes" ) if (findPattern(n)) else print ( "No" )
|
C#
using System;
class Test {
static bool findPattern( int n)
{
int prev = n % 2;
n = n / 2;
while (n > 0) {
int curr = n % 2;
if (curr == prev)
return false ;
prev = curr;
n = n / 2;
}
return true ;
}
public static void Main()
{
int n = 10;
Console.WriteLine(findPattern(n) ? "Yes" : "No" );
}
}
|
PHP
<?php
function findPattern( $n )
{
$prev = $n % 2;
$n = $n / 2;
while ( $n > 0)
{
$curr = $n % 2;
if ( $curr == $prev )
return false;
$prev = $curr ;
$n = floor ( $n / 2);
}
return true;
}
$n = 10;
if (findPattern( $n ))
echo "Yes" ;
else
echo "No" ;
return 0;
?>
|
Javascript
<script>
function findPattern(n)
{
let prev = n % 2;
n = Math.floor(n / 2);
while (n > 0)
{
let curr = n % 2;
if (curr == prev)
return false ;
prev = curr;
n = Math.floor(n / 2);
}
return true ;
}
let n = 10;
if (findPattern(n))
document.write( "Yes" );
else
document.write( "No" );
</script>
|
Output:
Yes
Time Complexity: O(log2n)
Auxiliary Space: O(1)
An efficient approach to check if a number has bits in alternate patterns:
The idea is to check bitwise AND of n with (n>>1) and check if it returns 0. When bits are in an alternate fashion, the result will be 0 else not.
Below is the implementation of the above idea:
C++
#include <iostream>
using namespace std;
bool findPattern( int n)
{
if (n > 1)
return ((n & (n >> 1))) == 0;
else
return false ;
}
int main()
{
int n = 10;
if (findPattern(n))
cout << "Yes" << endl;
else
cout << "No" << endl;
return 0;
}
|
C
#include <stdbool.h>
#include <stdio.h>
bool findPattern( int n)
{
if (n > 1)
return (!(n & (n >> 1)));
else
return 0;
}
int main()
{
int n = 10;
if (findPattern(n))
printf ( "Yes" );
else
printf ( "No" );
return 0;
}
|
Java
public class Solution {
static boolean findPattern( int n)
{
if (n > 1 )
return ((n & (n >> 1 ))) == 0 ;
else
return false ;
}
public static void main(String[] args)
{
int n = 10 ;
if (findPattern(n))
System.out.println( "Yes" );
else
System.out.println( "No" );
}
}
|
Python3
def findPattern(n):
if n > 1 :
return ((n & (n >> 1 ))) = = 0
else :
return False
if __name__ = = '__main__' :
n = 10
if findPattern(n):
print ( "Yes" )
else :
print ( "No" )
|
Javascript
function findPattern(n) {
if (n > 1) {
return ((n & (n >> 1))) == 0;
} else {
return false ;
}
}
let n = 21;
if (findPattern(n)) {
console.log( "Yes" );
} else {
console.log( "No" );
}
|
C#
using System;
public class Solution {
static bool FindPattern( int n) {
if (n > 1)
return ((n & (n >> 1))) == 0;
else
return false ;
}
public static void Main( string [] args) {
int n = 10;
if (FindPattern(n))
Console.WriteLine( "Yes" );
else
Console.WriteLine( "No" );
}
}
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Reference:
http://stackoverflow.com/questions/38690278/program-to-check-whether-the-given-integer-has-an-alternate-pattern
Share your thoughts in the comments
Please Login to comment...