Calculate XOR from 1 to n.
Last Updated :
11 Sep, 2023
Given a number n, the task is to find the XOR from 1 to n.
Examples :
Input : n = 6
Output : 7
// 1 ^ 2 ^ 3 ^ 4 ^ 5 ^ 6 = 7
Input : n = 7
Output : 0
// 1 ^ 2 ^ 3 ^ 4 ^ 5 ^ 6 ^ 7 = 0
Method 1 (Naive Approach):
1- Initialize the result as 0.
1- Traverse all numbers from 1 to n.
2- Do XOR of numbers one by one with results.
3- At the end, return the result.
C++
#include <bits/stdc++.h>
using namespace std;
int computeXOR( int n)
{
if (n == 0)
return 0;
int uni = 0;
for ( int i = 1; i <= n; i++) {
uni = uni ^ i;
}
return uni;
}
int main()
{
int n = 7;
int result = computeXOR(n);
cout << result << endl;
return 0;
}
|
Java
import java.io.*;
public class GFG {
public static void main(String[] args) {
int n = 7 ;
int ans = computeXor(n);
System.out.println(ans);
}
static int computeXor( int n){
if (n == 0 ) return 0 ;
int uni = 0 ;
for ( int i = 1 ; i <= n; i++) {
uni = uni^i;
}
return uni;
}
}
|
Python3
def computeXOR(n):
uni = 0
if n = = 0 :
return 0
for i in range ( 1 ,n + 1 ):
uni = uni ^ i
return uni
n = 7
ans = computeXOR(n)
print (ans)
|
C#
using System;
public class GFG {
static int computeXor( int n)
{
if (n == 0)
return 0;
int uni = 0;
for ( int i = 1; i <= n; i++) {
uni = uni ^ i;
}
return uni;
}
public static void Main( string [] args)
{
int n = 7;
int ans = computeXor(n);
Console.WriteLine(ans);
}
}
|
Javascript
function computeXor(n){
if (n == 0)
return 0;
var uni = 0;
for ( var i = 1; i <= n; i++)
uni = uni^i;
return uni;
}
var n = 7;
var ans = computeXor(n);
console.log(ans);
|
Time Complexity: O(n)
Auxiliary Space: O(1)
Method 2 (Efficient method) :
1- Find the remainder of n by moduling it with 4.
2- If rem = 0, then XOR will be same as n.
3- If rem = 1, then XOR will be 1.
4- If rem = 2, then XOR will be n+1.
5- If rem = 3 ,then XOR will be 0.
C++
#include<bits/stdc++.h>
using namespace std;
int computeXOR( int n)
{
if (n % 4 == 0)
return n;
if (n % 4 == 1)
return 1;
if (n % 4 == 2)
return n + 1;
return 0;
}
int main()
{
int n = 5;
cout<<computeXOR(n);
}
|
Java
class GFG
{
static int computeXOR( int n)
{
if (n % 4 == 0 )
return n;
if (n % 4 == 1 )
return 1 ;
if (n % 4 == 2 )
return n + 1 ;
return 0 ;
}
public static void main (String[] args)
{
int n = 5 ;
System.out.println(computeXOR(n));
}
}
|
Python 3
def computeXOR(n) :
if n % 4 = = 0 :
return n
if n % 4 = = 1 :
return 1
if n % 4 = = 2 :
return n + 1
return 0
if __name__ = = "__main__" :
n = 5
print (computeXOR(n))
|
C#
using System;
class GFG
{
static int computeXOR( int n)
{
if (n % 4 == 0)
return n;
if (n % 4 == 1)
return 1;
if (n % 4 == 2)
return n + 1;
return 0;
}
static public void Main ()
{
int n = 5;
Console.WriteLine(computeXOR(n));
}
}
|
PHP
<?php
function computeXOR( $n )
{
switch ( $n & 3)
{
case 0: return $n ;
case 1: return 1;
case 2: return $n + 1;
case 3: return 0;
}
}
$n = 5;
echo computeXOR( $n );
?>
|
Javascript
<script>
function computeXOR(n)
{
if (n % 4 == 0)
return n;
if (n % 4 == 1)
return 1;
if (n % 4 == 2)
return n + 1;
if (n % 4 == 3)
return 0;
}
let n = 5;
document.write(computeXOR(n));
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
How does this work?
When we do XOR of numbers, we get 0 as the XOR value just before a multiple of 4. This keeps repeating before every multiple of 4.
Number Binary-Repr XOR-from-1-to-n
1 1 [0001]
2 10 [0011]
3 11 [0000] <----- We get a 0
4 100 [0100] <----- Equals to n
5 101 [0001]
6 110 [0111]
7 111 [0000] <----- We get 0
8 1000 [1000] <----- Equals to n
9 1001 [0001]
10 1010 [1011]
11 1011 [0000] <------ We get 0
12 1100 [1100] <------ Equals to n
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...