Maximum subset with bitwise OR equal to k | Java 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
import java . util . * ; class GFG { static void subsetBitwiseORk ( int arr [ ] , int n , int k ) { ArrayList < Integer > v = new ArrayList < Integer > ( ) ; 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 . size ( ) ; i ++ ) ans = ans | v . get ( i ) ; 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 ) + " ▁ " ) ; } public static void main ( String [ ] args ) { int k = 3 ; int arr [ ] = { 1 , 4 , 2 } ; int n = arr . length ; subsetBitwiseORk ( arr , n , k ) ; } }