XOR of very large Binary Numbers in range [L, R]
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
Input: L = 10, R = 100
Explanation: L = 2 and R = 4 in decimal system.Therefore, the xor will be 2^3^4 = 5(101).
Input: L = 10, R = 10000
Naive Approach: The simplest approach to solve the problem is to find all the binary strings in the range [L, R] and then perform the XOR operation on all the binary strings.
Time Complexity: (R – L +1) *(N) where N is the length of binary string R.
Auxiliary Space: O(1)
Efficient Approach: The above approach can be optimized further by observing that –
- (L ^ (L+1) ^……^(R – 1) ^ R) can be written as (1^2^3 ^…. ^(R-1)^R) ^ (1^2^3 ….. ^(L-2) ^ (L-1)) where ‘^’ denotes the bitwise xor of the elements. So the problem is reduced to find the xor from 1 to n.
- To calculate xor from 1 to n, find the remainder of n by moduling it with 4 and store it in a variable, say rem and there are four possible values of rem–
- If rem =0 then xor = n.
- If rem = 1 then xor = 1.
- If rem = 2 then xor = n+1.
- If rem = 3 then xor = 0.
After observing the above points, follow the steps below to solve the problem:
- Create a function sub that subtracts 1 from a binary string S of size N by performing the steps mentioned below:
- Create a function ad that adds 1 to a binary string S of size N by performing the steps mentioned below:
- Initialize a variable, say carry as 0.
- Iterate in the range [N-1, 0] using the variable i and perform the following steps:
- If S[i] = ‘1’, then modify the value of S[i] as 0 and modify the value of carry as 1.
- If S[i] = ‘0’, then modify the value of S[i] as 1 and modify the value of carry as 0 and terminate the loop.
- If carry =1, then modify the value of S as ‘1’ + S and return the string S.
- Create a function Xor_Finder which return the value of XOR from 1 to S by performing the steps mentioned below:
- Initialize a variable, say val and update the value as S[n] + S[n-1]*2 where n is the length of the string S.
- Now if val = 0, then return string S as the answer.
- If val = 1, then return ‘1’ as the answer.
- If val = 2, then return ad(S) as the answer.
- If val = 3, then return 0 as the answer.
- Create a function final_xor which calculate xor of two binary strings L and R by performing the steps mentioned below:
- Append character ‘0’ to the string L while L.size() != R.size().
- Initialize a string, say ans that will store the value of xor of binary strings L and R.
- Iterate in the range [0, R.size()-1] using the variable i and perform the following steps:
- If L[i] = R[i], then append the character ‘0’ at the end of string ans.
- If L[i] != R[i], then append the character ‘1’ at the end of string ans.
- Return string ans as the xor of the two strings.
- Create a function xorr to calculate xor from L to R by performing the following steps:
- Modify the value of L as sub(L).
- Modify the value of L and R by calling the function xor_finder(L) and xor_finder(R) to calculate xor from 1 to L and from 1 to R respectively.
- Initialize a variable, say ans and update the value of ans by updating the value as final_xor(L, R).
- Return string ans as the answer.
Below is the implementation of the above approach:
Time Complexity: O(N) where N is the length of the string
Auxiliary Space: O(N)