Generate an Array such with elements maximized through swapping bits | Function to generate the maximized array elements ; Traverse the array ; Iterate to count set and unset bits ; Count of unset bits ; Count of set bits ; Bitwise right shift ; Shifting all 1 ' s ▁ to ▁ MSB ▁ ▁ and ▁ 0' s to LSB ; Driver Code
def maximizedArray ( arr , N ) : NEW_LINE INDENT i = 0 NEW_LINE while ( N > 0 ) : NEW_LINE INDENT num = arr [ i ] NEW_LINE one = 0 NEW_LINE zero = 0 NEW_LINE while ( num ) : NEW_LINE INDENT if ( num % 2 == 0 ) : NEW_LINE INDENT zero += 1 NEW_LINE DEDENT else : NEW_LINE INDENT one += 1 NEW_LINE DEDENT num = num >> 1 NEW_LINE DEDENT for j in range ( zero , ( one + zero ) ) : NEW_LINE INDENT num += ( 1 << j ) NEW_LINE DEDENT print ( num , end = " " ) NEW_LINE i += 1 NEW_LINE if ( N > 0 ) : NEW_LINE INDENT print ( " , ▁ " , end = " " ) NEW_LINE DEDENT N -= 1 NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 8 , 15 , 9 , 10 , 14 ] NEW_LINE N = len ( arr ) NEW_LINE maximizedArray ( arr , N ) NEW_LINE DEDENT