Given an array of n elements. The task is to print the maximum sum by selecting two subsequences of the array (not necessarily different) such that the sum of bitwise AND of all elements of the first subsequence and bitwise OR of all the elements of the second subsequence is maximum.
Examples:
Input: arr[] = {3, 5, 6, 1}
Output: 13
We get maximum AND value by choosing 6 only and maximum OR value by choosing all (3 | 5 | 6 | 1) = 7. So the result is 6 + 7 = 13.
Input: arr[] = {3, 3}
Output: 6
Approach: The maximum OR would be the or of all the numbers and the maximum AND would be the maximum element in the array. This is so because if (x | y) >= x, y and (x & y) <=x, y.
C++
//C++ implementation of above approach #include<bits/stdc++.h> using namespace std; //function to find the maximum sum void maxSum(int a[],int n) { int maxAnd=0; //Maximum And is maximum element for(int i=0;i<n;i++) maxAnd=max(maxAnd,a[i]); //Maximum OR is bitwise OR of all int maxOR=0; for(int i=0;i<n;i++) { maxOR=maxOR|a[i]; } cout<<maxAnd+maxOR; } //Driver code int main() { int a[]={3,5,6,1}; int n=sizeof(a)/sizeof(a[0]); maxSum(a,n); } //This code is contributed by Mohit kumar 29 |
Java
import java.util.Arrays; // Java implementation of the above approach // Function to find the maximum sum class GFG { static void maxSum(int[] a, int n) { // Maximum AND is maximum element int maxAnd = Arrays.stream(a).max().getAsInt(); // Maximum OR is bitwise OR of all. int maxOR = 0; for (int i = 0; i < n; i++) { maxOR |= a[i]; } System.out.println((maxAnd + maxOR)); // Driver code } public static void main(String[] args) { int n = 4; int[] a = {3, 5, 6, 1}; maxSum(a, n); } } //This code contributed by 29AjayKumar |
Python3
# Python implementation of the above approach # Function to find the maximum sum def maxSum(a, n): # Maximum AND is maximum element maxAnd = max(a) # Maximum OR is bitwise OR of all. maxOR = 0 for i in range(n): maxOR|= a[i] print(maxAnd + maxOR) # Driver code n = 4a = [3, 5, 6, 1] maxSum(a, n) |
C#
// C# implementation of the above approach using System; using System.Linq; public class GFG { // Function to find the maximum sum static void maxSum(int []a, int n) { // Maximum AND is maximum element int maxAnd = a.Max(); // Maximum OR is bitwise OR of all. int maxOR = 0; for (int i = 0; i < n; i++) { maxOR |= a[i]; } Console.Write((maxAnd + maxOR)); // Driver code } public static void Main() { int n = 4; int[] a = {3, 5, 6, 1}; maxSum(a, n); } } //This code contributed by 29AjayKumar |
PHP
<?php // PHP implementation of the // above approach // Function to find the maximum sum function maxSum($a, $n) { // Maximum AND is maximum element $maxAnd = max($a); // Maximum OR is bitwise OR of all. $maxOR = 0; for($i = 0; $i < $n; $i++) $maxOR|= $a[$i]; print($maxAnd + $maxOR); } // Driver code $n = 4; $a = array(3, 5, 6, 1); maxSum($a, $n); // This code is contributed by mits ?> |
13
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Total pairs in an array such that the bitwise AND, bitwise OR and bitwise XOR of LSB is 1
- Count subsequences with same values of Bitwise AND, OR and XOR
- Bitwise OR of sum of all subsequences of an array
- Product of all Subsequences of size K except the minimum and maximum Elements
- Count subsequences which contains both the maximum and minimum array element
- Maximum of minimum difference of all pairs from subsequences of given size
- Maximum K-digit number possible from subsequences of two given arrays
- Count subsequences for every array element in which they are the maximum
- Count ways to generate pairs having Bitwise XOR and Bitwise AND equal to X and Y respectively
- Maximize sum of squares of array elements possible by replacing pairs with their Bitwise AND and Bitwise OR
- Count pairs with equal Bitwise AND and Bitwise OR value
- Non-negative pairs with sum of Bitwise OR and Bitwise AND equal to N
- Leftover element after performing alternate Bitwise OR and Bitwise XOR operations on adjacent pairs
- Minimum possible Bitwise OR of all Bitwise AND of pairs generated from two given arrays
- Count pairs with bitwise XOR exceeding bitwise AND from a given array
- Find the count of subsequences where each element is divisible by K
- Find product of all elements at indexes which are factors of M for all possible sorted subsequences of length M
- Sum of width (max and min diff) of all Subsequences
- Number of Subsequences with Even and Odd Sum
- Number of Subsequences with Even and Odd Sum | Set 2
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

