Dado un número entero N , la tarea es encontrar si los bits de N se pueden organizar de manera alterna, es decir, 0101… o 10101… . Suponga que N se representa como un número entero de 32 bits.
Ejemplos:
Entrada: N = 23
Salida: No
“000000000000000000000000000010111” es la representación binaria de 23
y la permutación de bits requerida no es posible.
Entrada: N = 524280
Salida: Sí
binario (524280) = «00000000000001111111111111111000» que se puede
reorganizar a «01010101010101010101010101010101».
Enfoque: dado que el entero dado debe representarse en 32 bits y el número de 1 debe ser igual al número de 0 en su representación binaria para satisfacer la condición dada. Entonces, la cantidad de bits establecidos en N debe ser 16, lo que se puede calcular fácilmente usando __builtin_popcount()
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; const int TOTAL_BITS = 32; // Function that returns true if it is // possible to arrange the bits of // n in alternate fashion bool isPossible(int n) { // To store the count of 1s in the // binary representation of n int cnt = __builtin_popcount(n); // If the number set bits and the // number of unset bits is equal if (cnt == TOTAL_BITS / 2) return true; return false; } // Driver code int main() { int n = 524280; if (isPossible(n)) cout << "Yes"; else cout << "No"; return 0; }
Java
// Java implementation of the approach import java.util.*; class GFG { static int TOTAL_BITS = 32; // Function that returns true if it is // possible to arrange the bits of // n in alternate fashion static boolean isPossible(int n) { // To store the count of 1s in the // binary representation of n int cnt = Integer.bitCount(n); // If the number set bits and the // number of unset bits is equal if (cnt == TOTAL_BITS / 2) return true; return false; } // Driver code static public void main (String []arr) { int n = 524280; if (isPossible(n)) System.out.println("Yes"); else System.out.println("No"); } } // This code is contributed by 29AjayKumar
Python3
# Python3 implementation of the approach TOTAL_BITS = 32; # Function that returns true if it is # possible to arrange the bits of # n in alternate fashion def isPossible(n) : # To store the count of 1s in the # binary representation of n cnt = bin(n).count('1'); # If the number set bits and the # number of unset bits is equal if (cnt == TOTAL_BITS // 2) : return True; return False; # Driver code if __name__ == "__main__" : n = 524280; if (isPossible(n)) : print("Yes"); else : print("No"); # This code is contributed by AnkitRai01
C#
// C# implementation of the above approach using System; class GFG { static int TOTAL_BITS = 32; static int CountBits(int value) { int count = 0; while (value != 0) { count++; value &= value - 1; } return count; } // Function that returns true if it is // possible to arrange the bits of // n in alternate fashion static bool isPossible(int n) { // To store the count of 1s in the // binary representation of n int cnt = CountBits(n); // If the number set bits and the // number of unset bits is equal if (cnt == TOTAL_BITS / 2) return true; return false; } // Driver code public static void Main (String []arr) { int n = 524280; if (isPossible(n)) Console.WriteLine("Yes"); else Console.WriteLine("No"); } } // This code is contributed by Mohit kumar
Javascript
<script> // Javascript implementation of the approach const TOTAL_BITS = 32; function CountBits(value) { let count = 0; while (value != 0) { count++; value &= value - 1; } return count; } // Function that returns true if it is // possible to arrange the bits of // n in alternate fashion function isPossible(n) { // To store the count of 1s in the // binary representation of n let cnt = CountBits(n); // If the number set bits and the // number of unset bits is equal if (cnt == parseInt(TOTAL_BITS / 2)) return true; return false; } // Driver code let n = 524280; if (isPossible(n)) document.write("Yes"); else document.write("No"); // This code is contributed by subhammahato348. </script>
Yes
Complejidad de tiempo: O (log n)
Espacio Auxiliar: O(1)