Dado un rango [L, R] , la tarea es encontrar un par (X, Y) tal que L ≤ X < Y ≤ R y X & Y sea el máximo entre todos los pares posibles, luego imprima el AND bit a bit del encontrado par.
Ejemplos:
Entrada: L = 1, R = 9
Salida: 8
En todos los pares posibles, el par (8, 9) da el valor máximo para AND bit a bit.Entrada: L = 523641, R = 985624
Salida: 985622
Enfoque ingenuo: iterar de L a R y verificar el AND bit a bit para cada par posible e imprimir el valor máximo al final.
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; // Function to return the maximum bitwise AND // possible among all the possible pairs int maxAND(int L, int R) { int maximum = L & R; for (int i = L; i < R; i++) for (int j = i + 1; j <= R; j++) // Maximum among all (i, j) pairs maximum = max(maximum, (i & j)); return maximum; } // Driver code int main() { int L = 1, R = 632; cout << maxAND(L, R); return 0; }
Java
// Java implementation of the approach class GFG { // Function to return the maximum bitwise AND // possible among all the possible pairs static int maxAND(int L, int R) { int maximum = L & R; for (int i = L; i < R; i++) for (int j = i + 1; j <= R; j++) // Maximum among all (i, j) pairs maximum = Math.max(maximum, (i & j)); return maximum; } // Driver code public static void main(String[] args) { int L = 1, R = 632; System.out.println(maxAND(L, R)); } } // This code contributed by Rajput-Ji
Python3
# Python 3 implementation of the approach # Function to return the maximum bitwise AND # possible among all the possible pairs def maxAND(L, R): maximum = L & R for i in range(L, R, 1): for j in range(i + 1, R + 1, 1): # Maximum among all (i, j) pairs maximum = max(maximum, (i & j)) return maximum # Driver code if __name__ == '__main__': L = 1 R = 632 print(maxAND(L, R)) # This code is contributed by # Surendra_Gangwar
C#
// C# implementation of the approach using System; class GFG { // Function to return the maximum bitwise AND // possible among all the possible pairs static int maxAND(int L, int R) { int maximum = L & R; for (int i = L; i < R; i++) for (int j = i + 1; j <= R; j++) // Maximum among all (i, j) pairs maximum = Math.Max(maximum, (i & j)); return maximum; } // Driver code public static void Main(String[] args) { int L = 1, R = 632; Console.WriteLine(maxAND(L, R)); } } // This code has been contributed by 29AjayKumar
PHP
<?php // PHP implementation of the approach // Function to return the maximum bitwise AND // possible among all the possible pairs function maxAND($L, $R) { $maximum = $L & $R; for ($i = $L; $i < $R; $i++) for ($j = $i + 1; $j <= $R; $j++) // Maximum among all (i, j) pairs $maximum = max($maximum, ($i & $j)); return $maximum; } // Driver code $L = 1; $R = 632; echo(maxAND($L, $R)); // This code contributed by Code_Mech. ?>
Javascript
<script> // JavaScript implementation of the approach // Function to return the maximum bitwise AND // possible among all the possible pairs function maxAND(L, R) { var maximum = L & R; for (var i = L; i < R; i++) for (var j = i + 1; j <= R; j++) // Maximum among all (i, j) pairs maximum = Math.max(maximum, (i & j)); return maximum; } // Driver code var L = 1, R = 632; document.write( maxAND(L, R)); </script>
630
Complejidad temporal: O(R*R)
Espacio auxiliar: O(1)
Enfoque eficiente: si observamos cuidadosamente en el rango [L, R], el AND máximo es posible entre el AND de (R) y (R – 1) o el AND de (R – 1) y (R – 2) .
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; // Function to return the maximum bitwise AND // possible among all the possible pairs int maxAND(int L, int R) { // If there is only a single value // in the range [L, R] if (L == R) return L; // If there are only two values // in the range [L, R] else if ((R - L) == 1) return (R & L); else { if (((R - 1) & R) > ((R - 2) & (R - 1))) return ((R - 1) & R); else return ((R - 2) & (R - 1)); } } // Driver code int main() { int L = 1, R = 632; cout << maxAND(L, R); return 0; }
Java
// Java implementation of the approach class GfG { // Function to return the maximum bitwise AND // possible among all the possible pairs static int maxAND(int L, int R) { // If there is only a single value // in the range [L, R] if (L == R) return L; // If there are only two values // in the range [L, R] else if ((R - L) == 1) return (R & L); else { if (((R - 1) & R) > ((R - 2) & (R - 1))) return ((R - 1) & R); else return ((R - 2) & (R - 1)); } } // Driver code public static void main(String[] args) { int L = 1, R = 632; System.out.println(maxAND(L, R)); } } // This code is contributed by // Prerna Saini
Python3
# Python3 implementation of the approach # Function to return the maximum bitwise AND # possible among all the possible pairs def maxAND(L, R): # If there is only a single value # in the range [L, R] if (L == R): return L; # If there are only two values # in the range [L, R] elif ((R - L) == 1): return (R & L); else: if (((R - 1) & R) > ((R - 2) & (R - 1))): return ((R - 1) & R); else: return ((R - 2) & (R - 1)); # Driver code L = 1; R = 632; print(maxAND(L, R)); # This code contributed by PrinciRaj1992
C#
// C# implementation of the approach using System; class GfG { // Function to return the maximum bitwise AND // possible among all the possible pairs static int maxAND(int L, int R) { // If there is only a single value // in the range [L, R] if (L == R) return L; // If there are only two values // in the range [L, R] else if ((R - L) == 1) return (R & L); else { if (((R - 1) & R) > ((R - 2) & (R - 1))) return ((R - 1) & R); else return ((R - 2) & (R - 1)); } } // Driver code public static void Main() { int L = 1, R = 632; Console.WriteLine(maxAND(L, R)); } } // This code is contributed by Ryuga
PHP
<?php // PHP implementation of the approach // Function to return the maximum bitwise AND // possible among all the possible pairs function maxAND($L, $R) { // If there is only a single value // in the range [L, R] if ($L == $R) return $L; // If there are only two values // in the range [L, R] else if (($R - $L) == 1) return ($R & $L); else { if ((($R - 1) & $R) > (($R - 2) & ($R - 1))) return (($R - 1) & $R); else return (($R - 2) & ($R - 1)); } } // Driver code $L = 1; $R = 632; echo maxAND($L, $R); // This code is contributed by mits ?>
Javascript
<script> // JavaScript implementation of the approach // Function to return the maximum bitwise AND // possible among all the possible pairs function maxAND(L, R) { // If there is only a single value // in the range [L, R] if (L == R) return L; // If there are only two values // in the range [L, R] else if ((R - L) == 1) return (R & L); else { if (((R - 1) & R) > ((R - 2) & (R - 1))) return ((R - 1) & R); else return ((R - 2) & (R - 1)); } } let L = 1, R = 632; document.write(maxAND(L, R)); </script>
630
Tiempo Complejidad: O(1)
Espacio Auxiliar: O(1)
Publicación traducida automáticamente
Artículo escrito por Chandan_Agrawal y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA