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
def build_num ( bit ) : NEW_LINE INDENT ans = 0 NEW_LINE for i in range ( 0 , 32 ) : NEW_LINE INDENT if ( bit [ i ] ) : NEW_LINE INDENT ans += ( 1 << i ) NEW_LINE DEDENT DEDENT return ans ; NEW_LINE DEDENT def maximumOR ( arr , n , k ) : NEW_LINE INDENT bit = [ 0 ] * 32 NEW_LINE for i in range ( 31 , - 1 , - 1 ) : NEW_LINE INDENT if ( bit [ i ] == 0 and k > 0 ) : NEW_LINE INDENT temp = build_num ( bit ) NEW_LINE temp1 = temp NEW_LINE val = - 1 NEW_LINE for j in range ( 0 , n ) : NEW_LINE INDENT if ( temp1 < ( temp arr [ j ] ) ) : NEW_LINE INDENT temp1 = temp | arr [ j ] NEW_LINE val = arr [ j ] NEW_LINE DEDENT DEDENT if ( val != - 1 ) : NEW_LINE INDENT k -= 1 NEW_LINE for j in range ( 0 , 32 ) : NEW_LINE INDENT if ( val & ( 1 << j ) ) : NEW_LINE INDENT bit [ j ] += 1 NEW_LINE DEDENT DEDENT DEDENT DEDENT DEDENT return build_num ( bit ) NEW_LINE DEDENT arr = [ 5 , 9 , 7 , 19 ] NEW_LINE k = 3 ; NEW_LINE n = len ( arr ) NEW_LINE print ( maximumOR ( arr , n , k ) ) NEW_LINE