Dadas dos arrays de enteros positivos. Seleccione dos sub-arrays de igual tamaño de cada array y calcule la suma OR máxima posible de las dos sub-arrays.
Nota: Sea f(x, l, r) la suma OR de todos los elementos en el rango [l, r] en el arreglo x.
Ejemplos:
Input : A[] = {1, 2, 4, 3, 2} B[] = {2, 3, 3, 12, 1} Output : 22 Explanation: Here, one way to get maximum sum is to select sub-array [l = 2, r = 4] f(A, 2, 4) = 2|4|3 = 7 f(B, 2, 4) = 3|3|12 = 15 So, f(A, 2, 4) + f(B, 2, 4) = 7 + 15 = 22. This sum can be achieved in many other ways. Input : A[] = {1, 2, 2} B[] = {2, 1, 3} Output : 6
Observe el funcionamiento del operador OR bit a bit. Si tomamos dos enteros X e Y, entonces (X|Y >= X). Se puede probar tomando algunos ejemplos. Vamos a derivar una fórmula usando la ecuación anterior.
y también
de las dos ecuaciones anteriores,
por lo tanto, obtenemos la suma máxima cuando tomamos el OR de toda la array ->
A continuación se muestra la implementación del enfoque anterior:
C++
// CPP program to find maximum OR sum #include <bits/stdc++.h> using namespace std; // function to find maximum OR sum void MaximumSum(int a[], int b[], int n) { int sum1 = 0, sum2 = 0; // OR sum of all the elements // in both arrays for (int i = 0; i < n; i++) { sum1 |= a[i]; sum2 |= b[i]; } cout << sum1 + sum2 << endl; } // Driver Code int main() { int A[] = { 1, 2, 4, 3, 2 }; int B[] = { 2, 3, 3, 12, 1 }; int n = sizeof(A) / sizeof(A[0]); MaximumSum(A, B, n); return 0; }
Java
// Java program to find maximum OR sum class GFG { // function to find maximum OR sum static void MaximumSum(int a[], int b[], int n) { int sum1 = 0, sum2 = 0; // OR sum of all the elements // in both arrays for (int i = 0; i < n; i++) { sum1 |= a[i]; sum2 |= b[i]; } System.out.println(sum1 + sum2); } // Driver code public static void main(String arg[]) { int A[] = {1, 2, 4, 3, 2}; int B[] = {2, 3, 3, 12, 1}; int n = A.length; MaximumSum(A, B, n); } } // This code is contributed by Anant Agarwal.
Python3
# Python 3 program to # find maximum OR sum # function to find # maximum OR sum def MaximumSum(a, b, n): sum1 = 0 sum2 = 0 # OR sum of all the # elements in both arrays for i in range(0, n): sum1 |= a[i] sum2 |= b[i] print(sum1 + sum2) # Driver Code A = [ 1, 2, 4, 3, 2 ] B = [ 2, 3, 3, 12, 1 ] n = len(A) MaximumSum(A, B, n) # This code is contributed by Smitha Dinesh Semwal
C#
// C# program to find maximum OR sum using System; class GFG { // function to find maximum OR sum static void MaximumSum(int []a, int []b, int n) { int sum1 = 0, sum2 = 0; // OR sum of all the elements // in both arrays for (int i = 0; i < n; i++) { sum1 |= a[i]; sum2 |= b[i]; } Console.WriteLine(sum1 + sum2); } // Driver code public static void Main() { int []A = {1, 2, 4, 3, 2}; int []B = {2, 3, 3, 12, 1}; int n = A.Length; MaximumSum(A, B, n); } } // This code is contributed by Vt_m.
PHP
<?php // PHP program to find maximum OR sum // function to find maximum OR sum function MaximumSum($a, $b, $n) { $sum1 = 0; $sum2 = 0; // OR sum of all the elements // in both arrays for ($i = 0; $i < $n; $i++) { $sum1 |= $a[$i]; $sum2 |= $b[$i]; } echo ($sum1 + $sum2)."\n"; } // Driver Code $A = array(1, 2, 4, 3, 2 ); $B = array(2, 3, 3, 12, 1 ); $n = sizeof($A) / sizeof($A[0]); MaximumSum($A, $B, $n); // This code is contributed by mits ?>
Javascript
<script> // JavaScript program to find maximum OR sum // function to find maximum OR sum function MaximumSum(a, b, n) { let sum1 = 0, sum2 = 0; // OR sum of all the elements // in both arrays for (let i = 0; i < n; i++) { sum1 |= a[i]; sum2 |= b[i]; } document.write(sum1 + sum2); } // Driver code let A = [1, 2, 4, 3, 2]; let B = [2, 3, 3, 12, 1]; let n = A.length; MaximumSum(A, B, n); </script>
22
Complejidad temporal: O(n)
Espacio auxiliar: O(1)
Publicación traducida automáticamente
Artículo escrito por Harsha_Mogali y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA