Maximum OR sum of sub-arrays of two different arrays
Last Updated :
28 Jul, 2022
Given two arrays of positive integers. Select two sub-arrays of equal size from each array and calculate maximum possible OR sum of the two sub-arrays.
Note: Let f(x, l, r) is the OR sum of all the elements in the range [l, r] in array x.
Examples :
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 the operation of Bitwise OR operator. If we take two integers X and Y, then (X|Y >= X). It can be proved by taking some examples. Lets derive a formula using the above equation.
f(a, 1, i-1) | f(a, i, j) | f(a, j+1, n) >= f(a, i, j)
and also f(a, 1, i-1) | f(a, i, j) | f(a, j+1, n) = f(a, 1, n)
from the above two equations, f(a, 1, n) >= f(a, i, j).
So, we get maximum sum when we take the OR of the whole array -> f(a, 1, n) + f(b, 1, n)
Below is the implementation of above approach:
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>
Time Complexity: O(n)
Auxiliary Space: O(1)
Similar Reads
Maximum Sum of Products of Two Arrays Given two arrays A and B of positive integers of the same size N. The task is to find the maximum sum of products of their elements. Each element in A has to be multiplied with exactly one element in B or vice versa such that each element of both the arrays appears exactly once and the sum of the pr
5 min read
Maximum absolute difference between sum of two contiguous sub-arrays Given an array of integers, find two non-overlapping contiguous sub-arrays such that the absolute difference between the sum of two sub-arrays is maximum. Example: Input: [-2, -3, 4, -1, -2, 1, 5, -3] Output: 12 Two subarrays are [-2, -3] and [4, -1, -2, 1, 5] Input: [2, -1, -2, 1, -4, 2, 8] Output:
15+ min read
Maximum product of sum of two contiguous subarrays of an array Given an array arr[] of N positive integers, the task is to split the array into two contiguous subarrays such that the product of the sum of two contiguous subarrays is maximum. Examples: Input: arr[] = {4, 10, 1, 7, 2, 9} Output: 270 All possible partitions and their product of sum are: {4} and {1
10 min read
Maximum difference of prefix sum for all indices of given two Arrays Given 2 arrays of integers a[] and s[] both of size N. The task is to find the maximum difference of prefix sum for all indices of the given arrays. Examples: Input: N = 5, a[] = {20, 20, 35, 20, 35}, s[] = {21, 31, 34, 41, 14}Output: 32Explanation: After prefix sum the arrays are a[] = {20, 40, 75,
4 min read
Find Sum of pair from two arrays with maximum sum Given two arrays of positive and distinct integers. The task is to find a pair from the two arrays with maximum sum. Note: The pair should contain one element from both the arrays. Examples: Input : arr1[] = {1, 2, 3}, arr2[] = {4, 5, 6} Output : Max Sum = 9 Pair (3, 6) has the maximum sum. Input :
6 min read
Maximum possible pair sum at most K from two Arrays Given two arrays arr1[] and arr2[] of sizes N and M and an integer K, the task is to find the maximum possible sum pair from two arrays such that the sum is at most K. Note: You have to take one element from each array. Examples: Input: arr1[] = {5, 4, 1, 2, 3}, arr2[] = {30, 20, 40, 10}, K = 30Outp
12 min read
Maximise array sum after taking non-overlapping sub-arrays of length K Given an integer array arr[] of length N and an integer K, the task is to select some non-overlapping sub-arrays such that each sub-array is exactly of length K, no two sub-arrays are adjacent and sum of all the elements of the selected sub-arrays is maximum.Examples: Input: arr[] = {1, 2, 3, 4, 5},
8 min read
Maximum array from two given arrays keeping order same Given two same-sized arrays a[] and b[] (each containing distinct elements individually, but they may have some common elements between them). The task is to create a third array res[] of the same size n that includes maximum n elements combined from both arrays.Start by including elements from a[]
8 min read
Sum of middle elements of two sorted Arrays Given two sorted arrays, arr1[] and arr2[], each of size N, the task is to merge the arrays and find the sum of the two middle elements of the merged array.Example:Input: N = 5, arr1[] = {1,2,4,6,10}, arr2[] = {4,5,6,9,12}Output: 11Explanation: The merged array is {1,2,4,4,5,6,6,9,10,12}. The sum of
15 min read
Maximum Sum of two non-overlapping Subarrays of any length Given an array A consisting of N integers, the task is to find the maximum sum of two non-overlapping subarrays of any length of the array. Note: You can select empty subarrays also. Examples: Input: N = 3, A[] = {-4, -5, -2}Output: 0Explanation: Two empty subarrays are optimal with maximum sum = 0.
6 min read