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 = 4Output:3Explanation:

(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 = 6Output:1Explanation:

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 <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to return M<N such that ` `// N^M - N&M is maximum ` `int` `getMaxDifference(` `int` `N) ` `{ ` ` ` `// Initialize variables ` ` ` `int` `M = -1; ` ` ` `int` `maxDiff = 0; ` ` ` ` ` `// Iterate for all values < N ` ` ` `for` `(` `int` `i = 0; i < N; i++) { ` ` ` ` ` `// Find the difference between ` ` ` `// Bitwise XOR and AND ` ` ` `int` `diff = (N ^ i) - (N & i); ` ` ` ` ` `// Check if new diffference is ` ` ` `// greater than previous maximum ` ` ` `if` `(diff >= 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; ` `} ` |

*chevron_right*

*filter_none*

**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 <bits/stdc++.h> ` `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; ` `} ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

**Output:**

1

**Time Complexity:** O(log_{2}N) **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.

## Recommended Posts:

- Find the maximum difference after applying the given operations two times on a number
- Find the largest number smaller than integer N with maximum number of set bits
- Program to find the maximum difference between the index of any two different numbers
- Find the maximum number of composite summands of a number
- Find maximum number that can be formed using digits of a given number
- Find M such that GCD of M and given number N is maximum
- Find a value whose XOR with given number is maximum
- Find a positive number M such that gcd(N^M, N&M) is maximum
- Find the maximum number of handshakes
- Find the maximum possible Binary Number from given string
- Find prime number K in an array such that (A[i] % K) is maximum
- Find sum of a number and its maximum prime factor
- Find the maximum number of elements divisible by 3
- Find the Number of Maximum Product Quadruples
- Find a number such that maximum in array is minimum possible after XOR
- Find the number in a range having maximum product of the digits
- Find maximum power of a number that divides a factorial
- Find a number that divides maximum array elements
- Missing occurrences of a number in an array such that maximum absolute difference of adjacent elements is minimum
- Find integers that divides maximum number of elements of the array

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.