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
function maximizedArray ( arr , N ) { let num , i = 0 ; for ( let l = N ; l > 0 ; l -- ) { num = arr [ i ] ; let one = 0 ; let zero = 0 ; while ( num != 0 ) { if ( num % 2 == 0 ) { zero ++ ; } else { one ++ ; } num = num >> 1 ; } for ( let j = zero ; j < ( one + zero ) ; j ++ ) { num += ( 1 << j ) ; } document . write ( num ) ; i ++ ; if ( N > 0 ) document . write ( " " ) ; } } let arr = [ 8 , 15 , 9 , 10 , 14 ] ; let N = arr . length ; maximizedArray ( arr , N ) ;