Find a number M < N such that difference between their XOR and AND is maximum | Python3 program for the above approach ; Function to flip all bits of N ; Finding most significant bit of N ; Calculating required number ; Return the answer ; Driver Code ; Given number ; Function call
import math NEW_LINE def findM ( N ) : NEW_LINE INDENT M = 0 ; NEW_LINE MSB = int ( math . log ( N ) ) ; NEW_LINE for i in range ( MSB ) : NEW_LINE INDENT if ( ( N & ( 1 << i ) ) == 0 ) : NEW_LINE INDENT M += ( 1 << i ) ; NEW_LINE DEDENT DEDENT return M ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 6 ; NEW_LINE print ( findM ( N ) ) ; NEW_LINE DEDENT