# Find the value of N XOR’ed to itself K times

Given two integer N and K, the task is to find the value of N XOR N XOR N XOR … XOR N (K times).

Examples:

Input: N = 123, K = 3
Output: 123
(123 ^ 123 ^ 123) = 123

Input: N = 123, K = 2
Output: 0
(123 ^ 123) = 0

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

Naive approach: Simply run a for loop and xor N, K times.

Below is the implementation of the above approach:

## C++

 `// C++ implementation of the approach ` `#include ` `using` `namespace` `std; ` ` `  `// Function to return n ^ n ^ ... k times ` `int` `xorK(``int` `n, ``int` `k) ` `{ ` ` `  `    ``// Find the result ` `    ``int` `res = n; ` `    ``for` `(``int` `i = 1; i < k; i++) ` `        ``res = (res ^ n); ` `    ``return` `n; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `n = 123, k = 3; ` ` `  `    ``cout << xorK(n, k); ` ` `  `    ``return` `0; ` `} `

## Java

 `// Java implementation of the approach ` `class` `GFG ` `{ ` ` `  `// Function to return n ^ n ^ ... k times ` `static` `int` `xorK(``int` `n, ``int` `k) ` `{ ` ` `  `    ``// Find the result ` `    ``int` `res = n; ` `    ``for` `(``int` `i = ``1``; i < k; i++) ` `        ``res = (res ^ n); ` `    ``return` `n; ` `} ` ` `  `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` `    ``int` `n = ``123``, k = ``3``; ` ` `  `    ``System.out.print(xorK(n, k)); ` `} ` `} ` ` `  `// This code is contributed by 29AjayKumar `

## Python3

 `# Python3 implementation of the approach ` ` `  `# Function to return n ^ n ^ ... k times ` `def` `xorK(n, k): ` ` `  `    ``# Find the result ` `    ``res ``=` `n ` `    ``for` `i ``in` `range``(``1``, k): ` `        ``res ``=` `(res ^ n) ` `    ``return` `n ` ` `  `# Driver code ` `n ``=` `123` `k ``=` `3` ` `  `print``(xorK(n, k)) ` ` `  `# This code is contributed by Mohit Kumar `

## C#

 `// C# implementation of the approach ` `using` `System; ` ` `  `class` `GFG ` `{ ` `     `  `// Function to return n ^ n ^ ... k times ` `static` `int` `xorK(``int` `n, ``int` `k) ` `{ ` ` `  `    ``// Find the result ` `    ``int` `res = n; ` `    ``for` `(``int` `i = 1; i < k; i++) ` `        ``res = (res ^ n); ` `    ``return` `n; ` `} ` ` `  `// Driver code ` `static` `public` `void` `Main () ` `{ ` `    ``int` `n = 123, k = 3; ` `     `  `    ``Console.Write(xorK(n, k)); ` `} ` `} ` ` `  `// This code is contributed by ajit. `

Output:

```123
```

Time Complexity: O(K)
Space Complexity: O(1)

Efficient approach: From the properties X XOR X = 0 and X ^ 0 = X, it can be observed that when K is odd then the answer will be N itself else the answer will be 0.

Below is the implementation of the above approach:

## C++

 `// C++ implementation of the approach ` `#include ` `using` `namespace` `std; ` ` `  `// Function to return n ^ n ^ ... k times ` `int` `xorK(``int` `n, ``int` `k) ` `{ ` ` `  `    ``// If k is odd the answer is ` `    ``// the number itself ` `    ``if` `(k % 2 == 1) ` `        ``return` `n; ` ` `  `    ``// Else the answer is 0 ` `    ``return` `0; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `n = 123, k = 3; ` ` `  `    ``cout << xorK(n, k); ` ` `  `    ``return` `0; ` `} `

## Java

 `// Java implementation of the approach ` `class` `GFG ` `{ ` ` `  `// Function to return n ^ n ^ ... k times ` `static` `int` `xorK(``int` `n, ``int` `k) ` `{ ` ` `  `    ``// If k is odd the answer is ` `    ``// the number itself ` `    ``if` `(k % ``2` `== ``1``) ` `        ``return` `n; ` ` `  `    ``// Else the answer is 0 ` `    ``return` `0``; ` `} ` ` `  `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` `    ``int` `n = ``123``, k = ``3``; ` ` `  `    ``System.out.print(xorK(n, k)); ` `} ` `} ` ` `  `// This code is contributed by PrinciRaj1992 `

## Python3

 `# Python implementation of the approach  ` ` `  `# Function to return n ^ n ^ ... k times  ` `def` `xorK(n, k): ` `     `  `    ``# If k is odd the answer is  ` `    ``# the number itself  ` `    ``if` `(k ``%` `2` `=``=` `1``): ` `        ``return` `n ` ` `  `    ``# Else the answer is 0  ` `    ``return` `0` ` `  `# Driver code  ` `n ``=` `123` `k ``=` `3` ` `  `print``(xorK(n, k)) ` ` `  `# This code is contributed by Sanjit_Prasad `

## C#

 `// C# implementation of the approach ` `using` `System; ` ` `  `class` `GFG ` `{ ` ` `  `// Function to return n ^ n ^ ... k times ` `static` `int` `xorK(``int` `n, ``int` `k) ` `{ ` ` `  `    ``// If k is odd the answer is ` `    ``// the number itself ` `    ``if` `(k % 2 == 1) ` `        ``return` `n; ` ` `  `    ``// Else the answer is 0 ` `    ``return` `0; ` `} ` ` `  `// Driver code ` `public` `static` `void` `Main(String[] args) ` `{ ` `    ``int` `n = 123, k = 3; ` ` `  `    ``Console.Write(xorK(n, k)); ` `} ` `} ` ` `  `// This code is contributed by 29AjayKumar `

Output:

```123
```

Time Complexity: O(1)
Space Complexity: O(1)

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.

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.