Dado un entero n > 0, la tarea es encontrar si este entero tiene un patrón alternativo en su representación de bits. Por ejemplo, 5 tiene un patrón alternativo, es decir, 101.
Escriba «Sí» si tiene un patrón alternativo; de lo contrario, «No». Aquí el patrón alternativo puede ser como 0101 o 1010.
Ejemplos:
Input : 15 Output : No Explanation: Binary representation of 15 is 1111. Input : 10 Output : Yes Explanation: Binary representation of 10 is 1010.
Un enfoque simple es encontrar su equivalente binario y luego verificar sus bits.
C++
// C++ program to find if a number has alternate bit pattern #include <bits/stdc++.h> using namespace std; // Returns true if n has alternate bit pattern else false bool findPattern(int n) { // Store last bit int prev = n % 2; n = n / 2; // Traverse through remaining bits while (n > 0) { int curr = n % 2; // If current bit is same as previous if (curr == prev) return false; prev = curr; n = n / 2; } return true; } // Driver code int main() { int n = 10; if (findPattern(n)) cout << "Yes"; else cout << "No"; return 0; } // This code is contributed by Sania Kumari Gupta (kriSania804)
C
// C program to find if a number has alternate bit pattern #include <stdbool.h> #include <stdio.h> // Returns true if n has alternate bit pattern else false bool findPattern(int n) { // Store last bit int prev = n % 2; n = n / 2; // Traverse through remaining bits while (n > 0) { int curr = n % 2; // If current bit is same as previous if (curr == prev) return false; prev = curr; n = n / 2; } return true; } // Driver code int main() { int n = 10; if (findPattern(n)) printf("Yes"); else printf("No"); return 0; } // This code is contributed by Sania Kumari Gupta // (kriSania804)
Java
// Java program to find if a number has alternate bit // pattern class Test { // Returns true if n has alternate bit pattern // else false static boolean findPattern(int n) { // Store last bit int prev = n % 2; n = n / 2; // Traverse through remaining bits while (n > 0) { int curr = n % 2; // If current bit is same as previous if (curr == prev) return false; prev = curr; n = n / 2; } return true; } // Driver method public static void main(String args[]) { int n = 10; System.out.println(findPattern(n) ? "Yes" : "No"); } } // This code is contributed by Sania Kumari Gupta // (kriSania804)
Python3
# Python3 program to find if a number # has alternate bit pattern # Returns true if n has alternate # bit pattern else false def findPattern(n): # Store last bit prev = n % 2 n = n // 2 # Traverse through remaining bits while (n > 0): curr = n % 2 # If current bit is same as previous if (curr == prev): return False prev = curr n = n // 2 return True # Driver code n = 10 print("Yes") if(findPattern(n)) else print("No") # This code is contributed by Anant Agarwal.
C#
// Program to find if a number // has alternate bit pattern using System; class Test { // Returns true if n has alternate // bit pattern else returns false static bool findPattern(int n) { // Store last bit int prev = n % 2; n = n / 2; // Traverse through remaining bits while (n > 0) { int curr = n % 2; // If current bit is same as previous if (curr == prev) return false; prev = curr; n = n / 2; } return true; } // Driver method public static void Main() { int n = 10; Console.WriteLine(findPattern(n) ? "Yes" : "No"); } } // This code is contributed by Anant Agarwal.
PHP
<?php // PHP program to find if a // number has alternate // bit pattern // Returns true if n has // alternate bit pattern // else false function findPattern($n) { // Store last bit $prev = $n % 2; $n = $n / 2; // Traverse through // remaining bits while ($n > 0) { $curr = $n % 2; // If current bit is // same as previous if ($curr == $prev) return false; $prev = $curr; $n = floor($n / 2); } return true; } // Driver code $n = 10; if (findPattern($n)) echo "Yes"; else echo "No"; return 0; // This code is contributed by nitin mittal. ?>
Javascript
<script> // Javascript program to find if a number // has alternate bit pattern // Returns true if n has alternate // bit pattern else false function findPattern(n) { // Store last bit let prev = n % 2; n = Math.floor(n / 2); // Traverse through remaining bits while (n > 0) { let curr = n % 2; // If current bit is // same as previous if (curr == prev) return false; prev = curr; n = Math.floor(n / 2); } return true; } // Driver code let n = 10; if (findPattern(n)) document.write("Yes"); else document.write("No"); // This code is contributed by gfgking </script>
Producción:
Yes
Complejidad del tiempo: O(log 2 n)
Espacio Auxiliar: O(1)
Este artículo es una contribución de Sahil Chhabra (akku) . Si te gusta GeeksforGeeks y te gustaría contribuir, también puedes escribir un artículo usando write.geeksforgeeks.org o enviar tu artículo por correo a review-team@geeksforgeeks.org. Vea su artículo que aparece en la página principal de GeeksforGeeks y ayude a otros Geeks.
Escriba comentarios si encuentra algo incorrecto o si desea compartir más información sobre el tema tratado anteriormente.
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