# Find a number M < N such that difference between their XOR and AND is maximum

Given a natural number N, the task is to find a number M smaller than N such that the difference between their bitwise XOR (N ^ M ) and bitwise AND (N & M) is maximum.

Examples:

Input: N = 4
Output: 3
Explanation:
(4 ^ 0) – (4 & 0) = 4
(4 ^ 1) – (4 & 1) = 5
(4 ^ 2) – (4 & 2) = 6
(4 ^ 3) – (4 & 3) = 7
Hence, the value of M is 3.

Input: N = 6
Output: 1
Explanation:
The difference between N ^ M and N & M is maximum when M = 1.

Naive Approach: The idea is to iterate for every element less than N and find M for which N^M – N&M is maximum. Below are the steps:

• Initialize a variable let’s say maxDiff with 0 and M with -1.
• Iterate from 0 to N-1 and calculate diff = N^i -N&i.
• If diff is greater or equal to maxDiff assign M = i and maxDiff = diff.

Below is the implementation of the above approach:

## C++

 `// C++ program for the above approach ` `#include ` `using` `namespace` `std; ` ` `  `// Function to return M= maxDiff) { ` ` `  `            ``// Update variables ` `            ``maxDiff = diff; ` `            ``M = i; ` `        ``} ` `    ``} ` ` `  `    ``// Return the answer ` `    ``return` `M; ` `} ` ` `  `// Driver Code ` `int` `main() ` `{ ` `    ``// Given Number N ` `    ``int` `N = 6; ` ` `  `    ``// Function Call ` `    ``cout << getMaxDifference(N); ` `    ``return` `0; ` `} `

Output:

```1
```

Time Complexity: O(N)
Auxiliary Space: O(1)

Efficient Approach: The idea is to observe that the difference between Bitwise XOR and Bitwise AND is maximum if Bitwise AND of the two numbers is minimum possible number and the minimum possible number is 0
The Bitwise AND between the two numbers is zero if and only if they complement each other. Therefore the possible value of M must be the complement of the given number N.

Below is the implementation of the above approach:

## C++

 `// C++ program for the above approach ` `#include ` `using` `namespace` `std; ` ` `  `// Function to flip all bits of N ` `int` `findM(``int` `N) ` `{ ` `    ``int` `M = 0; ` ` `  `    ``// Finding most signifcant bit of N ` `    ``int` `MSB = (``int``)log2(N); ` ` `  `    ``// Calculating required number ` `    ``for` `(``int` `i = 0; i < MSB; i++) { ` ` `  `        ``if` `(!(N & (1 << i))) ` `            ``M += (1 << i); ` `    ``} ` ` `  `    ``// Return the answer ` `    ``return` `M; ` `} ` ` `  `// Driver Code ` `int` `main() ` `{ ` `    ``// Given Number ` `    ``int` `N = 6; ` ` `  `    ``// Function Call ` `    ``cout << findM(N); ` `    ``return` `0; ` `} `

## Java

 `// Java program for the above approach ` `class` `GFG{ ` ` `  `// Function to flip all bits of N ` `static` `int` `findM(``int` `N) ` `{ ` `    ``int` `M = ``0``; ` ` `  `    ``// Finding most signifcant bit of N ` `    ``int` `MSB = (``int``)Math.log(N); ` ` `  `    ``// Calculating required number ` `    ``for``(``int` `i = ``0``; i < MSB; i++)  ` `    ``{ ` `        ``if` `((N & (``1` `<< i)) == ``0``) ` `            ``M += (``1` `<< i); ` `    ``} ` ` `  `    ``// Return the answer ` `    ``return` `M; ` `} ` ` `  `// Driver Code ` `public` `static` `void` `main(String[] args) ` `{ ` `     `  `    ``// Given number ` `    ``int` `N = ``6``; ` ` `  `    ``// Function call ` `    ``System.out.print(findM(N)); ` `} ` `} ` ` `  `// This code is contributed by Rajput-Ji `

## C#

 `// C# program for the above approach ` `using` `System; ` ` `  `class` `GFG{ ` ` `  `// Function to flip all bits of N ` `static` `int` `findM(``int` `N) ` `{ ` `    ``int` `M = 0; ` ` `  `    ``// Finding most signifcant bit of N ` `    ``int` `MSB = (``int``)Math.Log(N); ` ` `  `    ``// Calculating required number ` `    ``for``(``int` `i = 0; i < MSB; i++)  ` `    ``{ ` `        ``if` `((N & (1 << i)) == 0) ` `            ``M += (1 << i); ` `    ``} ` ` `  `    ``// Return the answer ` `    ``return` `M; ` `} ` ` `  `// Driver Code ` `public` `static` `void` `Main(String[] args) ` `{ ` `     `  `    ``// Given number ` `    ``int` `N = 6; ` ` `  `    ``// Function call ` `    ``Console.Write(findM(N)); ` `} ` `} ` ` `  `// This code is contributed by Amit Katiyar `

Output:

```1
```

Time Complexity: O(log2N)
Auxiliary Space: O(1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Improved By : Rajput-Ji, amit143katiyar