Check if a number has bits in alternate pattern | Set2 O(1) Approach (original) (raw)

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:

  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.

Try It Yourselfredirect icon

C++ `

// 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; }

// This code is contributed by Sania Kumari Gupta (kriSania804)

C

// C implementation to check if a number has bits in // alternate pattern #include <stdio.h> #include <stdbool.h>

// 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)) printf("Yes"); else printf("No"); return 0; }

// This code is contributed by Sania Kumari Gupta (kriSania804)

Java

// 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 */

Python3

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

C#

// 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

PHP

num=num = 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 ?>

JavaScript

`

Output :

Yes

Time Complexity : O(1)

Auxiliary Space : O(1)