Given a natural number** N**, the task is to find the largest number **M** having the same length in binary representation as **N** such that the difference between **N |** **M **and **N ^ M** is maximum.

**Examples:**

Input:N = 6Output:7Explanation:

All number numbers having same length in binary representation as N are 4, 5, 6, 7.

(6 | 4) – (6 ^ 4) = 4

(6 | 5) – (6 ^ 5) = 4

(6 | 6) – (6 ^ 6) = 6

(6 | 7) – (6 ^ 7) = 6

Hence, largest M for which (N | M) – (N ^ M) is maximum is 7

Input:N = 10Output:15Explanation:

The largest number M = 15 which has the same length in binary representation as 10 and the difference betweenN |MandN ^ Mis maximum.

**Naive Approach:** The idea is to simply find all the numbers having the same length in binary representation as** N** and then for every number iterate and find the largest** **integer** **having **(N | i) – (N ^ i) **maximum.

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

**Efficient Approach:** The idea is to initialize **M = 0** and iterate bit by bit in N (say **i**) and set or unset the** i ^{th}** bit of

**M**according to the following 2 observations :

- When an i
^{th}bit of N is**set**: In this case, if we**unset**the i^{th}bit of M, i^{th}bit of both**N | M**and**N^M**will be set whereas on setting this bit of M, an i^{th}bit of**N|M**will be set and**N^M**will be unset which will increase**(N | M) – (N ^ M)**. Hence, it is optimal to set this bit of**M**. - When an i
^{th}bit of N is**unset:**In this case, if we**set**this bit of**N|M**and N^M will have this bit set or on keeping this bit of M unset both**N|M**and**N^M**will have this bit unset. So, in this case, we cannot increase the difference between them but as the requirement is to output the maximum**M**possible, so set this bit of**M**. - From the above observations, it is clear that
**M**will have all the bits set.

Below is the implementation of the above approach:

## C++

`// C++ program for the above approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to find the largest number ` `// M having the same length in binary ` `// form as N such that the difference ` `// between N | M and N ^ M is maximum ` `int` `maxORminusXOR(` `int` `N) ` `{ ` ` ` `// Find the most significant ` ` ` `// bit of N ` ` ` `int` `MSB = log2(N); ` ` ` ` ` `// Initialize M ` ` ` `int` `M = 0; ` ` ` ` ` `// Set all the bits of M ` ` ` `for` `(` `int` `i = 0; i <= MSB; i++) ` ` ` `M += (1 << i); ` ` ` ` ` `// Return the answer ` ` ` `return` `M; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `// Given Number N ` ` ` `int` `N = 10; ` ` ` ` ` `// Function Call ` ` ` `cout << maxORminusXOR(N); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program for the above approach ` `import` `java.util.*; ` ` ` `class` `GFG{ ` ` ` `// Function to find the largest number ` `// M having the same length in binary ` `// form as N such that the difference ` `// between N | M and N ^ M is maximum ` `static` `int` `maxORminusXOR(` `int` `N) ` `{ ` ` ` ` ` `// Find the most significant ` ` ` `// bit of N ` ` ` `int` `MSB = (` `int` `)Math.ceil(Math.log(N)); ` ` ` ` ` `// Initialize M ` ` ` `int` `M = ` `0` `; ` ` ` ` ` `// Set all the bits of M ` ` ` `for` `(` `int` `i = ` `0` `; i <= MSB; i++) ` ` ` `M += (` `1` `<< i); ` ` ` ` ` `// Return the answer ` ` ` `return` `M; ` `} ` ` ` `// Driver Code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` ` ` `// Given number N ` ` ` `int` `N = ` `10` `; ` ` ` ` ` `// Function call ` ` ` `System.out.print(maxORminusXOR(N)); ` `} ` `} ` ` ` `// This code is contributed by Rajput-Ji ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program for the above approach ` `import` `math ` ` ` `# Function to find the largest number ` `# M having the same length in binary ` `# form as N such that the difference ` `# between N | M and N ^ M is maximum ` `def` `maxORminusXOR(N): ` ` ` ` ` `# Find the most significant ` ` ` `# bit of N ` ` ` `MSB ` `=` `int` `(math.log2(N)); ` ` ` ` ` `# Initialize M ` ` ` `M ` `=` `0` ` ` ` ` `# Set all the bits of M ` ` ` `for` `i ` `in` `range` `(MSB ` `+` `1` `): ` ` ` `M ` `+` `=` `(` `1` `<< i) ` ` ` ` ` `# Return the answer ` ` ` `return` `M ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `'__main__'` `: ` ` ` ` ` `# Given Number N ` ` ` `N ` `=` `10` ` ` ` ` `# Function call ` ` ` `print` `(maxORminusXOR(N)) ` ` ` `# This code is contributed by jana_sayantan ` |

*chevron_right*

*filter_none*

## C#

`// C# program for the above approach ` `using` `System; ` ` ` `class` `GFG{ ` ` ` `// Function to find the largest number ` `// M having the same length in binary ` `// form as N such that the difference ` `// between N | M and N ^ M is maximum ` `static` `int` `maxORminusXOR(` `int` `N) ` `{ ` ` ` ` ` `// Find the most significant ` ` ` `// bit of N ` ` ` `int` `MSB = (` `int` `)Math.Ceiling(Math.Log(N)); ` ` ` ` ` `// Initialize M ` ` ` `int` `M = 0; ` ` ` ` ` `// Set all the bits of M ` ` ` `for` `(` `int` `i = 0; i <= MSB; i++) ` ` ` `M += (1 << i); ` ` ` ` ` `// Return the answer ` ` ` `return` `M; ` `} ` ` ` `// Driver Code ` `public` `static` `void` `Main(String[] args) ` `{ ` ` ` ` ` `// Given number N ` ` ` `int` `N = 10; ` ` ` ` ` `// Function call ` ` ` `Console.Write(maxORminusXOR(N)); ` `} ` `} ` ` ` `// This code is contributed by 29AjayKumar ` |

*chevron_right*

*filter_none*

**Output:**

15

**Time Complexity:** O(log 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:

- Count pairs from 1 to N such that their Sum is divisible by their XOR
- Find a number M < N such that difference between their XOR and AND is maximum
- Largest possible value of M not exceeding N having equal Bitwise OR and XOR between them
- Find K numbers with sum equal to N and sum of their squares maximized
- Possible values of Q such that, for any value of R, their product is equal to X times their sum
- Count of pairs of (i, j) such that ((n % i) % j) % n is maximized
- Count ordered pairs of positive numbers such that their sum is S and XOR is K
- Count all Quadruples from four arrays such that their XOR equals to 'x'
- Count of pairs in a given range with sum of their product and sum equal to their concatenated number
- Count number of subsets having a particular XOR value
- Generate an Array such with elements maximized through swapping bits
- Lengths of maximized partitions of a string such that each character of the string appears in one substring
- Maximized partitions of a string such that each character of the string appears in one substring
- Find number of pairs in an array such that their XOR is 0
- Count the pairs in an array such that the difference between them and their indices is equal
- Inserting m into n such that m starts at bit j and ends at bit i.
- Inserting M into N such that m starts at bit j and ends at bit i | Set-2
- Count pairs from an array having product of their sum and difference equal to 0
- Count pairs from an array having product of their sum and difference equal to 1
- Minimum bit flips such that every K consecutive bits contain at least one set bit

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.