Maximize sum of LSBs of Bitwise OR of all possible N / 2 pairs from given Array | Function top get LSB value of v ; Binary conversion ; Function to find the sum of LSBs of all possible pairs of the given array ; Stores the LSB of array elements ; Storing the LSB values ; Sort the array lab_arr [ ] ; Taking pairwise sum to get the maximum sum of LSB ; Print the result ; Driver Code ; Function Call
const chk = ( n ) => { let v = [ ] ; while ( n != 0 ) { v . push ( n % 2 ) ; n = parseInt ( n / 2 ) ; } for ( let i = 0 ; i < v . length ; i ++ ) { if ( v [ i ] == 1 ) { return Math . pow ( 2 , i ) ; } } return 0 ; } const sumOfLSB = ( arr , N ) => { let lsb_arr = [ ] ; for ( let i = 0 ; i < N ; i ++ ) { lsb_arr . push ( chk ( arr [ i ] ) ) ; } lsb_arr . sort ( ( a , b ) => a - b ) let ans = 0 ; for ( let i = 0 ; i < N - 1 ; i += 2 ) { ans += ( lsb_arr [ i + 1 ] ) ; } document . write ( ans ) ; } let N = 5 ; let arr = [ 1 , 2 , 3 , 4 , 5 ] ; sumOfLSB ( arr , N ) ;