# Check whether XOR of all numbers in a given range is even or odd

Given a range [ L, R ], the task is to find if value of XOR of all natural numbers in range L to R ( both inclusive ) is even or odd. Print ‘Even’ if XOR of all numbers in the range is even, otherwise print odd.

**Examples:**

Input:L = 1, R= 10Output:OddInput:L= 5, R=15Output:Even

A ** Simple Solution ** is to calculate XOR of all numbers in range [L, R] and then check if resultant XOR value is even or odd.

Time Complexity of this approach will be O(n).

An ** Efficient Solution ** is based on the below fact:

odd ^ odd = even odd ^ even = odd even ^ odd = odd even ^ even = even

XOR of all even numbers will be even ( irrespective of size of range ) and if count of odd numbers is odd then the final XOR will be odd and if even then final XOR will be even.

Now, it can be concluded that,

- If the count of Odd Numbers is even,

XOR of all odd numbers = Even

XOR of all even numbers = Even

Final XOR = Even ^ Even = Even

- If the count of Odd Numbers is Odd,

XOR of all odd numbers = Odd

XOR of all even numbers = Even

Final XOR = Odd ^ Even = Odd

So, all we have to do is to count odd numbers in range L to R.

**Approach :**

- Count the odd numbers in the range [ L, R ].
- Check if count of odd numbers is even or odd.
- Print ‘Even’ if count is even otherwise print ‘Odd’ .

Below is the implementation of above approach:

## C++

`// C++ program to check if XOR of ` `// all numbers in range [L, R] ` `// is Even or odd ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to check if XOR of all numbers ` `// in range [L, R] is Even or Odd ` ` ` `string isEvenOrOdd(` `int` `L, ` `int` `R) ` `{ ` ` ` `// Count odd Numbers in range [L, R] ` ` ` `int` `oddCount = (R - L) / 2; ` ` ` ` ` `if` `(R % 2 == 1 || L % 2 == 1) ` ` ` `oddCount++; ` ` ` ` ` `// Check if count of odd Numbers ` ` ` `// is even or odd ` ` ` ` ` `if` `(oddCount % 2 == 0) ` ` ` `return` `"Even"` `; ` ` ` `else` ` ` `return` `"Odd"` `; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` ` ` `int` `L = 5, R = 15; ` ` ` ` ` `cout << isEvenOrOdd(L, R); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to check if XOR of ` `// all numbers in range [L, R] ` `// is Even or odd ` ` ` `class` `GFG { ` ` ` ` ` `// Function to check if XOR of all numbers ` ` ` `// in range [L, R] is Even or Odd ` ` ` ` ` `static` `String isEvenOrOdd(` `int` `L, ` `int` `R) ` ` ` `{ ` ` ` `// Count odd Numbers in range [L, R] ` ` ` `int` `oddCount = (R - L) / ` `2` `; ` ` ` ` ` `if` `(R % ` `2` `== ` `1` `|| L % ` `2` `== ` `1` `) ` ` ` `oddCount++; ` ` ` ` ` `// Check if count of odd Numbers ` ` ` `// is even or odd ` ` ` ` ` `if` `(oddCount % ` `2` `== ` `0` `) ` ` ` `return` `"Even"` `; ` ` ` `else` ` ` `return` `"Odd"` `; ` ` ` `} ` ` ` ` ` `// Driver Code ` ` ` `public` `static` `void` `main(String[] args) ` ` ` `{ ` ` ` ` ` `int` `L = ` `5` `, R = ` `15` `; ` ` ` ` ` `System.out.println(isEvenOrOdd(L, R)); ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

## C#

`// C# program to check if XOR of ` `// all numbers in range [L, R] ` `// is Even or odd ` ` ` `using` `System; ` `class` `GFG { ` ` ` ` ` `// Function to check if XOR of all numbers ` ` ` `// in range [L, R] is Even or Odd ` ` ` ` ` `static` `string` `isEvenOrOdd(` `int` `L, ` `int` `R) ` ` ` `{ ` ` ` `// Count odd Numbers in range [L, R] ` ` ` `int` `oddCount = (R - L) / 2; ` ` ` ` ` `if` `(R % 2 == 1 || L % 2 == 1) ` ` ` `oddCount++; ` ` ` ` ` `// Check if count of odd Numbers ` ` ` `// is even or odd ` ` ` ` ` `if` `(oddCount % 2 == 0) ` ` ` `return` `"Even"` `; ` ` ` `else` ` ` `return` `"Odd"` `; ` ` ` `} ` ` ` ` ` `// Driver Code ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` ` ` `int` `L = 5, R = 15; ` ` ` ` ` `Console.WriteLine(isEvenOrOdd(L, R)); ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to check if XOR of ` `# all numbers in range [L, R] ` `# is Even or odd ` ` ` ` ` `# Function to check if XOR of all numbers ` `# in range [L, R] is Even or Odd ` ` ` `def` `isEvenOrOdd( L, R ): ` ` ` ` ` `# Count odd Numbers in range [L, R] ` ` ` `oddCount ` `=` `(R ` `-` `L )` `/` `2` ` ` ` ` `if` `( R ` `%` `2` `=` `=` `1` `or` `L ` `%` `2` `=` `=` `1` `): ` ` ` `oddCount ` `=` `oddCount ` `+` `1` ` ` ` ` ` ` `# Check if count of odd Numbers ` ` ` `# is even or odd ` ` ` ` ` `if` `(oddCount ` `%` `2` `=` `=` `0` `): ` ` ` `return` `"Even"` ` ` `else` `: ` ` ` `return` `"Odd"` ` ` ` ` ` ` `# Driver Code ` ` ` `L ` `=` `5` `R ` `=` `15` ` ` `print` `(isEvenOrOdd(L, R)); ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to check if XOR of all ` `// numbers in range [L, R] is Even or odd ` ` ` `// Function to check if XOR of all numbers ` `// in range [L, R] is Even or Odd ` `function` `isEvenOrOdd(` `$L` `, ` `$R` `) ` `{ ` ` ` `// Count odd Numbers in range [L, R] ` ` ` `$oddCount` `= ` `floor` `((` `$R` `- ` `$L` `) / 2); ` ` ` ` ` `if` `(` `$R` `% 2 == 1 || ` `$L` `% 2 == 1) ` ` ` `$oddCount` `++; ` ` ` ` ` `// Check if count of odd Numbers ` ` ` `// is even or odd ` ` ` `if` `(` `$oddCount` `% 2 == 0) ` ` ` `return` `"Even"` `; ` ` ` `else` ` ` `return` `"Odd"` `; ` `} ` ` ` `// Driver Code ` `$L` `= 5; ` `$R` `= 15; ` ` ` `echo` `isEvenOrOdd(` `$L` `, ` `$R` `); ` ` ` `// This code is contributed by Ryuga ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

Even

## Recommended Posts:

- Check if bits in range L to R of two numbers are complement of each other or not
- Sum of all even numbers in range L and R
- Prime numbers in a given range using STL | Set 2
- Sum of Fibonacci Numbers in a range
- Sum of all numbers divisible by 6 in a given range
- Count Odd and Even numbers in a range from L to R
- Sum of all natural numbers in range L to R
- Sum of all odd natural numbers in range L and R
- Sum of all the prime numbers in a given range
- Sum of all even factors of numbers in the range [l, r]
- Find XOR of numbers from the range [L, R]
- Count of numbers having only 1 set bit in the range [0, n]
- Sum of all odd factors of numbers in the range [l, r]
- Check whether all the bits are unset in the given range
- Check if there is any pair in a given range with GCD is divisible by k

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.