Given a positive integer **N**, the task is to find the largest integer **M** such that **0 <= M < N** and **XOR(M, N)** is an even number. If such a value of M cannot be obtained for given **N**, print **-1**.

**Examples:**

Input:N = 10

Output:8

Explanation:

(10 XOR 9) = 3, so M = 9 is not possible because 3 is not an even number.

(10 XOR 8) = 2, so M = 8 is possible as it is the largest possible value of M satisfying the necessary conditions.

Input:N = 5

Output:3

(5 XOR 4) = 1, so M = 4 is not possible because 1 is not an even number.

(5 XOR 3) = 6, so M = 3 is possible as 6 is an even number and 3 is the largest possible value of M (which is less than N).

**Approach:**

Following observations need to be made while solving the problem:

- An odd number represented in its binary form has 1 as its Least Significant Bit(LSB), whereas an even number represented in its binary form has 0 as its Least Significant Bit(LSB).
- While performing
**XOR**operation, if the number of set bits is odd, then the XOR value will be 1. If the number of set bits is even, then the XOR value will be 0. - Hence, we need to perform XOR of two odd numbers or two even numbers to obtain an even number as the result.

From the above explanation, the problem can be solved by the following steps:

- If N is odd, the largest odd number which is less than N will be
**N – 2**. - If N is even, the largest even number which is less than N will be
**N – 2**. - Hence, if N = 1, print -1 as a suitable M cannot be obtained in this case. For all other cases, print
**N – 2**as the aswer.

Below is the implementation of the above approach:

## C++

`// C++ program for the above problem. ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to find the maximum ` `// possible value of M ` `int` `getM(` `int` `n) ` `{ ` ` ` `// Edge case ` ` ` `if` `(n == 1) ` ` ` `return` `-1; ` ` ` ` ` `// M = N - 2 is maximum ` ` ` `// possible value ` ` ` `else` ` ` `return` `n - 2; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `int` `n = 10; ` ` ` ` ` `int` `ans = getM(n); ` ` ` `cout << ans; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program for the above problem. ` `import` `java.util.*; ` ` ` `class` `GFG{ ` ` ` `// Function to find the maximum ` `// possible value of M ` `static` `int` `getM(` `int` `n) ` `{ ` ` ` ` ` `// Edge case ` ` ` `if` `(n == ` `1` `) ` ` ` `return` `-` `1` `; ` ` ` ` ` `// M = N - 2 is maximum ` ` ` `// possible value ` ` ` `else` ` ` `return` `n - ` `2` `; ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `int` `n = ` `10` `; ` ` ` ` ` `System.out.print(getM(n)); ` `} ` `} ` ` ` `// This code is contributed by sanjoy_62 ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program for the above problem ` ` ` `# Function to find the maximum ` `# possible value of M ` `def` `getM(n): ` ` ` ` ` `# Edge case ` ` ` `if` `(n ` `=` `=` `1` `): ` ` ` `return` `-` `1` `; ` ` ` ` ` `# M = N - 2 is maximum ` ` ` `# possible value ` ` ` `else` `: ` ` ` `return` `n ` `-` `2` `; ` ` ` `# Driver code ` `n ` `=` `10` ` ` `print` `(getM(n)) ` ` ` `# This code is contributed by sanjoy_62 ` |

*chevron_right*

*filter_none*

## C#

`// C# program for the above problem. ` `using` `System; ` `class` `GFG{ ` ` ` `// Function to find the maximum ` `// possible value of M ` `static` `int` `getM(` `int` `n) ` `{ ` ` ` ` ` `// Edge case ` ` ` `if` `(n == 1) ` ` ` `return` `-1; ` ` ` ` ` `// M = N - 2 is maximum ` ` ` `// possible value ` ` ` `else` ` ` `return` `n - 2; ` `} ` ` ` `// Driver code ` `static` `void` `Main() ` `{ ` ` ` `int` `n = 10; ` ` ` ` ` `Console.Write(getM(n)); ` `} ` `} ` ` ` `// This code is contributed by divyeshrabadiya07 ` |

*chevron_right*

*filter_none*

**Output:**

8

**Time Complexity:** O(1)

**Auxiliary Space:** O(1)

## Recommended Posts:

- Find the largest number smaller than integer N with maximum number of set bits
- Number of times the largest perfect square number can be subtracted from N
- Largest number dividing maximum number of elements in the array
- Find the largest good number in the divisors of given number N
- Largest even number that can be formed by any number of swaps
- Largest number less than N whose each digit is prime number
- Largest number less than X having at most K set bits
- Largest number with prime digits
- Largest number divisible by 90 that can be made using 0 and 5
- Largest number in [2, 3, .. n] which is co-prime with numbers in [2, 3, .. m]
- Largest N digit number in Base B
- Largest and smallest digit of a number
- Largest number with the given set of N digits that is divisible by 2, 3 and 5
- Largest number possible after removal of K digits
- Largest number N which can be reduced to 0 in K steps
- Largest value of x such that ax
^{x}is N-digit number of base b - Largest K digit number divisible by X
- Largest number with one swap allowed
- Largest number not greater than N all the digits of which are odd
- Largest palindromic number in an 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.