Find element with the maximum set bits in an array | Function to return the element from the array which has the maximum set bits ; To store the required element and the maximum set bits so far ; Count of set bits in the current element ; Update the max ; Driver code
< ? php function maxBitElement ( $ arr , $ n ) { $ num = 0 ; $ max = -1 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ cnt = BitCount ( $ arr [ $ i ] ) ; if ( $ cnt > $ max ) { $ max = $ cnt ; $ num = $ arr [ $ i ] ; } } return $ num ; } function BitCount ( $ n ) { $ count = 0 ; while ( $ n != 0 ) { $ count ++ ; $ n &= ( $ n - 1 ) ; } return $ count ; } $ arr = array ( 3 , 2 , 4 , 7 , 1 , 10 , 5 , 8 , 9 , 6 ) ; $ n = count ( $ arr ) ; echo ( maxBitElement ( $ arr , $ n ) ) ; ? >