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 ; Driver Code ; Given number N ; Function call
def getMaxDifference ( N ) : NEW_LINE INDENT M = - 1 ; NEW_LINE maxDiff = 0 ; NEW_LINE for i in range ( N ) : NEW_LINE INDENT diff = ( N ^ i ) - ( N & i ) ; NEW_LINE if ( diff >= maxDiff ) : NEW_LINE INDENT maxDiff = diff ; NEW_LINE M = i ; NEW_LINE DEDENT DEDENT return M ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 6 ; NEW_LINE print ( getMaxDifference ( N ) ) ; NEW_LINE DEDENT