int calc(int n){
if(n%4 == 1)return 1;
else if(n%4 == 2)return n+1;
else if(n%4 == 3)return 0;
else return n;
}
int findXOR(int L, int R){
int n1 = calc(L-1);
int n2 = calc(R);
return n1^n2;
}
Problem of the day
You are given two numbers 'L' and 'R'.
Find the XOR of the elements in the range [L, R].
For 'L' = 1 and ‘R’ = 5.
The answer is 1.
The first line contains two integers 'L' and ‘R’, representing the lower and upper limits of the range respectively.
Output Format:
Return the value as explained in the statement.
Note:-
You don’t need to print anything. Just implement the given function.
3 5
2
'L' = 3 and ‘R’ = 5
Answer is 2. Value of 3^4^5 is 2.
1 3
0
1 <= ‘L’ <= ‘R’ <= 10^9
Time Limit: 1 sec
Iterate over the whole range ‘L’ to ‘R’.
Initialise ‘ans’ to 0 and then iterate via ‘i’ from ‘L’ to ‘R’ and update ‘ans’ to XOR(‘ans’, ‘i’).
Algorithm:
O(R-L), where 'R' and ‘L’ are upper and lower limits of range.
We are iterating via ‘i’ from ‘L’ to ‘R’. Hence the overall time complexity of this solution is O(R-L).
O(1).
We are not utilising any extra space here, hence the O(1) space complexity.
Interview problems
C++ xor
int calc(int n){
if(n%4 == 1)return 1;
else if(n%4 == 2)return n+1;
else if(n%4 == 3)return 0;
else return n;
}
int findXOR(int L, int R){
int n1 = calc(L-1);
int n2 = calc(R);
return n1^n2;
}
Interview problems
What do you think about this solution
public class Solution {
public static int findXOR(int L, int R){
return (XOR(L-1) ^ XOR(R));
}
public static int XOR(int n){
if(n%4==0) return n;
if(n%4==1) return 1;
if(n%4==2) return n+1;
return 0;
}
}
Interview problems
Very easy to understand o(1) time complexity
int getXOR(int n){
if(n%4==1) return 1;
if(n%4==2) return n+1;
if(n%4==3)return 0;
if(n%4==0) return n;
}
int findXOR(int L, int R){
// Write your code here.
return getXOR(L-1)^getXOR(R);
}
Interview problems
easy cpp
int XorTillN(int N){
if(N%4==1) return 1;
else if(N%4==2) return N+1;
else if(N%4==3) return 0;
else if(N%4==0) return N;
}
int findXOR(int L, int R){
// Write your code here.
return XorTillN(R) ^ XorTillN(L-1);
}
Interview problems
c++ solutions && complete approach and expalination OF L to R XOR
mathematical properties of XOR
Interview problems
O(1) Time Complexity || Space complexity O(1) || Java solution
Approach:
Xor of number from 1 to a will be
Xor a
0 if a%4==0
1 if a%4==1
a+1 if a%4==2
0 if a%4==3
.
.
.
Java Code:
public class Solution {
public static int findXOR(int L, int R){
int a=L%4;
if(a==0){a=L;}
else if(a==1){a=1;}
else if(a==2){a=L+1;}
else if(a==3){a=0;}
int b=R%4;
if(b==0){b=R;}
else if(b==1){b=1;}
else if(b==2){b=R+1;}
else{b=0;}
return (a^b)^L;
}
}
Interview problems
Simple short c++
int helper(int n)
{
int mod=n%4;
switch(mod)
{
case 0: return n;
case 1: return 1;
case 2: return n+1;
case 3: return 0;
}
}
int findXOR(int L, int R){
// Write your code here.
return helper(L-1)^helper(R);
}Interview problems
O(N) Time complexity solution , passing 39 cases out of 40 (39/40) but easy to understand than that modulo intuition
int findXOR(int L, int R){
// Write your code here.
int ans=L;
for(int i=L+1;i<=R;i++){
ans=ans^i;
}
return ans;
}
Interview problems
O(1) C++ code using XOR operation
int XOR(int 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;
}
int findXOR(int L, int R){
int ans = XOR(R)^XOR(L-1);
return ans;
}
//We observe a pattern from 1->n when modulus with 4
// N = 1 1 <-| 1%4 = 1 -> 1
// N = 2 1^2 3 | 2%4 = 2 -> 2+1
// N = 3 3^3 0 | 3%4 = 3 -> 0
// N = 4 4^0 4 <-| 4%4 = 0 -> 4
// N = 5 5^4 1 <-| 5%4 = 1 -> 1
// N = 6 5^6 7 | 6%4 = 2 -> 6+1
// N = 7 7^7 0 | 7%4 = 3 -> 0
// N = 8 8^0 8 <-| 8%4 = 0 -> 8
// for L=3 and R = 5
// XOR(R)^XOR(L-1) = (1^2^3^4^5)^(1^3) => (3^4^5) =>ANSInterview problems
Xor in O(1)
int makeXor(int 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;
}
}
int findXOR(int L, int R){
int ans = makeXor(R) ^ makeXor(L-1);
return ans;
}