Check if a number has bits in alternate pattern | Set-2 O(1) Approach

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:

  1. 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.
  2. Check whether all the bits in num are set or not. Refer this post.

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation to check if a number 
// has bits in alternate pattern
#include <bits/stdc++.h>
  
using namespace std;
  
// function to check if all the bits are set or not
// in the binary representation of 'n'
bool allBitsAreSet(unsigned int n)
{
    // if true, then all bits are set
    if (((n + 1) & n) == 0)
        return true;
      
    // else all bits are not set
    return false;
}
  
// function to check if a number 
// has bits in alternate pattern
bool bitsAreInAltOrder(unsigned int n)
{
    unsigned int num = n ^ (n >> 1);
      
    // to check if all bits are set 
    // in 'num'
    return allBitsAreSet(num);        
}
  
// Driver program to test above
int main()
{
    unsigned int n = 10;
      
    if (bitsAreInAltOrder(n))
        cout << "Yes";
    else
        cout << "No";
          
    return 0;        

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation to check if a 
// number has bits in alternate pattern
class AlternateSetBits
{
    // function to check if all the bits 
    // are set or not in the binary 
    // representation of 'n'
    static boolean allBitsAreSet(int n)
    {
        // if true, then all bits are set
        if (((n + 1) & n) == 0)
            return true;
            
        // else all bits are not set
        return false;
    }
        
    // function to check if a number 
    // has bits in alternate pattern
    static boolean bitsAreInAltOrder(int n)
    {
        int num = n ^ (n >>> 1);
            
        // to check if all bits are set 
        // in 'num'
        return allBitsAreSet(num);        
    }
      
    // Driver Code
    public static void main(String args[])
    {
        int n = 10;
          
        if (bitsAreInAltOrder(n))
            System.out.println("Yes");
        else
            System.out.println("No");
    }
}
/* This code is contributed by Danish Kaleem */

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python implementation to check if a number 
# has bits in alternate pattern
  
# function to check if all the bits are set or not
# in the binary representation of 'n'
def allBitsAreSet(n):
      
    # if true, then all bits are set
    if (((n + 1) & n) == 0):
        return True;
      
    # else all bits are not set
    return False;
  
  
# function to check if a number 
# has bits in alternate pattern
def bitsAreInAltOrder(n):
    num = n ^ (n >> 1);
      
    # to check if all bits are set 
    # in 'num'
    return allBitsAreSet(num);   
  
  
# Driver code
n = 10;
  
if (bitsAreInAltOrder(n)):
    print("Yes");
else:
    print("No");
  
# This code is contributed by PrinciRaj1992 

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation to check if a
// number has bits in alternate pattern
using System;
  
class GFG {
  
    // function to check if all the bits
    // are set or not in the binary
    // representation of 'n'
    static bool allBitsAreSet(int n)
    {
        // if true, then all bits are set
        if (((n + 1) & n) == 0)
            return true;
  
        // else all bits are not set
        return false;
    }
  
    // function to check if a number
    // has bits in alternate pattern
    static bool bitsAreInAltOrder(int n)
    {
        int num = n ^ (n >> 1);
  
        // to check if all bits are set
        // in 'num'
        return allBitsAreSet(num);
    }
  
    // Driver Code
    public static void Main()
    {
        int n = 10;
  
        if (bitsAreInAltOrder(n))
            Console.WriteLine("Yes");
        else
            Console.WriteLine("No");
    }
  
}
  
// This code is contributed by Sam007

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP implementation to check 
// if a number has bits in 
// alternate pattern
  
// function to check if all the 
// bits are set or not in the 
// binary representation of 'n'
function allBitsAreSet($n)
{
    // if true, then all
    // bits are set
    if ((($n + 1) & $n) == 0)
        return true;
      
    // else all bits are not set
    return false;
}
  
// function to check if a number 
// has bits in alternate pattern
function bitsAreInAltOrder($n)
{
        $num = $n ^ ($n >> 1);
      
    // to check if all bits 
    // are set in 'num'
    return allBitsAreSet($num); 
}
  
// Driver Code
$n = 10;
  
if (bitsAreInAltOrder($n))
    echo "Yes";
else
    echo "No";
      
// This code is contributed by aj_36
?>

chevron_right



Output :

Yes

This article is contributed by Ayush Jauhari. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



My Personal Notes arrow_drop_up

Improved By : jit_t, princiraj1992



Article Tags :
Practice Tags :


2


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.