Maximum subset with bitwise OR equal to k | C # 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 ; main function
using System ; using System . Collections ; class GFG { static void subsetBitwiseORk ( int [ ] arr , int n , int k ) { ArrayList v = new ArrayList ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( ( arr [ i ] k ) == k ) { v . Add ( arr [ i ] ) ; } } int ans = 0 ; for ( int i = 0 ; i < v . Count ; i ++ ) ans = ans | ( int ) v [ i ] ; if ( ans != k ) { Console . WriteLine ( " Subset ▁ does " + " ▁ not ▁ exist " ) ; return ; } for ( int i = 0 ; i < v . Count ; i ++ ) Console . Write ( ( int ) v [ i ] + " ▁ " ) ; } static public void Main ( String [ ] args ) { int k = 3 ; int [ ] arr = { 1 , 4 , 2 } ; int n = arr . Length ; subsetBitwiseORk ( arr , n , k ) ; } }