# Minimum value of N such that xor from 1 to N is equal to K

Given a value K which is the XOR of all the values from 1 to N, the task is to find the minimum value of N such that XOR from 1 to N is equal to K.

Examples:

```Input: K = 7
Output: 6
1 ^ 2 ^ 3 ^ 4 ^ 5 ^ 6 = 7

Input: K = 10
Output: Not Possible
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach: This problem is similar to the Calculate XOR from 1 to n. Below are the conditions to be checked:

1. If k = 0, then N = 3.
2. If k = 1, then N = 1.
3. If k % 4 = 0, then N = k.
4. If k % 4 = 3, then N = k-1.

Below is the implementation of above approach:

## C++

 `// C++ implementation of above approach ` `#include ` `using` `namespace` `std; ` ` `  `// Function to find the value of N ` `int` `findN(``int` `k) ` `{ ` `     `  `    ``// variable to store the result ` `    ``int` `ans; ` ` `  `    ``// handling case for '0' ` `    ``if` `(k == 0) ` `        ``ans = 3; ` ` `  `    ``// handling case for '1' ` `    ``if` `(k == 1) ` `        ``ans = 1; ` ` `  `    ``// when number is completely divided by ` `    ``// 4 then minimum 'x' will be 'k' ` `    ``else` `if` `(k % 4 == 0) ` `        ``ans = k; ` ` `  `    ``// when number divided by 4 gives 3 as ` `    ``// remainder then minimum 'x' will be 'k-1' ` `    ``else` `if` `(k % 4 == 3) ` `        ``ans = k - 1; ` ` `  `    ``// else it is not possible to get ` `    ``// k for any value of x ` `    ``else` `        ``ans = -1; ` ` `  `    ``return` `ans; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``// let the given number be 7 ` `    ``int` `k = 7; ` ` `  `    ``int` `res = findN(k); ` `    ``if` `(res == -1) ` `        ``cout << ``"Not possible"``; ` `    ``else` `        ``cout << res; ` ` `  `    ``return` `0; ` `} `

## Java

 `// Java implementation of ` `// above approach ` `import` `java.io.*; ` ` `  `class` `GFG ` `{ ` ` `  `// Function to find the  ` `// value of N ` `static` `int` `findN(``int` `k) ` `{ ` `     `  `    ``// variable to store  ` `    ``// the result ` `    ``int` `ans; ` ` `  `    ``// handling case for '0' ` `    ``if` `(k == ``0``) ` `        ``ans = ``3``; ` ` `  `    ``// handling case for '1' ` `    ``if` `(k == ``1``) ` `        ``ans = ``1``; ` ` `  `    ``// when number is completely  ` `    ``// divided by 4 then minimum ` `    ``// 'x' will be 'k' ` `    ``else` `if` `(k % ``4` `== ``0``) ` `        ``ans = k; ` ` `  `    ``// when number divided by 4  ` `    ``// gives 3 as remainder then ` `    ``// minimum 'x' will be 'k-1' ` `    ``else` `if` `(k % ``4` `== ``3``) ` `        ``ans = k - ``1``; ` ` `  `    ``// else it is not possible to ` `    ``// get k for any value of x ` `    ``else` `        ``ans = -``1``; ` ` `  `    ``return` `ans; ` `} ` ` `  `// Driver code ` `public` `static` `void` `main (String[] args)  ` `{ ` `    ``// let the given number be 7 ` `    ``int` `k = ``7``; ` `     `  `    ``int` `res = findN(k); ` `    ``if` `(res == -``1``) ` `        ``System.out.println(``"Not possible"``); ` `    ``else` `        ``System.out.println(res); ` `} ` `} ` ` `  `// This code is contributed ` `// by inder_verma `

## Python3

 `# Python3 implementation of ` `# above approach  ` ` `  `# Function to find the value of N  ` `def` `findN(k) : ` ` `  `    ``# handling case for '0'  ` `    ``if` `(k ``=``=` `0``) : ` `        ``ans ``=` `3` ` `  `    ``# handling case for '1'  ` `    ``if` `(k ``=``=` `1``) : ` `        ``ans ``=` `1` ` `  `    ``# when number is completely  ` `    ``# divided by 4 then minimum ` `    ``# 'x' will be 'k'  ` `    ``elif` `(k ``%` `4` `=``=` `0``) : ` `        ``ans ``=` `k  ` ` `  `    ``# when number divided by 4  ` `    ``# gives 3 as remainder then ` `    ``# minimum 'x' will be 'k-1'  ` `    ``elif` `(k ``%` `4` `=``=` `3``) : ` `        ``ans ``=` `k ``-` `1` ` `  `    ``# else it is not possible to   ` `    ``# get k for any value of x  ` `    ``else``: ` `        ``ans ``=` `-``1` ` `  `    ``return` `ans  ` ` `  `# Driver code  ` ` `  `# let the given number be 7  ` `k ``=` `7` ` `  `res ``=` `findN(k)  ` `if` `(res ``=``=` `-``1``):  ` `    ``print``(``"Not possible"``)  ` `else``: ` `    ``print``(res) ` ` `  `# This code is contributed ` `# by Smitha `

## C#

 `// C# implementation of ` `// above approach ` `using` `System; ` ` `  `class` `GFG ` `{ ` ` `  `// Function to find the  ` `// value of N ` `static` `int` `findN(``int` `k) ` `{ ` `     `  `    ``// variable to store  ` `    ``// the result ` `    ``int` `ans; ` ` `  `    ``// handling case for '0' ` `    ``if` `(k == 0) ` `        ``ans = 3; ` ` `  `    ``// handling case for '1' ` `    ``if` `(k == 1) ` `        ``ans = 1; ` ` `  `    ``// when number is completely  ` `    ``// divided by 4 then minimum ` `    ``// 'x' will be 'k' ` `    ``else` `if` `(k % 4 == 0) ` `        ``ans = k; ` ` `  `    ``// when number divided by 4  ` `    ``// gives 3 as remainder then ` `    ``// minimum 'x' will be 'k-1' ` `    ``else` `if` `(k % 4 == 3) ` `        ``ans = k - 1; ` ` `  `    ``// else it is not possible to ` `    ``// get k for any value of x ` `    ``else` `        ``ans = -1; ` ` `  `    ``return` `ans; ` `} ` ` `  `// Driver code ` `public` `static` `void` `Main ()  ` `{ ` `    ``// let the given number be 7 ` `    ``int` `k = 7; ` `     `  `    ``int` `res = findN(k); ` `    ``if` `(res == -1) ` `        ``Console.WriteLine(``"Not possible"``); ` `    ``else` `        ``Console.WriteLine(res); ` `} ` `} ` ` `  `// This code is contributed ` `// by inder_verma `

## PHP

 ` `

Output:

```6
```

How does this work?
When we do XOR of numbers, we get 0 as XOR value just before a multiple of 4. This keeps repeating before every multiple of 4.

```Number Binary-Repr  XOR-from-1-to-n
1         1           
2        10           
3        11             <----- We get a 0
4       100             <----- Equals to n
5       101           
6       110           
7       111             <----- We get 0
8      1000             <----- Equals to n
9      1001           
10     1010           
11     1011            <------ We get 0
12     1100            <------ Equals to n
```

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

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.