

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Maximum OR sum of sub-arrays of two different arrays in C++
<h2>Problem statement</h2><p>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.</p><h2>Example</h2><p>If arr1[] = {1, 2, 4, 3, 2} and</p><p>Arr2[] = {1, 3, 3, 12, 2} then maximum result is obtained when we create following two subarrays −</p><p>Subarr1[] = {2, 4, 3} and</p><p>Subarr2[] = {3, 3, 12}</p><h2>Algorithm</h2><p>We can use below formula to gets result −</p><pre class="result notranslate">f(a, 1, n) + f(b, 1, n)</pre><h2>Example</h2><p><a class="demo" href="http://tpcg.io/SO0FeeKk" rel="nofollow" target="_blank"> Live Demo</a></p><pre class="prettyprint notranslate">#include <bits/stdc++.h> using namespace std; int getMaximumSum(int *arr1, int *arr2, int n) { int sum1 = 0; int sum2 = 0; for (int i = 0; i < n; ++i) { sum1 = sum1 | arr1[i]; sum2 = sum2 | arr2[i]; } return sum1 + sum2; } int main() { int arr1[] = {1, 2, 4, 3, 2}; int arr2[] = {1, 3, 3, 12, 2}; int n = sizeof(arr1) / sizeof(arr1[0]); cout << "Maximum result = " << getMaximumSum(arr1, arr2, n) << endl; return 0; }</pre><h2>Output</h2><p>When you compile and execute above program. It generates following output −</p><pre class="result notranslate">Maximum result = 22</pre>
- Related Questions & Answers
- Maximum Sum of Products of Two Arrays in C++
- Find Sum of pair from two arrays with maximum sum in C++
- Maximum Sum Path in Two Arrays in C++
- Plotting two different arrays of different lengths in matplotlib
- Reverse sum of two arrays in JavaScript
- Find sub-arrays from given two arrays such that they have equal sum in Python
- Combine two different arrays in JavaScript
- Maximize the maximum among minimum of K consecutive sub-arrays in C++
- Find the overlapping sum of two arrays using C++
- Program to find number of sub-arrays with odd sum using Python
- Intersection of two arrays JavaScript
- Equality of two arrays JavaScript
- 8086 program to determine sum of corresponding elements of two arrays
- Maximum sum of increasing order elements from n arrays in C++
- Count all sub-arrays having sum divisible by k
Advertisements