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.
Input: L = 1, R= 10 Output: Odd Input: L= 5, R=15 Output: 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.
- 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:
Time Complexity : O(1)
Auxiliary Space : O(1)
- Count numbers from given range having odd digits at odd places and even digits at even places
- Count of integers in a range which have even number of odd digits and odd number of even digits
- Check if a number has an odd count of odd divisors and even count of even divisors
- Queries to check whether all the elements in the given range occurs even number of times
- Check whether product of 'n' numbers is even or odd
- Check whether a given number is even or odd
- C program to check whether a given number is even or odd
- Check whether given floating point number is even or odd
- Rotate all odd numbers right and all even numbers left in an Array of 1 to N
- Check whether product of digits at even places is divisible by sum of digits at odd place of a number
- Queries to check whether bitwise AND of a subarray is even or odd
- Check whether count of odd and even factors of a number are equal
- Check if the XOR of an array of integers is Even or Odd
- Count numbers in given range such that sum of even digits is greater than sum of odd digits
- Print even and odd numbers in a given range using recursion
- Sum of range in a series of first odd then even natural numbers
- Sum of elements in range L-R where first half and second half is filled with odd and even numbers
- Count Odd and Even numbers in a range from L to R
- Count Numbers in Range with difference between Sum of digits at even and odd positions as Prime
- Count of N-digit Numbers having Sum of even and odd positioned digits divisible by given numbers
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.