Maximum subset with bitwise OR equal to k
Last Updated :
06 Apr, 2023
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 : arr[] = [1, 2, 5]
k = 4
Output : []
No subset's bitwise OR equals 4.
Method 1(Simple):
The naive method would be to consider all the subsets. While considering a subset, compute its bitwise OR. If it equals k, compare the subset's length with the maximum length so far and update the maximum length if required.
Method 2(Efficient):
0 OR 0 = 0
1 OR 0 = 1
1 OR 1 = 1
Hence, for all the positions in the binary representation of k with the bit equal to 0, the corresponding position in the binary representations of all the elements in the resulting subset should necessarily be 0.
On the other hand, for positions in k with the bit equal to 1, there has to be at least one element with a 1 in the corresponding position. The rest of the elements can have either 0 or 1 in that position. It does not matter.
Therefore, to obtain the resulting subset, traverse the initial array. While deciding if the element should be in the resulting subset or not, check whether there is any position in the binary representation of k which is 0 and the corresponding position in that element is 1. If there exists such a position, then ignore that element, else include it in the resulting subset.
How to determine if there exists a position in the binary representation of k which is 0 and the corresponding position in an element is 1?
Simply take the bitwise OR of k and that element. If it does not equal to k, then there exists such a position and the element has to be ignored. If their bitwise OR equals k, then include the current element in the resulting subset.
The final step is to determine if there is at least one element with a 1 in a position with 1 in the corresponding position in k.
Simply compute the bitwise OR of the resulting subset. If it equals k, then this is the final answer. Else no subset exists which satisfies the condition.
Steps to solve this problem:
1. declare a vector v of integers.
2. iterate through i=0 till n:
*check if arr[i] bitwise or k is equal to k than push arr[i] in v.
3. declare a variable ans=0.
4. iterate through i=0 till size of v:
*update ans to ans bitwise or v[i].
5. check if ans is not equal to k than subset doesn't exist.
6. iterate through i=0 till size of v:
*print v[i].
C++
// CPP Program to find the maximum subset
// with bitwise OR equal to k
#include <bits/stdc++.h>
using namespace std;
// function to find the maximum subset with
// bitwise OR equal to k
void subsetBitwiseORk(int arr[], int n, int k)
{
vector<int> v;
for (int i = 0; i < n; i++) {
// If the bitwise OR of k and element
// is equal to k, then include that element
// in the subset
if ((arr[i] | k) == k)
v.push_back(arr[i]);
}
// Store the bitwise OR of elements in v
int ans = 0;
for (int i = 0; i < v.size(); i++)
ans |= v[i];
// If ans is not equal to k, subset doesn't exist
if (ans != k) {
cout << "Subset does not exist" << endl;
return;
}
for (int i = 0; i < v.size(); i++)
cout << v[i] << ' ';
}
// Driver Code
int main()
{
int k = 3;
int arr[] = { 1, 4, 2 };
int n = sizeof(arr) / sizeof(arr[0]);
subsetBitwiseORk(arr, n, k);
return 0;
}
Java
// Java Program to find the maximum subset
// with bitwise OR equal to k
import java.util.*;
class GFG {
// function to find the maximum subset
// with bitwise OR equal to k
static void subsetBitwiseORk(int arr[],
int n, int k)
{
ArrayList<Integer> v =
new ArrayList<Integer>();
for (int i = 0; i < n; i++) {
// If the bitwise OR of k and
// element is equal to k, then
// include that element in the
// subset
if ((arr[i] | k) == k){
v.add(arr[i]);
}
}
// Store the bitwise OR of elements
// in v
int ans = 0;
for (int i = 0; i < v.size(); i++)
ans = ans|v.get(i);
// If ans is not equal to k, subset
// doesn't exist
if (ans != k) {
System.out.println("Subset does"
+ " not exist" );
return;
}
for (int i = 0; i < v.size(); i++)
System.out.print(v.get(i) + " " );
}
// main function
public static void main(String[] args)
{
int k = 3;
int arr[] = { 1, 4, 2 };
int n = arr.length;
subsetBitwiseORk(arr, n, k);
}
}
// This code is contributed by Arnab Kundu.
Python3
# Python3 Program to find the
# maximum subset with bitwise
# OR equal to k
# function to find the maximum
# subset with bitwise OR equal to k
def subsetBitwiseORk(arr, n, k) :
v = []
for i in range(0, n) :
# If the bitwise OR of k
# and element is equal to k,
# then include that element
# in the subset
if ((arr[i] | k) == k) :
v.append(arr[i])
# Store the bitwise OR
# of elements in v
ans = 0
for i in range(0, len(v)) :
ans |= v[i]
# If ans is not equal to
# k, subset doesn't exist
if (ans != k) :
print ("Subset does not exist\n")
return
for i in range(0, len(v)) :
print ("{} ".format(v[i]), end="")
# Driver Code
k = 3
arr = [1, 4, 2]
n = len(arr)
subsetBitwiseORk(arr, n, k)
# This code is contributed by
# Manish Shaw(manishshaw1)
C#
// C# Program to find the maximum subset
// with bitwise OR equal to k
using System;
using System.Collections;
class GFG {
// function to find the maximum subset
// with bitwise OR equal to k
static void subsetBitwiseORk(int []arr,
int n, int k)
{
ArrayList v = new ArrayList();
for (int i = 0; i < n; i++) {
// If the bitwise OR of k and
// element is equal to k, then
// include that element in the
// subset
if ((arr[i] | k) == k){
v.Add(arr[i]);
}
}
// Store the bitwise OR of
// elements in v
int ans = 0;
for (int i = 0; i < v.Count; i++)
ans = ans|(int)v[i];
// If ans is not equal to k, subset
// doesn't exist
if (ans != k) {
Console.WriteLine("Subset does"
+ " not exist" );
return;
}
for (int i = 0; i < v.Count; i++)
Console.Write((int)v[i] + " " );
}
// main function
static public void Main(String []args)
{
int k = 3;
int []arr = { 1, 4, 2 };
int n = arr.Length;
subsetBitwiseORk(arr, n, k);
}
}
// This code is contributed by Arnab Kundu
PHP
<?php
// PHP Program to find the
// maximum subset with bitwise
// OR equal to k
// function to find the maximum
// subset with bitwise OR equal to k
function subsetBitwiseORk($arr, $n, $k)
{
$v = array();
for ($i = 0; $i < $n; $i++)
{
// If the bitwise OR of k
// and element is equal to k,
// then include that element
// in the subset
if (($arr[$i] | $k) == $k)
array_push($v, $arr[$i]);
}
// Store the bitwise OR
// of elements in v
$ans = 0;
for ($i = 0; $i < count($v); $i++)
$ans |= $v[$i];
// If ans is not equal to
// k, subset doesn't exist
if ($ans != $k)
{
echo ("Subset does not exist\n");
return;
}
for ($i = 0; $i < count($v); $i++)
echo ($v[$i]." ");
}
// Driver Code
$k = 3;
$arr = array(1, 4, 2);
$n = count($arr);
subsetBitwiseORk($arr, $n, $k);
// This code is contributed by
// Manish Shaw(manishshaw1)
?>
JavaScript
<script>
// Javascript Program to find the maximum subset
// with bitwise OR equal to k
// function to find the maximum subset with
// bitwise OR equal to k
function subsetBitwiseORk(arr, n, k)
{
var v = [];
for (var i = 0; i < n; i++) {
// If the bitwise OR of k and element
// is equal to k, then include that element
// in the subset
if ((arr[i] | k) == k)
v.push(arr[i]);
}
// Store the bitwise OR of elements in v
var ans = 0;
for (var i = 0; i < v.length; i++)
ans |= v[i];
// If ans is not equal to k, subset doesn't exist
if (ans != k) {
document.write( "Subset does not exist" );
return;
}
for (var i = 0; i < v.length; i++)
document.write( v[i] + ' ');
}
// Driver Code
var k = 3;
var arr = [1, 4, 2];
var n = arr.length;
subsetBitwiseORk(arr, n, k);
</script>
Output :
1 2
Time complexity: O(N), where N is the size of the array.
Auxiliary Space : O(N)
Similar Reads
Largest set with bitwise OR equal to n
Given an integer n, find the largest possible set of non-negative integers with bitwise OR equal to n.Examples: Input : n = 5 Output : arr[] = [0, 1, 4, 5] The bitwise OR of 0, 1, 4 and 5 equals 5. It is not possible to obtain a set larger than this. Input : n = 8 Output : arr[] = [0, 8] Prerequisit
5 min read
Size of the smallest subset with maximum Bitwise OR
Given an array of positive integers. The task is to find the size of the smallest subset such that the Bitwise OR of that set is Maximum possible. Examples: Input : arr[] = {5, 1, 3, 4, 2}Output : 2Explanation: 7 is the maximum value possible of OR, 5|2 = 7 and 5|3 = 7 Input : arr[] = {2, 6, 2, 8, 4
15+ min read
Find subsequences with maximum Bitwise AND and Bitwise OR
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
4 min read
Maximum value with no 3 consecutive set bits
Given a non-negative integer n, find the maximum value that can be obtained by unsetting any set bit, such that no three consecutive bits in the resulting integer are set bits. Examples: Input: n = 2Output: 2Explanation: 2's binary form is 10, no 3 consecutive set bits are here. So, 2 itself would b
6 min read
Maximum sum of Bitwise XOR of all elements of two equal length subsets
Given an array arr[] of N integers, where N is an even number. The task is to divide the given N integers into two equal subsets such that the sum of Bitwise XOR of all elements of two subsets is maximum. Examples: Input: N= 4, arr[] = {1, 2, 3, 4} Output: 10 Explanation:There are 3 ways possible: (
14 min read
Count of subarrays with maximum value as K
Given an array arr[] of N integers and an integer K. The task is to find the number of subarrays with a maximum value is equal to K. Examples: Input: arr[ ] = {2, 1, 3, 4}, K = 3Output: 3Explanation: Sub-arrays with maximum value is equals K are { 2, 1, 3 }, { 1, 3 }, { 3 }, hence the answer is 3. I
8 min read
Minimum flips required to maximize a number with k set bits
Given two numbers n and k, we need to find the minimum number of flips required to maximize given number by flipping its bits such that the resulting number has exactly k set bits. Note : K must be less than number of bits in n.Examples : Input : n = 14, k = 2 Output : Min Flips = 1 Explanation : Bi
7 min read
Split Array into maximum Subsets with same bitwise AND
Given an array arr[] of size N, the task is to find the maximum number of subsets the array can be split such that the bitwise AND of the subsets is the same. Examples: Input: N = 4, arr[] = {1, 5, 2, 8}Output: 2Explanation:1st subset -> {1, 8}; bitwise AND = 0 2nd subset -> {2, 5}; bitwise AN
10 min read
Maximum non overlapping Subset with sum greater than K
Given an array, arr[] and integer K, the task is to find the maximum number of non-overlapping subsets such that the sum of the subsets is strictly greater than K when you may also change the value of each element to the maximum value of the particular subset. Examples: Input: arr[]= {90, 80, 70, 60
5 min read
Maximize Bitwise XOR of K with two numbers from Array
Given an integer K and an array arr[] of size N, the task is to choose two elements from the array in such a way that the Bitwise XOR of those two with K (i.e. K â First chosen element â Second chosen element) is the maximum. Note: Any array element can be chosen as many times as possible Examples:
15 min read