Maximum subset with bitwise OR equal to k | CPP Program to find the maximum subset with bitwise OR equal to k ; function to find the maximum subset with bitwise OR equal to k ; If the bitwise OR of k and element is equal to k , then include that element in the subset ; Store the bitwise OR of elements in v ; If ans is not equal to k , subset doesn 't exist ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void subsetBitwiseORk ( int arr [ ] , int n , int k ) { vector < int > v ; for ( int i = 0 ; i < n ; i ++ ) { if ( ( arr [ i ] k ) == k ) v . push_back ( arr [ i ] ) ; } int ans = 0 ; for ( int i = 0 ; i < v . size ( ) ; i ++ ) ans |= v [ i ] ; if ( ans != k ) { cout << " Subset ▁ does ▁ not ▁ exist " << endl ; return ; } for ( int i = 0 ; i < v . size ( ) ; i ++ ) cout << v [ i ] << ' ▁ ' ; } int main ( ) { int k = 3 ; int arr [ ] = { 1 , 4 , 2 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; subsetBitwiseORk ( arr , n , k ) ; return 0 ; }