Dado un entero N , la tarea es encontrar el AND bit a bit (&) de todos los enteros impares del rango [1, N] .
Ejemplos:
Entrada: N = 7
Salida: 1
(1 y 3 y 5 y 7) = 1Entrada: N = 1
Salida: 1
Enfoque ingenuo: a partir de 1 , bit a bit Y todos los números impares ≤ N .
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ implementation of the approach #include <iostream> using namespace std; // Function to return the bitwise AND // of all the odd integers from // the range [1, n] int bitwiseAndOdd(int n) { // Initialize result to 1 int result = 1; // Starting from 3, bitwise AND // all the odd integers less // than or equal to n for (int i = 3; i <= n; i = i + 2) { result = (result & i); } return result; } // Driver code int main() { int n = 10; cout << bitwiseAndOdd(n); return 0; }
Java
// Java implementation of the approach class GFG { // Function to return the bitwise AND // of all the odd integers from // the range [1, n] static int bitwiseAndOdd(int n) { // Initialize result to 1 int result = 1; // Starting from 3, bitwise AND // all the odd integers less // than or equal to n for (int i = 3; i <= n; i = i + 2) { result = (result & i); } return result; } // Driver code public static void main (String[] args) { int n = 10; System.out.println(bitwiseAndOdd(n)); } } // This code is contributed by AnkitRai01
Python3
# Python3 implementation of the approach # Function to return the bitwise AND # of all the odd integers from # the range [1, n] def bitwiseAndOdd(n) : # Initialize result to 1 result = 1; # Starting from 3, bitwise AND # all the odd integers less # than or equal to n for i in range(3, n + 1, 2) : result = (result & i); return result; # Driver code if __name__ == "__main__" : n = 10; print(bitwiseAndOdd(n)); # This code is contributed by AnkitRai01
C#
// C# implementation of the approach using System; class GFG { // Function to return the bitwise AND // of all the odd integers from // the range [1, n] static int bitwiseAndOdd(int n) { // Initialize result to 1 int result = 1; // Starting from 3, bitwise AND // all the odd integers less // than or equal to n for (int i = 3; i <= n; i = i + 2) { result = (result & i); } return result; } // Driver code public static void Main() { int n = 10; Console.WriteLine(bitwiseAndOdd(n)); } } // This code is contributed by AnkitRai01
Javascript
<script> // Javascript implementation of the approach // Function to return the bitwise AND // of all the odd integers from // the range [1, n] function bitwiseAndOdd(n) { // Initialize result to 1 var result = 1; // Starting from 3, bitwise AND // all the odd integers less // than or equal to n for(var i = 3; i <= n; i = i + 2) { result = (result & i); } return result; } // Driver code var n = 10; document.write(bitwiseAndOdd(n)); // This code is contributed by noob2000 </script>
Producción:
1
Complejidad de tiempo: O(n)
Espacio Auxiliar: O(1)
Enfoque eficiente: AND bit a bit con 1 siempre dará 1 como resultado si el entero tiene un uno en el bit menos significativo (todos los enteros impares en este caso). Entonces, el resultado será 1 en todos los casos.
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ implementation of the approach #include <iostream> using namespace std; // Function to return the bitwise AND // of all the odd integers from // the range [1, n] int bitwiseAndOdd(int n) { return 1; } // Driver code int main() { int n = 10; cout << bitwiseAndOdd(n); return 0; }
Java
// Java implementation of the approach class GFG { // Function to return the bitwise AND // of all the odd integers from // the range [1, n] static int bitwiseAndOdd(int n) { return 1; } // Driver code public static void main (String[] args) { int n = 10; System.out.println(bitwiseAndOdd(n)); } } // This code is contributed by AnkitRai01
Python 3
# Python 3 implementation of the approach # Function to return the bitwise AND # of all the odd integers from # the range [1, n] def bitwiseAndOdd(n): return 1 # Driver code n = 10 print(bitwiseAndOdd(n)) # This code is contributed by ApurvaRaj
C#
// C# implementation of the approach using System; class GFG { // Function to return the bitwise AND // of all the odd integers from // the range [1, n] static int bitwiseAndOdd(int n) { return 1; } // Driver code public static void Main() { int n = 10; Console.WriteLine(bitwiseAndOdd(n)); } } // This code is contributed by AnkitRai01
Javascript
<script> // Javascript implementation of the approach // Function to return the bitwise AND // of all the odd integers from // the range [1, n] function bitwiseAndOdd(n) { return 1; } // Driver code var n = 10; document.write( bitwiseAndOdd(n)); //This code is contributed by SoumikMondal </script>
Producción:
1
Complejidad de tiempo: O(1)
Espacio Auxiliar: O(1)