Given an integer **N**, the task is to find the largest number **M**, where (**M < N**), such that **N(XOR)M** is equal to **N(OR)M** i.e. **(N ^ M) = (N | M)**.

**Examples:**

Input:N = 5Output:2

5 ^ 4 = 1 and 5 | 4 = 5. Therefore,XORandORbetween them are not equal.

5 ^ 3 = 6 and 5 | 3 = 7. Therefore,XORandORbetween them are not equal.

5 ^ 2 = 7 and 5 | 2 = 7. Therefore,XORandORbetween them are equal.

Input:N = 14Output:1

**Approach:**

To get the required number** M**, traverse all the bits of **N** from its Least Significant Bit (LSB) to Most Significant Bit (MSB). Two cases arise here:

- If the
**i**of^{th}bit**N**is**1**then:- If the
**i**of^{th}bit**M**is set to**1**, then**N^M**will not be equal to**N|M**as**(1^1 = 0)**and**(1|1 = 1)**. - If the
**i**is set of^{th}bit**M**to**0**, then**N^M**will be equal to**N|M**as**(1^0 = 1)**and**(1|0 = 1)**. - So if the
**i**of^{th}bit**N**is**1**, set the**i**of^{th}bit**M**to**0**.

- If the
- If the
**i**of^{th}bit**N**is**0**then:- If the
**i**of^{th}bit**M**is set to**1**, then**N^M**will be equal to**N|M**as (**0^1 = 1**) and (**0|1 = 1**). - If we set the
**i**of^{th}bit**M**to**0**, then**N^M**will be equal to**N|M**as (**0^0 = 0**) and (**0|0 = 0**). - So, if the
**i**of^{th}bit**M**is set to either**0**or**1**,**N^M**will always be equal to**N|M**. - As the largest value of
**M**which is less than**N**has to be found out, always set the**i**of^{th}bit**M**to**1**.

- If the

Illustration:

- N = 5
- 32-bit representation of 5 = 00000000000000000000000000000101
- LSB index of 5 = 31
- MSB index of 5 = 29
- Traversing from LSB to MSB i.e. from 31 to 29:

- For index 31, N[31] = 1. So M[31] should be set to 0.
- For index 30, N[30] = 0. So M[30] should be set to 1.
- For index 29, N[29] = 1. So M[29] should be set to 0.
- Thus the 32-bit representation of M is 00000000000000000000000000000010, which is equal to 2 in decimal representation.

Below is the implementation of the above approach:

## C++

`// C++ Program to implement ` `// the above approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to find required ` `// number M ` `int` `equalXORandOR(` `int` `n) ` `{ ` ` ` `// Initialising m ` ` ` `int` `m = 0; ` ` ` ` ` `// Finding the index of the ` ` ` `// most significant 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` `m; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `int` `n = 14; ` ` ` `cout << equalXORandOR(n); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to implement ` `// the above approach ` `class` `GFG{ ` ` ` `// Function to find required ` `// number M ` `static` `int` `equalXORandOR(` `int` `n) ` `{ ` ` ` ` ` `// Initialising m ` ` ` `int` `m = ` `0` `; ` ` ` ` ` `// Finding the index of the ` ` ` `// most significant 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` `m; ` `} ` ` ` `// Driver Code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `int` `n = ` `14` `; ` ` ` ` ` `System.out.print(equalXORandOR(n)); ` `} ` `} ` ` ` `// This code is contributed by amal kumar choubey ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to implement ` `# the above approach ` `from` `math ` `import` `log2 ` ` ` `# Function to find required ` `# number M ` `def` `equalXORandOR(n): ` ` ` ` ` `# Initialising m ` ` ` `m ` `=` `0` ` ` ` ` `# Finding the index of the ` ` ` `# most significant bit of N ` ` ` `MSB ` `=` `int` `(log2(n)) ` ` ` ` ` `# Calculating required number ` ` ` `for` `i ` `in` `range` `(MSB ` `+` `1` `): ` ` ` `if` `(` `not` `(n & (` `1` `<< i))): ` ` ` `m ` `+` `=` `(` `1` `<< i) ` ` ` ` ` `return` `m ` ` ` `# Driver Code ` `n ` `=` `14` ` ` `# Function call ` `print` `(equalXORandOR(n)) ` ` ` `# This code is contributed by Shivam Singh` |

*chevron_right*

*filter_none*

## C#

`// C# program to implement ` `// the above approach ` `using` `System; ` ` ` `class` `GFG{ ` ` ` `// Function to find required ` `// number M ` `static` `int` `equalXORandOR(` `int` `n) ` `{ ` ` ` ` ` `// Initialising m ` ` ` `int` `m = 0; ` ` ` ` ` `// Finding the index of the ` ` ` `// most significant 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` `m; ` `} ` ` ` `// Driver Code ` `public` `static` `void` `Main(String[] args) ` `{ ` ` ` `int` `n = 14; ` ` ` ` ` `Console.Write(equalXORandOR(n)); ` `} ` `} ` ` ` `// This code is contributed by amal kumar choubey` |

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

- Construct a Matrix with no element exceeding X and sum of two adjacent elements not exceeding Y
- Count ways to generate pairs having Bitwise XOR and Bitwise AND equal to X and Y respectively
- Total pairs in an array such that the bitwise AND, bitwise OR and bitwise XOR of LSB is 1
- Split an Array to maximize subarrays having equal count of odd and even elements for a cost not exceeding K
- Count of all possible numbers not exceeding M having suffix N
- Pair of integers having least GCD among all given pairs having GCD exceeding K
- Largest number M having bit count of N such that difference between their OR and XOR value is maximized
- Count number of triangles possible with length of sides not exceeding N
- Minimize count of Subsets with difference between maximum and minimum element not exceeding K
- Leftover element after performing alternate Bitwise OR and Bitwise XOR operations on adjacent pairs
- Split array into minimum number of subarrays having GCD of its first and last element exceeding 1
- Minimum possible Bitwise OR of all Bitwise AND of pairs generated from two given arrays
- Maximum students to pass after giving bonus to everybody and not exceeding 100 marks
- Make A, B and C equal by adding total value N to them
- Minimum possible value T such that at most D Partitions of the Array having at most sum T is possible
- Bitwise XOR of elements having odd frequency
- Count of N-digit numbers with absolute difference of adjacent digits not exceeding K
- Count of N-digit numbers with absolute difference of adjacent digits not exceeding K | Set 2
- Count of pairs having bit size at most X and Bitwise OR equal to X
- Count the pairs in an array such that the difference between them and their indices is equal

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.