Find a number M < N such that difference between their XOR and AND is maximum | Function to return M < N such that N ^ M - N & M is maximum ; Initialize variables ; Iterate for all values < N ; Find the difference between Bitwise XOR and AND ; Check if new difference is greater than previous maximum ; Update variables ; Return the answer ; Given Number N ; Function Call
function getMaxDifference ( N ) { var M = - 1 ; var maxDiff = 0 ; for ( i = 0 ; i < N ; i ++ ) { var diff = ( N ^ i ) - ( N & i ) ; if ( diff >= maxDiff ) { maxDiff = diff ; M = i ; } } return M ; } var N = 6 ; document . write ( getMaxDifference ( N ) ) ;