Find subsequences with maximum Bitwise AND and Bitwise OR Last Updated : 19 Jul, 2022 Comments Improve Suggest changes Like Article Like Report 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 = 4 a = [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 ?> JavaScript <script> // Javascript implementation of the above approach // Function to find the maximum sum function maxSum(a, n) { // Maximum AND is maximum element var maxAnd = Math.max(...a); // Maximum OR is bitwise OR of all. var maxOR = 0; for(var i = 0; i < n; i++) { maxOR |= a[i]; } document.write((maxAnd + maxOR)); } // Driver code var n = 4; var a = [ 3, 5, 6, 1]; maxSum(a, n); // This code is contributed by Ankita saini </script> Output: 13 Time Complexity: O(n) Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article Find subsequences with maximum Bitwise AND and Bitwise OR I indrajit1 Follow Improve Article Tags : Bit Magic Mathematical Python DSA Technical Scripter 2018 Bitwise-OR Bitwise-AND +3 More Practice Tags : Bit MagicMathematicalpython Similar Reads Find maximum product of Bitwise AND and Bitwise OR of K-size subarray Given an array arr[] containing N integers and an integer K, the task is to find the maximum value of the product of Bitwise AND and Bitwise OR of all elements of a K-sized subarray. Example: Input: arr[] = {1, 2, 3, 4}, K = 2Output: 6Explanation: Bitwise AND and Bitwise XOR of all K-sized subarrays 9 min read Count pairs with equal Bitwise AND and Bitwise OR value Given an array, arr[] of size N, the task is to count the number of unordered pairs such that Bitwise AND and Bitwise OR of each pair is equal. Examples: Input: arr[] = {1, 2, 1} Output: 1 Explanation: Bitwise AND value and Bitwise OR value all possible pairs are: Bitwise AND of the pair(arr[0], arr 6 min read Count subsequences with same values of Bitwise AND, OR and XOR We are given an array arr of n element. We need to count number of non-empty subsequences such that these individual subsequences have same values of bitwise AND, OR and XOR. For example, we need to count a subsequence (x, y, z) if (x | y | z) is equal to (x & y & z) and (x ^ y ^ z). For a s 6 min read Maximum Bitwise AND value of subsequence of length K Given an array a of size N and an integer K. The task is to find the maximum bitwise and value of elements of any subsequence of length K. Note: a[i] <= 109 Examples: Input: a[] = {10, 20, 15, 4, 14}, K = 4 Output: 4 {20, 15, 4, 14} is the subsequence with highest '&' value. Input: a[] = {255 15+ min read Maximum subset with bitwise OR equal to k Given an array of non-negative integers and an integer k, find the subset of maximum length with bitwise OR equal to k. Examples: Input : arr[] = [1, 4, 2] k = 3 Output : [1, 2] Explanation: The bitwise OR of 1 and 2 equals 3. It is not possible to obtain a subset of length greater than 2. Input : a 8 min read Non-negative pairs with sum of Bitwise OR and Bitwise AND equal to N Given an integer N, the task is to find all non-negative pairs (A, B) such that the sum of Bitwise OR and Bitwise AND of A, B is equal to N, i.e., (A | B) + (A & B) = N. Examples: Input: N = 5Output: (0, 5), (1, 4), (2, 3), (3, 2), (4, 1), (5, 0)Explanation: All possible pairs satisfying the nec 5 min read Minimum steps to determine the subsequence with max 1s based on given conditions Given a string S of size N consisting of '0', '1' and '?', where N is always even. Divide the string into two different strings say S1 and S2, where S1 will only contain the characters at even indices of S and S2 will only contain the characters at odd indices of S. The task is to find the minimum p 12 min read Count of subsequences having odd Bitwise AND values in the given array Given an array arr[] of N integers, the task is to find the number of subsequences of the given array such that their Bitwise AND value is Odd. Examples: Input: arr[] = {2, 3, 1}Output: 3Explanation: The subsequences of the given array having odd Bitwise AND values are {3} = 3, {1} = 1, {3, 1} = 3 5 min read Queries to find Maximum index R with bitwise AND from index L at least K Given array A[] of size N and array Q[][2] of size M representing queries of the type {L, K}. The task for this problem is to answer queries for each query to find the largest index R such that bitwise AND of all elements from L to R is at least K. If there is no such index print -1 Examples: Input: 15+ min read Find size of largest subset with bitwise AND greater than their bitwise XOR Given an array arr[] of N integers, the task is to find the size of the largest subset such that the bitwise AND of all elements of the subset is greater than the bitwise XOR of all elements of the subset. Example: Input: arr[] = {1, 2, 3, 4, 5}Output: 2Explanation: The subset {2, 3} has the bitwise 6 min read Like