Given two integers L and R, the task is to find the XOR of elements of the range [L, R].
Examples:
Input: L = 4, R = 8
Output: 8
Explanation: 4 ^ 5 ^ 6 ^ 7 ^ 8 = 8Input: L = 2, R = 4
Output: 5
Explanation: 2 ^ 3 ^ 4 = 5
Table of Content
[Naive Approach] Simple Loop - O(n) Time and O(1) Space
Initialize answer as zero, Traverse all numbers from L to R and perform XOR of the numbers one by one with the answer.
// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
// Function to return the XOR of elements
// from the range [l, r]
int findXOR(int l, int r)
{
int ans = 0;
for (int i = l; i <= r; i++) {
ans = ans ^ i;
}
return ans;
}
// Driver code
int main()
{
int l = 4, r = 8;
cout << findXOR(l, r);
return 0;
}
// this code is contributed by devendra solunke
/*package whatever //do not write package name here */
import java.io.*;
class GFG {
// Function to return the XOR of elements
// from the range [l, r]
public static int findXOR(int l, int r)
{
int ans = 0;
for (int i = l; i <= r; i++) {
ans = ans ^ i;
}
return ans;
}
// Driver code
public static void main(String[] args)
{
int l = 4;
int r = 8;
System.out.println(findXOR(l, r));
}
}
// this code is contributed by devendra solunke
# Python3 implementation of the approach
from operator import xor
# Function to return the XOR of elements
# from the range [1, n]
def findXOR(l, r):
ans = 0
for i in range(l,r+1):
ans = xor(ans,i)
return ans
# Driver code
l = 4; r = 8;
print(findXOR(l, r));
# This code is contributed by Arpit Jain
// c# implementation of find xor between given range
using System;
public class GFG {
// Function to return the XOR of elements
// from the range [l, r]
static int findXOR(int l, int r)
{
int ans = 0;
for (int i = l; i <= r; i++) {
ans = ans ^ i;
}
return ans;
}
// Driver code
static void Main(String[] args)
{
int l = 4;
int r = 8;
Console.WriteLine(findXOR(l, r));
}
}
// this code is contributed by devendra saunke
// Javascript code to find xor of given range
<script>
var l = 4;
var r = 8;
var ans = 0;
var(int i = l; i <= r; i++)
{
ans = ans ^ i;
}
document.write(ans);
</script>
// this code is contributed by devendra solunke
Output
8
[Efficient Approach] Using XOR Pattern - O(1) Time and O(1) Space
Use the repeating XOR pattern from
1 to nand compute the range XOR as: XOR(L to R) = XOR(1 to R) ^ XOR(1 to L-1).
How does this work? We mainly get the elements from 1 to L-1 appeared twice in x1 ^ x2 and if we do XOR of an element with itself, we get 0 and if we do XOR of 0 with an element x, we get x.
How do we find XOR of 1 to n Find the remainder of n by moduling it with 4.
- If rem = 0, then XOR will be same as n.
- If rem = 1, then XOR will be 1.
- If rem = 2, then XOR will be n+1.
- If rem = 3 ,then XOR will be 0.
Please refer XOR of 1 to n for working of this.
// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
// Function to return the XOR of elements
// from the range [1, n]
int findXOR(int n)
{
int mod = n % 4;
// If n is a multiple of 4
if (mod == 0)
return n;
// If n % 4 gives remainder 1
else if (mod == 1)
return 1;
// If n % 4 gives remainder 2
else if (mod == 2)
return n + 1;
// If n % 4 gives remainder 3
else if (mod == 3)
return 0;
}
// Function to return the XOR of elements
// from the range [l, r]
int findXOR(int l, int r)
{
return (findXOR(l - 1) ^ findXOR(r));
}
// Driver code
int main()
{
int l = 4, r = 8;
cout << findXOR(l, r);
return 0;
}
// Java implementation of the approach
class GFG
{
// Function to return the XOR of elements
// from the range [1, n]
static int findXOR(int n)
{
int mod = n % 4;
// If n is a multiple of 4
if (mod == 0)
return n;
// If n % 4 gives remainder 1
else if (mod == 1)
return 1;
// If n % 4 gives remainder 2
else if (mod == 2)
return n + 1;
// If n % 4 gives remainder 3
else if (mod == 3)
return 0;
return 0;
}
// Function to return the XOR of elements
// from the range [l, r]
static int findXOR(int l, int r)
{
return (findXOR(l - 1) ^ findXOR(r));
}
// Driver code
public static void main(String[] args)
{
int l = 4, r = 8;
System.out.println(findXOR(l, r));
}
}
// This code contributed by Rajput-Ji
# Python3 implementation of the approach
from operator import xor
# Function to return the XOR of elements
# from the range [1, n]
def findXOR(n):
mod = n % 4;
# If n is a multiple of 4
if (mod == 0):
return n;
# If n % 4 gives remainder 1
elif (mod == 1):
return 1;
# If n % 4 gives remainder 2
elif (mod == 2):
return n + 1;
# If n % 4 gives remainder 3
elif (mod == 3):
return 0;
# Function to return the XOR of elements
# from the range [l, r]
def findXORFun(l, r):
return (xor(findXOR(l - 1) , findXOR(r)));
# Driver code
l = 4; r = 8;
print(findXORFun(l, r));
# This code is contributed by PrinciRaj1992
// C# implementation of the approach
using System;
class GFG
{
// Function to return the XOR of elements
// from the range [1, n]
static int findXOR(int n)
{
int mod = n % 4;
// If n is a multiple of 4
if (mod == 0)
return n;
// If n % 4 gives remainder 1
else if (mod == 1)
return 1;
// If n % 4 gives remainder 2
else if (mod == 2)
return n + 1;
// If n % 4 gives remainder 3
else if (mod == 3)
return 0;
return 0;
}
// Function to return the XOR of elements
// from the range [l, r]
static int findXOR(int l, int r)
{
return (findXOR(l - 1) ^ findXOR(r));
}
// Driver code
public static void Main()
{
int l = 4, r = 8;
Console.WriteLine(findXOR(l, r));
}
}
// This code is contributed by AnkitRai01
<script>
// Javascript implementation of the approach
// Function to return the XOR of elements
// from the range [1, n]
function findxOR(n)
{
let mod = n % 4;
// If n is a multiple of 4
if (mod == 0)
return n;
// If n % 4 gives remainder 1
else if (mod == 1)
return 1;
// If n % 4 gives remainder 2
else if (mod == 2)
return n + 1;
// If n % 4 gives remainder 3
else if (mod == 3)
return 0;
}
// Function to return the XOR of elements
// from the range [l, r]
function findXOR(l, r)
{
return (findxOR(l - 1) ^ findxOR(r));
}
let l = 4, r = 8;
document.write(findXOR(l, r));
</script>
Output
8