You are given two integers L and R, your task is to find the XOR of elements of the range [L, R].
Example:
Input: 
L = 4, R = 8 
Output:
8 
Explanation:
4 ^ 5 ^ 6 ^ 7 ^ 8 = 8
Your Task:
Your task is to complete the function findXOR() which takes two integers l and r and returns the XOR of numbers from l to r.
^{Expected Time Complexity: O(1).}
^{Expected Auxiliary Space: O(1).}
Constraints:
1<=l<=r<=10^{9}

Source: geeksforgeeks | Difficulty: EASY

Starter code: #User function Template for python3



class Solution:

    def findXOR(self, l, r):

        # Code here

Solutions:
class Solution:

	def xor(self, num):
		if num % 4 == 0:
			return num
		elif num % 4 == 1:
			return 1
		elif num % 4 == 2:
			return num + 1
		elif num % 4 == 3:
			return 0

	def findXOR(self, l, r):
		x = self.xor(l - 1)
		y = self.xor(r)
		return x ^ y


class Solution:

	def findXOR(self, l, r):

		def find(n):
			k = n % 4
			if k == 0:
				return n
			elif k == 1:
				return 1
			elif k == 2:
				return n + 1
			elif k == 3:
				return 0
		return find(l - 1) ^ find(r)


def computeXOR(n):
	if n % 4 == 0:
		return n
	if n % 4 == 1:
		return 1
	if n % 4 == 2:
		return n + 1
	return 0

class Solution:

	def findXOR(self, l, r):
		return computeXOR(r) ^ computeXOR(l - 1)


class Solution:

	def findXOR(self, l, r):

		def f(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
		return f(r) ^ f(l - 1)


class Solution:

	def findXOR(self, l, r):

		def calculateXor(n):
			remainder = n % 4
			if remainder == 0:
				return n
			elif remainder == 1:
				return 1
			elif remainder == 2:
				return n + 1
			else:
				return 0
		return calculateXor(r) ^ calculateXor(l - 1)


class Solution:

	def findXOR(self, l, r):

		def xorx(n):
			mod = n % 4
			if mod == 0:
				return n
			elif mod == 1:
				return 1
			elif mod == 2:
				return n + 1
			elif mod == 3:
				return 0
		a = xorx(l - 1)
		b = xorx(r)
		return a ^ b


class Solution:

	def XOR(self, x):
		if x % 4 == 0:
			return x
		if x % 4 == 1:
			return 1
		if x % 4 == 2:
			return x + 1
		if x % 4 == 3:
			return 0

	def findXOR(self, l, r):
		l = self.XOR(l - 1)
		r = self.XOR(r)
		return l ^ r


class Solution:

	def xor(self, n):
		if (n + 1) % 4 == 0:
			return 0
		else:
			if (n + 1) % 4 == 1:
				return n
			if (n + 1) % 4 == 2:
				return 1
			if (n + 1) % 4 == 3:
				return n + 1

	def findXOR(self, l, r):
		return self.xor(l - 1) ^ self.xor(r)


class Solution:

	def xor12n(self, n):
		rem = n % 4
		values = {0: n, 1: 1, 2: n + 1, 3: 0}
		return values[rem]

	def findXOR(self, l, r):
		return self.xor12n(l - 1) ^ self.xor12n(r)


from operator import xor

class Solution:

	def findXOR(self, l, r):

		def findXOR(n):
			mod = n % 4
			if mod == 0:
				return n
			elif mod == 1:
				return 1
			elif mod == 2:
				return n + 1
			elif mod == 3:
				return 0
		return findXOR(l - 1) ^ findXOR(r)


class Solution:

	def find(self, x):
		if x % 4 == 0:
			return x
		elif x % 4 == 1:
			return 1
		elif x % 4 == 2:
			return x + 1
		else:
			return 0

	def findXOR(self, l, r):
		return self.find(r) ^ self.find(l - 1)


class Solution:

	def findXOR(self, l, r):

		def xortillnum(num):
			if num % 4 == 0:
				return num
			elif num % 4 == 1:
				return 1
			elif num % 4 == 2:
				return num + 1
			elif num % 4 == 3:
				return 0
		xorR = xortillnum(r)
		xorLminus1 = xortillnum(l - 1)
		return xorR ^ xorLminus1


class Solution:

	def findXOR(self, l, r):
		x1 = 0
		x2 = 0
		if r % 4 == 0:
			x1 = r
		elif r % 4 == 1:
			x1 = 1
		elif r % 4 == 2:
			x1 = r + 1
		elif r % 4 == 3:
			x1 = 0
		l = l - 1
		if l % 4 == 0:
			x2 = l
		elif l % 4 == 1:
			x2 = 1
		elif l % 4 == 2:
			x2 = l + 1
		elif l % 4 == 3:
			x2 = 0
		return x1 ^ x2


class Solution:

	def findXOR_N(self, n):
		mod = n % 4
		if mod == 0:
			return n
		elif mod == 1:
			return 1
		elif mod == 2:
			return n + 1
		else:
			return 0

	def findXOR(self, l, r):
		return self.findXOR_N(l - 1) ^ self.findXOR_N(r)


class Solution:

	def findXOR(self, l, r):

		def x1(m):
			if m % 4 == 0:
				return m
			elif m % 4 == 1:
				return 1
			elif m % 4 == 2:
				return m + 1
			else:
				return 0
		l = l - 1
		x = x1(l)
		y = x1(r)
		return x ^ y


class Solution:

	def findXOR(self, l, r):
		l = l - 1
		xor_l = 0
		xor_r = 0
		if l % 4 == 0:
			xor_l = l
		elif l % 4 == 1:
			xor_l = 1
		elif l % 4 == 2:
			xor_l = l + 1
		elif l % 4 == 3:
			xor_l = 0
		if r % 4 == 0:
			xor_r = r
		elif r % 4 == 1:
			xor_r = 1
		elif r % 4 == 2:
			xor_r = r + 1
		elif r % 4 == 3:
			xor_r = 0
		return xor_r ^ xor_l


class Solution:

	def xor1(self, n):
		arr = [1, n + 1, 0, n]
		return arr[n % 4 - 1]

	def findXOR(self, l, r):
		return self.xor1(r) ^ self.xor1(l - 1)


class Solution:

	def find(self, n):
		mod = 0
		mod = n % 4
		if mod == 0:
			return n
		elif mod == 1:
			return 1
		elif mod == 2:
			return n + 1
		elif mod == 3:
			return 0
		else:
			pass

	def findXOR(self, l, r):
		return self.find(l - 1) ^ self.find(r)


class Solution:

	def findXOR(self, l, r):

		def xor(num):
			if num % 4 == 0:
				return num
			if num % 4 == 1:
				return 1
			if num % 4 == 2:
				return num + 1
			if num % 4 == 3:
				return 0
		return xor(l - 1) ^ xor(r)


class Solution:

	def findXOR(self, l, r):

		def fi(n):
			if n % 4 == 0:
				return n
			elif n % 4 == 1:
				return 1
			elif n % 4 == 2:
				return n + 1
			else:
				return 0
		lx = fi(l - 1)
		rx = fi(r)
		return lx ^ rx


class Solution:

	def findXOR(self, l, r):

		def checkforxorsum(x):
			rem = x % 4
			if rem == 0:
				return x
			elif rem == 1:
				return 1
			elif rem == 2:
				return x + 1
			else:
				return 0
		before = l - 1
		total = checkforxorsum(r)
		bef = checkforxorsum(before)
		return total ^ bef


def fromStart(num):
	if num % 4 == 0:
		return num // 4 * 4
	elif num % 4 == 1:
		return 1
	elif num % 4 == 2:
		return num // 4 * 4 + 3
	elif num % 4 == 3:
		return 0

class Solution:

	def findXOR(self, l, r):
		a = fromStart(l - 1)
		b = fromStart(r)
		return a ^ b


class Solution:

	def xorr(self, i):
		if i % 4 == 0:
			return i
		if i % 4 == 1:
			return 1
		if i % 4 == 2:
			return i + 1
		if i % 4 == 3:
			return 0

	def findXOR(self, l, r):
		return self.xorr(l - 1) ^ self.xorr(r)


class Solution:

	def findXOR(self, l, r):
		if r % 4 == 0:
			a = r
		elif r % 4 == 1:
			a = 1
		elif r % 4 == 2:
			a = r + 1
		else:
			a = 0
		r = l - 1
		b = 0
		if r % 4 == 0:
			b = r
		elif r % 4 == 1:
			b = 1
		elif r % 4 == 2:
			b = r + 1
		else:
			b = 0
		return a ^ b


class Solution:

	def findXOR(self, l, r):

		def f(n):
			y = 0
			if n % 4 == 0:
				y = n
			elif n % 4 == 1:
				y = 1
			elif n % 4 == 2:
				y = n + 1
			else:
				y = 0
			return y
		return f(r) ^ f(l - 1)


class Solution:

	def findXOR(self, l, r):

		def xor(n):
			if n % 4 == 0:
				return n
			elif n % 4 == 1:
				return 1
			elif n % 4 == 2:
				return n + 1
			else:
				return 0
		res = xor(r) ^ xor(l - 1)
		return res


class Solution:

	def solve(self, l, r) -> int:
		z = r % 4
		if not z:
			return r
		if z == 1:
			return 1
		if z == 2:
			return r + 1
		if z == 3:
			return 0

	def findXOR(self, l, r):
		return self.solve(1, l - 1) ^ self.solve(1, r)


class Solution:

	def findXOR(self, l, r):
		if not l % 4:
			l = 0
		elif not (l - 1) % 4:
			l -= 1
		elif not (l + 2) % 4:
			l = 1
		if not (r - 1) % 4:
			r = 1
		elif not (r + 1) % 4:
			r = 0
		elif not (r + 2) % 4:
			r += 1
		return l ^ r


class Solution:

	def findXOR(self, l, r):
		if l == 0:
			return [r, 1, r + 1, 0][r % 4]
		return self.findXOR(0, l - 1) ^ self.findXOR(0, r)


class Solution:

	def GiveXOR(self, n):
		if n % 4 == 0:
			return n
		elif n % 4 == 1:
			return 1
		elif n % 4 == 2:
			return n + 1
		else:
			return 0

	def findXOR(self, l, r):
		XORr = self.GiveXOR(r)
		XORl = self.GiveXOR(l - 1)
		return XORr ^ XORl


class Solution:

	def findXOR(self, l, r):

		def compute(n):
			res = [n, 1, n + 1, 0]
			return res[n % 4]
		return compute(l - 1) ^ compute(r)


class Solution:

	def findXOR(self, l, r):

		def help(n):
			if not n % 4:
				return n
			if n % 4 == 1:
				return 1
			if n % 4 == 2:
				return n + 1
			return 0
		return help(l - 1) ^ help(r)


class Solution:

	def findXOR(self, l, r):

		def xor(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
		a = xor(r)
		b = xor(l - 1)
		return a ^ b


class Solution:

	def findXOR(self, l, r):

		def findXOR(n):
			rem = n % 4
			if rem == 0:
				return n
			elif rem == 1:
				return 1
			elif rem == 2:
				return n + 1
			else:
				return 0
		return findXOR(l - 1) ^ findXOR(r)