Maximum bitwise OR value of subsequence of length K | Function to convert bit array to decimal number ; Return the final result ; Function to find the maximum Bitwise OR value of subsequence of length K ; Initialize bit array of size 32 with all value as 0 ; Iterate for each index of bit [ ] array from 31 to 0 , and check if the ith value of bit array is 0 ; Check for maximum contributing element ; Update the bit array if max_contributing element is found ; Decrement the value of K ; Return the result ; Given array arr [ ] ; Length of subsequence ; Function Call
function build_num ( bit ) { let ans = 0 ; for ( let i = 0 ; i < 32 ; i ++ ) if ( bit [ i ] > 0 ) ans += ( 1 << i ) ; return ans ; } function maximumOR ( arr , n , k ) { let bit = new Array ( 32 ) ; bit . fill ( 0 ) ; for ( let i = 31 ; i >= 0 ; i -- ) { if ( bit [ i ] == 0 && k > 0 ) { let temp = build_num ( bit ) ; let temp1 = temp ; let val = - 1 ; for ( let j = 0 ; j < n ; j ++ ) { if ( temp1 < ( temp arr [ j ] ) ) { temp1 = temp | arr [ j ] ; val = arr [ j ] ; } } if ( val != - 1 ) { k -- ; for ( let j = 0 ; j < 32 ; j ++ ) { if ( ( val & ( 1 << j ) ) > 0 ) bit [ j ] ++ ; } } } } return build_num ( bit ) ; } let arr = [ 5 , 9 , 7 , 19 ] ; let k = 3 ; let n = arr . length ; document . write ( maximumOR ( arr , n , k ) ) ;