Count even and odd Bitwise XORs of consecutive numbers in a range [L, R] starting from L
Last Updated :
02 Feb, 2022
Given two integers L and R, the task is to find the count of even and odd Bitwise XOR values of consecutive numbers from the range [L, R] starting from L.
Examples:
Input: L = 2, R = 7
Output: Even = 3, Odd = 3
Explanation: Taking bitwise XOR of continuous numbers:
2
2 ^ 3 = 1
2 ^ 3 ^ 4 = 5
2 ^ 3 ^ 4 ^ 5 = 0
2 ^ 3 ^ 4 ^ 5 ^ 6 = 6
2 ^ 3 ^ 4 ^ 5 ^ 6 ^ 7 = 1
Therefore, Bitwise XOR values obtained are {2, 1, 5, 0, 6, 1}.
Therefore, count of even XOR values is 3 and odd XOR values is 3.
Input: L = 1, R = 7
Output: Even = 3, Odd = 4
Naive Approach: The simplest approach is to traverse all numbers in the range [L, R] and perform Bitwise XOR of consecutive numbers starting from L. Finally, count the number of even and odd values Bitwise XOR values obtained.
Time Complexity: O(R – L)
Auxiliary Space: O(1)
Efficient Approach: To optimize the above approach, the idea is based on the following observation that the Bitwise XOR from 1 to N can be calculated in constant time:
- Find the remainder of N when divide by 4.
- If remainder obtained is 0, then XOR will be equal to N.
- If remainder obtained is 1, then XOR will be equal to 1.
- If remainder obtained is 2, then XOR will be equal to N + 1.
- If remainder obtained is 3, then XOR obtained will be 0.
From the above observation, it can be concluded that the even XOR values are either 0 or multiples of 4. Follow the steps below to solve the problem:
- Store the number of elements in the range [L, R] in a variable, say X.
- Store the count of even XOR values by dividing X by 4 and multiplying by 2 in a variable, say Even.
- If L is odd and X % 4 is equal to 3, then increment Even by 1.
- Otherwise, if L is even and X % 4 is greater than 0, then increment Even by 1.
- Store the count of odd XOR values in a variable Odd = X – Even.
- After completing the above steps, print the value of Even and Odd as the result.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
void countEvenOdd( int L, int R)
{
int range = R - L + 1;
int even = (range / 4) * 2;
if ((L & 1) && (range % 4 == 3)) {
even++;
}
else if (!(L & 1) && (range % 4)) {
even++;
}
cout << "Even = " << even
<< ", Odd = " << range - even;
}
int main()
{
int L = 2, R = 7;
countEvenOdd(L, R);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG
{
static void countEvenOdd( int L, int R)
{
int range = R - L + 1 ;
int even = (range / 4 ) * 2 ;
if ((L & 1 ) != 0 && (range % 4 == 3 ))
{
even++;
}
else if ((L & 1 ) == 0 && (range % 4 != 0 ))
{
even++;
}
System.out.print( "Even = " + even +
", Odd = " + (range - even));
}
public static void main(String[] args)
{
int L = 2 , R = 7 ;
countEvenOdd(L, R);
}
}
|
Python3
def countEvenOdd(L, R):
range = R - L + 1 ;
even = ( range / / 4 ) * 2 ;
if ((L & 1 ) ! = 0 and ( range % 4 = = 3 )):
even + = 1 ;
elif ((L & 1 ) = = 0 and ( range % 4 ! = 0 )):
even + = 1 ;
print ( "Even = " , even ,\
", Odd = " , ( range - even));
if __name__ = = '__main__' :
L = 2 ; R = 7 ;
countEvenOdd(L, R);
|
C#
using System;
class GFG
{
static void countEvenOdd( int L, int R)
{
int range = R - L + 1;
int even = (range / 4) * 2;
if ((L & 1) != 0 && (range % 4 == 3))
{
even++;
}
else if ((L & 1) == 0 && (range % 4 != 0))
{
even++;
}
Console.Write( "Even = " + even +
", Odd = " + (range - even));
}
static void Main()
{
int L = 2, R = 7;
countEvenOdd(L, R);
}
}
|
Javascript
<script>
function countEvenOdd(L, R)
{
let range = R - L + 1;
let even = parseInt(range / 4) * 2;
if ((L & 1) && (range % 4 == 3)) {
even++;
}
else if (!(L & 1) && (range % 4)) {
even++;
}
document.write( "Even = " + even
+ ", Odd = " + (range - even));
}
let L = 2, R = 7;
countEvenOdd(L, R);
</script>
|
Output:
Even = 3, Odd = 3
Time Complexity: O(1)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...