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, 127, 31, 5, 24, 37, 15}, K = 5
Output: 8
Naive Approach: A naive approach is to recursively find the bitwise and value of all subsequences of length K and the maximum among all of them will be the answer.
Efficient Approach: An efficient approach is to solve it using bit properties. Below are the steps to solve the problem:
- Iterate from the left(initially left = 31 as 232 > 109 ) till we find > K numbers in the vector temp (initially temp = arr) whose i-th bit is set. Update the new set of numbers to temp array
- If we do not get > K numbers, the & value of any K elements in the temp array will be the maximum & value possible.
- Repeat Step-1 with left re-initialized as first-bit + 1.
till K elements are possible
Below is the implementation of the above approach:
C++
// C++ program to find the sum of // the addition of all possible subsets. #include <bits/stdc++.h> using namespace std; // Function to perform step-1 vector<int> findSubset(vector<int>& temp, int& last, int k) { vector<int> ans; // Iterate from left till 0 // till we get a bit set of K numbers for (int i = last; i >= 0; i--) { int cnt = 0; // Count the numbers whose // i-th bit is set for (auto it : temp) { int bit = it & (1 << i); if (bit > 0) cnt++; } // If the array has numbers>=k // whose i-th bit is set if (cnt >= k) { for (auto it : temp) { int bit = it & (1 << i); if (bit > 0) ans.push_back(it); } // Update last last = i - 1; // Return the new set of numbers return ans; } } return ans; } // Function to find the maximum '&' value // of K elements in subsequence int findMaxiumAnd(int a[], int n, int k) { int last = 31; // Temporary arrays vector<int> temp1, temp2; // Initially temp = arr for (int i = 0; i < n; i++) { temp2.push_back(a[i]); } // Iterate till we have >=K elements while ((int)temp2.size() >= k) { // Temp array temp1 = temp2; // Find new temp array if // K elements are there temp2 = findSubset(temp2, last, k); } // Find the & value int ans = temp1[0]; for (int i = 0; i < k; i++) ans = ans & temp1[i]; return ans; } // Driver Code int main() { int a[] = { 255, 127, 31, 5, 24, 37, 15 }; int n = sizeof(a) / sizeof(a[0]); int k = 4; cout << findMaxiumAnd(a, n, k); } |
Java
// Java program to find the sum of // the addition of all possible subsets. import java.util.*; class GFG { static int last; // Function to perform step-1 static Vector<Integer> findSubset(Vector<Integer> temp, int k) { Vector<Integer> ans = new Vector<Integer>(); // Iterate from left till 0 // till we get a bit set of K numbers for (int i = last; i >= 0; i--) { int cnt = 0; // Count the numbers whose // i-th bit is set for (Integer it : temp) { int bit = it & (1 << i); if (bit > 0) cnt++; } // If the array has numbers>=k // whose i-th bit is set if (cnt >= k) { for (Integer it : temp) { int bit = it & (1 << i); if (bit > 0) ans.add(it); } // Update last last = i - 1; // Return the new set of numbers return ans; } } return ans; } // Function to find the maximum '&' value // of K elements in subsequence static int findMaxiumAnd(int a[], int n, int k) { last = 31; // Temporary arrays Vector<Integer> temp1 = new Vector<Integer>(); Vector<Integer> temp2 = new Vector<Integer>();; // Initially temp = arr for (int i = 0; i < n; i++) { temp2.add(a[i]); } // Iterate till we have >=K elements while ((int)temp2.size() >= k) { // Temp array temp1 = temp2; // Find new temp array if // K elements are there temp2 = findSubset(temp2, k); } // Find the & value int ans = temp1.get(0); for (int i = 0; i < k; i++) ans = ans & temp1.get(i); return ans; } // Driver Code public static void main(String[] args) { int a[] = { 255, 127, 31, 5, 24, 37, 15 }; int n = a.length; int k = 4; System.out.println(findMaxiumAnd(a, n, k)); } } // This code is contributed by 29AjayKumar |
Python3
# Python3 program to find the sum of # the addition of all possible subsets. last = 31 # Function to perform step-1 def findSubset(temp, k): global last ans = [] # Iterate from left till 0 # till we get a bit set of K numbers for i in range(last, -1, -1): cnt = 0 # Count the numbers whose # i-th bit is set for it in temp: bit = it & (1 << i) if (bit > 0): cnt += 1 # If the array has numbers>=k # whose i-th bit is set if (cnt >= k): for it in temp: bit = it & (1 << i) if (bit > 0): ans.append(it) # Update last last = i - 1 # Return the new set of numbers return ans return ans # Function to find the maximum '&' value # of K elements in subsequence def findMaxiumAnd(a, n, k): global last # Temporary arrays temp1, temp2, = [], [] # Initially temp = arr for i in range(n): temp2.append(a[i]) # Iterate till we have >=K elements while len(temp2) >= k: # Temp array temp1 = temp2 # Find new temp array if # K elements are there temp2 = findSubset(temp2, k) # Find the & value ans = temp1[0] for i in range(k): ans = ans & temp1[i] return ans # Driver Code a = [255, 127, 31, 5, 24, 37, 15] n = len(a) k = 4 print(findMaxiumAnd(a, n, k)) # This code is contributed by Mohit Kumar |
C#
// C# program to find the sum of // the addition of all possible subsets. using System; using System.Collections.Generic; class GFG { static int last; // Function to perform step-1 static List<int>findSubset(List<int> temp, int k) { List<int> ans = new List<int>(); // Iterate from left till 0 // till we get a bit set of K numbers for (int i = last; i >= 0; i--) { int cnt = 0; // Count the numbers whose // i-th bit is set foreach (int it in temp) { int bit = it & (1 << i); if (bit > 0) cnt++; } // If the array has numbers>=k // whose i-th bit is set if (cnt >= k) { foreach (int it in temp) { int bit = it & (1 << i); if (bit > 0) ans.Add(it); } // Update last last = i - 1; // Return the new set of numbers return ans; } } return ans; } // Function to find the maximum '&' value // of K elements in subsequence static int findMaxiumAnd(int []a, int n, int k) { last = 31; // Temporary arrays List<int> temp1 = new List<int>(); List<int> temp2 = new List<int>();; // Initially temp = arr for (int i = 0; i < n; i++) { temp2.Add(a[i]); } // Iterate till we have >=K elements while ((int)temp2.Count >= k) { // Temp array temp1 = temp2; // Find new temp array if // K elements are there temp2 = findSubset(temp2, k); } // Find the & value int ans = temp1[0]; for (int i = 0; i < k; i++) ans = ans & temp1[i]; return ans; } // Driver Code public static void Main(String[] args) { int []a = { 255, 127, 31, 5, 24, 37, 15 }; int n = a.Length; int k = 4; Console.WriteLine(findMaxiumAnd(a, n, k)); } } // This code is contributed by Rajput-Ji |
24
Recommended Posts:
- Maximum length subsequence such that adjacent elements in the subsequence have a common factor
- Maximum Sum Subsequence of length k
- Maximum length subsequence possible of the form R^N K^N
- Find the Increasing subsequence of length three with maximum product
- Maximum length prefix of one string that occurs as subsequence in another
- Find subsequences with maximum Bitwise AND and Bitwise OR
- Subsequence X of length K such that gcd(X[0], X[1]) + (X[2], X[3]) + ... is maximized
- Repeated subsequence of length 2 or more
- Length of Longest Balanced Subsequence
- Count subsequence of length three in a given string
- Length of longest common subsequence containing vowels
- Find length of longest Fibonacci like subsequence
- Length of longest strict bitonic subsequence
- Number of balanced bracket subsequence of length 2 and 4
- Length of the longest subsequence such that xor of adjacent elements is non-decreasing
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.

