Comprobar si un número tiene bits en patrón alternativo | Conjunto-2 O(1) Aproximación

Dado un entero positivo n . El problema es verificar si este entero tiene un patrón alternativo en su representación binaria o no. Aquí patrón alternativo significa que los bits activados y desactivados en n ocurren en orden alternativo. Por ejemplo, 5 tiene un patrón alternativo, es decir, 101. 
Escriba «Sí» si tiene un patrón alternativo; de lo contrario, «No». 
Nota: 0 < n.
Ejemplos: 
 

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.

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

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

Javascript

<script>
// Javascript 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)
    {
        let num = n ^ (n >>> 1);
              
        // to check if all bits are set 
        // in 'num'
        return allBitsAreSet(num);        
    }
    
// driver function
  
        let n = 10;
            
        if (bitsAreInAltOrder(n))
            document.write("Yes");
        else
            document.write("No");
   
 // This code is contributed by code_hunt.
</script>    

Publicación traducida automáticamente

Artículo escrito por GeeksforGeeks-1 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *