Generate an array having sum of Bitwise OR of same | Function to print the resultant array ; Stores the sum of the array ; Calculate sum of the array ; If sum > K ; Not possible to construct the array ; Update K ; Stores the resultant array ; Traverse the array ; Stores the current element ; If jth bit is not set and value of 2 ^ j is less than K ; Update curr ; Update K ; Push curr into B ; If K is greater than 0 ; Print the vector B ; Otherwise ; Driver Code ; Input ; Size of the array ; Given input ; Function call to print the required array
def constructArr ( A , N , K ) : NEW_LINE INDENT sum = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT sum += A [ i ] NEW_LINE DEDENT if ( sum > K ) : NEW_LINE INDENT print ( - 1 ) NEW_LINE return NEW_LINE DEDENT K -= sum NEW_LINE B = [ ] NEW_LINE for i in range ( N ) : NEW_LINE INDENT curr = A [ i ] NEW_LINE j = 32 NEW_LINE while ( j >= 0 and K > 0 ) : NEW_LINE INDENT if ( ( curr & ( 1 << j ) ) == 0 and ( 1 << j ) <= K ) : NEW_LINE INDENT curr |= ( 1 << j ) NEW_LINE K -= ( 1 << j ) NEW_LINE DEDENT j -= 1 NEW_LINE DEDENT B . append ( curr ) NEW_LINE DEDENT if ( K == 0 ) : NEW_LINE INDENT for it in B : NEW_LINE INDENT print ( it , end = " ▁ " ) NEW_LINE DEDENT print ( " " , end ▁ = ▁ " " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " - 1" ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 2 , 3 , 4 ] NEW_LINE N = len ( arr ) NEW_LINE K = 32 NEW_LINE constructArr ( arr , N , K ) NEW_LINE DEDENT