Dado un número entero N , la tarea es encontrar bit a bit y (&) de todos los números pares del 1 al N.
Ejemplos:
Entrada: 2
Salida: 2Entrada: 10
Salida: 0
Explicación: Bitwise y de 2, 4, 6, 8 y 10 son 0.
Enfoque ingenuo: inicialice el resultado como 2. Repita el bucle de 4 a n (para todos los números pares) y actualice el resultado encontrando bit a bit y (&).
A continuación se muestra la implementación del enfoque:
C++
// C++ implementation of the above approach #include <iostream> using namespace std; // Function to return the bitwise & // of all the even numbers upto N int bitwiseAndTillN(int n) { // Initialize result as 2 int result = 2; for (int i = 4; i <= n; i = i + 2) { result = result & i; } return result; } // Driver code int main() { int n = 2; cout << bitwiseAndTillN(n); return 0; }
Java
// Java implementation of the above approach class GFG { // Function to return the bitwise & // of all the even numbers upto N static int bitwiseAndTillN(int n) { // Initialize result as 2 int result = 2; for (int i = 4; i <= n; i = i + 2) { result = result & i; } return result; } // Driver code public static void main (String[] args) { int n = 2; System.out.println(bitwiseAndTillN(n)); } } // This code is contributed by AnkitRai01
Python3
# Python3 implementation of the above approach # Function to return the bitwise & # of all the even numbers upto N def bitwiseAndTillN(n) : # Initialize result as 2 result = 2; for i in range(4, n + 1, 2) : result = result & i; return result; # Driver code if __name__ == "__main__" : n = 2; print(bitwiseAndTillN(n)); # This code is contributed by AnkitRai01
C#
// C# implementation of the above approach using System; class GFG { // Function to return the bitwise & // of all the even numbers upto N static int bitwiseAndTillN(int n) { // Initialize result as 2 int result = 2; for (int i = 4; i <= n; i = i + 2) { result = result & i; } return result; } // Driver code public static void Main() { int n = 2; Console.WriteLine(bitwiseAndTillN(n)); } } // This code is contributed by AnkitRai01
Javascript
<script> // Javascript implementation of the above approach // Function to return the bitwise & // of all the even numbers upto N function bitwiseAndTillN(n) { // Initialize result as 2 let result = 2; for (let i = 4; i <= n; i = i + 2) { result = result & i; } return result; } // Driver code let n = 2; document.write(bitwiseAndTillN(n)); </script>
2
Enfoque eficiente: el enfoque eficiente es devolver 2 para N menos de 4 y devolver 0 para todos los N>=4 porque bit a bit y de 2 y 4 es 0 y bit a bit y de 0 con cualquier número es 0.
A continuación se muestra la implementación del enfoque :
C++
// C++ implementation of the above approach #include <iostream> using namespace std; // Function to return the bitwise & // of all the numbers upto N int bitwiseAndTillN(int n) { if (n < 4) return 2; else return 0; } int main() { int n = 2; cout << bitwiseAndTillN(n); return 0; }
Java
// Java implementation of the above approach class GFG { // Function to return the bitwise & // of all the numbers upto N static int bitwiseAndTillN(int n) { if (n < 4) return 2; else return 0; } // Driver code public static void main (String[] args) { int n = 2; System.out.println(bitwiseAndTillN(n)); } } // This code is contributed by AnkitRai01
Python3
# Python3 implementation of the above approach # Function to return the bitwise & # of all the numbers upto N def bitwiseAndTillN( n): if (n < 4): return 2 else: return 0 # Driver code n = 2 print(bitwiseAndTillN(n)) # This code is contributed by ANKITKUMAR34
C#
// C# implementation of the above approach using System; class GFG { // Function to return the bitwise & // of all the numbers upto N static int bitwiseAndTillN(int n) { if (n < 4) return 2; else return 0; } // Driver code public static void Main() { int n = 2; Console.WriteLine(bitwiseAndTillN(n)); } } // This code is contributed by AnkitRai01
Javascript
<script> // JavaScript implementation of the above approach // Function to return the bitwise & // of all the numbers upto N function bitwiseAndTillN(n) { if (n < 4) return 2; else return 0; } // driver code let n = 2; document.write (bitwiseAndTillN(n)); // this code is contributed by shivanisinghss2110 </script>
2
Complejidad temporal: O(1)
Espacio auxiliar: O(1)
Publicación traducida automáticamente
Artículo escrito por ANKITKUMAR34 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA