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*

## Java

`// Java program for the above approach ` `class` `GFG{ ` ` ` `// Function to return M<N such that ` `// N^M - N&M is maximum ` `static` `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 ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `// Given Number N ` ` ` `int` `N = ` `6` `; ` ` ` ` ` `// Function Call ` ` ` `System.out.print(getMaxDifference(N)); ` `} ` `} ` ` ` `// This code is contributed by Rohit_ranjan` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program for the above approach ` ` ` `# Function to return M<N such that ` `# N^M - N&M is maximum ` `def` `getMaxDifference(N): ` ` ` ` ` `# Initialize variables ` ` ` `M ` `=` `-` `1` `; ` ` ` `maxDiff ` `=` `0` `; ` ` ` ` ` `# Iterate for all values < N ` ` ` `for` `i ` `in` `range` `(N): ` ` ` ` ` `# Find the difference between ` ` ` `# Bitwise XOR and AND ` ` ` `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 ` `if` `__name__ ` `=` `=` `'__main__'` `: ` ` ` ` ` `# Given number N ` ` ` `N ` `=` `6` `; ` ` ` ` ` `# Function call ` ` ` `print` `(getMaxDifference(N)); ` ` ` `# This code is contributed by amal kumar choubey` |

*chevron_right*

*filter_none*

## C#

`// C# program for the above approach ` `using` `System; ` ` ` `class` `GFG{ ` ` ` `// Function to return M<N such that ` `// N^M - N&M is maximum ` `static` `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 ` `public` `static` `void` `Main(String[] args) ` `{ ` ` ` ` ` `// Given number N ` ` ` `int` `N = 6; ` ` ` ` ` `// Function call ` ` ` `Console.Write(getMaxDifference(N)); ` `} ` `} ` ` ` `// This code is contributed by Rajput-Ji ` |

*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*

## Python3

`# Python3 program for the above approach ` `import` `math ` ` ` `# Function to flip all bits of N ` `def` `findM(N): ` ` ` ` ` `M ` `=` `0` `; ` ` ` ` ` `# Finding most signifcant bit of N ` ` ` `MSB ` `=` `int` `(math.log(N)); ` ` ` ` ` `# Calculating required number ` ` ` `for` `i ` `in` `range` `(MSB): ` ` ` `if` `((N & (` `1` `<< i)) ` `=` `=` `0` `): ` ` ` `M ` `+` `=` `(` `1` `<< i); ` ` ` ` ` `# Return the answer ` ` ` `return` `M; ` ` ` `# Driver Code ` `if` `__name__ ` `=` `=` `'__main__'` `: ` ` ` ` ` `# Given number ` ` ` `N ` `=` `6` `; ` ` ` ` ` `# Function call ` ` ` `print` `(findM(N)); ` ` ` `# This code is contributed by Amit Katiyar ` |

*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.