Números ficticios (sin 1 consecutivos en binario) – Enfoque O(1)

Dado un entero positivo n . El problema es verificar si el número es Fibbinary Number o no. Los números fibbinarios son números enteros cuya representación binaria no contiene números consecutivos.
Ejemplos: 
 

Input : 10
Output : Yes
Explanation: 1010 is the binary representation 
             of 10 which does not contains any 
             consecutive 1's.

Input : 11
Output : No
Explanation: 1011 is the binary representation 
             of 11, which contains consecutive 
             1's.

Enfoque: Si (n & (n >> 1)) == 0, entonces ‘n’ es un número ficticio De lo contrario, no.
 

C++

// C++ implementation to check whether a number
// is fibbinary or not
#include <bits/stdc++.h>
using namespace std;
 
// function to check whether a number
// is fibbinary or not
bool isFibbinaryNum(unsigned int n) {
 
  // if the number does not contain adjacent ones
  // then (n & (n >> 1)) operation results to 0
  if ((n & (n >> 1)) == 0)
    return true;
 
  // not a fibbinary number
  return false;
}
 
// Driver program to test above
int main() {
  unsigned int n = 10;
  if (isFibbinaryNum(n))
    cout << "Yes";
  else
    cout << "No";
  return 0;
}

Java

// Java implementation to check whether
// a number is fibbinary or not
class GFG {
     
    // function to check whether a number
    // is fibbinary or not
    static boolean isFibbinaryNum(int n) {
     
        // if the number does not contain
        // adjacent ones then (n & (n >> 1))
        // operation results to 0
        if ((n & (n >> 1)) == 0)
            return true;
         
        // not a fibbinary number
        return false;
    }
     
    // Driver program to test above
    public static void main(String[] args) {
 
        int n = 10;
 
        if (isFibbinaryNum(n) == true)
            System.out.println("Yes");
        else
            System.out.println("No");
    }
}
 
// This code is contributed by
// Smitha Dinesh Semwal

Python3

# Python3 program to check if a number
# is fibbinary number or not
 
# function to check whether a number
# is fibbinary or not
def isFibbinaryNum( n):
     
    # if the number does not contain adjacent
    # ones then (n & (n >> 1)) operation
    # results to 0
    if ((n & (n >> 1)) == 0):
        return 1
         
    # Not a fibbinary number
    return 0
 
# Driver code
n = 10
 
if (isFibbinaryNum(n)):
    print("Yes")
else:
    print("No")
     
# This code is contributed by sunnysingh

C#

// C# implementation to check whether
// a number is fibbinary or not
using System;
 
class GFG {
     
    // function to check whether a number
    // is fibbinary or not
    static bool isFibbinaryNum(int n) {
     
        // if the number does not contain
        // adjacent ones then (n & (n >> 1))
        // operation results to 0
        if ((n & (n >> 1)) == 0)
            return true;
         
        // not a fibbinary number
        return false;
    }
     
    // Driver program to test above
    public static void Main() {
 
        int n = 10;
 
        if (isFibbinaryNum(n) == true)
            Console.WriteLine("Yes");
        else
            Console.WriteLine("No");
    }
}
 
// This code is contributed by vt_m.

PHP

<?php
// PHP implementation to check whether
// a number is fibbinary or not
 
// function to check whether a number
// is fibbinary or not
function isFibbinaryNum($n)
{
     
    // if the number does not contain
    // adjacent ones then (n & (n >> 1))
    // operation results to 0
    if (($n & ($n >> 1)) == 0)
        return true;
     
    // not a fibbinary number
    return false;
}
 
// Driver code
$n = 10;
if (isFibbinaryNum($n))
    echo "Yes";
else
    echo "No";
 
// This code is contributed by mits
?>

Javascript

<script>
 
// JavaScript program implementation to find  whether
// a number is fibbinary or not
  
  // function to check whether a number
    // is fibbinary or not
    function isFibbinaryNum(n) {
       
        // if the number does not contain
        // adjacent ones then (n & (n >> 1))
        // operation results to 0
        if ((n & (n >> 1)) == 0)
            return true;
           
        // not a fibbinary number
        return false;
    }
       
// Driver code
 
        let n = 10;
   
        if (isFibbinaryNum(n) == true)
            document.write("Yes");
        else
            document.write("No");
 
// This code is contributed by souravghosh0416.
</script>

Producción : 
 

Yes

Complejidad Temporal: O(1).
 Espacio Auxiliar: O(1).

Publicación traducida automáticamente

Artículo escrito por ayushjauhari14 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 *