Maximum sum of a subsequence whose Bitwise AND is non | Function to find the maximum sum of a subsequence whose Bitwise AND is non - zero ; Stores the resultant maximum sum of the subsequence ; Iterate over all the bits ; Stores the sum of array elements whose i - th bit is set ; Traverse the array elements ; If the bit is set , then add its value to the sum ; Update the resultant maximum sum ; Return the maximum sum ; Driver Code
function maximumSum ( arr , N ) { let ans = 0 ; for ( let bit = 0 ; bit < 32 ; bit ++ ) { let sum = 0 ; for ( let i = 0 ; i < N ; i ++ ) { if ( arr [ i ] & ( 1 << bit ) ) { sum += arr [ i ] ; } } ans = Math . max ( ans , sum ) ; } return ans ; } let arr = [ 5 , 4 , 1 , 7 , 11 ] ; let N = arr . length ; document . write ( maximumSum ( arr , N ) ) ;