Maximize given integer by swapping pairs of unequal bits | Function to return the maximum possible value that can be obtained from the given integer by performing given operations ; Convert to equivalent binary representation ; Stores binary representation of the maximized value ; Store the count of 0 ' s ▁ and ▁ 1' s ; Stores the total Number of Bits ; If current bit is set ; Increment Count1 ; Increment Count0 ; Shift all set bits to left to maximize the value ; Shift all unset bits to right to maximize the value ; Return the maximized value ; Given "Input ; Function call to find the Maximum Possible Number
function findMaxNum ( num ) { var binaryNumber = Number ( num ) . toString ( 2 ) ; ; var maxBinaryNumber = " " ; var count0 = 0 , count1 = 0 ; var N = binaryNumber . length ; for ( i = 0 ; i < N ; i ++ ) { if ( binaryNumber . charAt ( i ) == ' ' ) { count1 ++ ; } else { count0 ++ ; } } for ( i = 0 ; i < count1 ; i ++ ) { maxBinaryNumber += ' ' ; } for ( i = 0 ; i < count0 ; i ++ ) { maxBinaryNumber += ' ' ; } return parseInt ( maxBinaryNumber , 2 ) ; } var N = 11 ; document . write ( findMaxNum ( N ) ) ;