Find the size of Largest Subset with positive Bitwise AND | Java program for the above approach ; Function to find the largest possible subset having Bitwise AND positive ; Stores the number of set bits at each bit position ; Traverse the given array arr [ ] ; Current bit position ; Loop till array element becomes zero ; If the last bit is set ; Increment frequency ; Divide array element by 2 ; Decrease the bit position ; Size of the largest possible subset ; Driver code
import java . io . * ; class GFG { static void largestSubset ( int a [ ] , int N ) { int bit [ ] = new int [ 32 ] ; for ( int i = 0 ; i < N ; i ++ ) { int x = 31 ; while ( a [ i ] > 0 ) { if ( ( int ) ( a [ i ] & 1 ) == ( int ) 1 ) { bit [ x ] ++ ; } a [ i ] = a [ i ] >> 1 ; x -- ; } } int max = Integer . MIN_VALUE ; for ( int i = 0 ; i < 32 ; i ++ ) { max = Math . max ( max , bit [ i ] ) ; } System . out . println ( max ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 7 , 13 , 8 , 2 , 3 } ; int N = arr . length ; largestSubset ( arr , N ) ; } }