Taking on a new challenge: solving GeeksforGeeks POTD daily and sharing my solutions! π»π₯
The goal: sharpen problem-solving skills, level up coding, and learn something new every day. Follow my journey! π
100DaysOfCode #CodingChallenge #ProblemSolving #GeeksforGeeks #DeveloperJourney
Problem:
https://www.geeksforgeeks.org/problems/game-of-xor1541/1
Game of XOR
Difficulty: Medium Accuracy: 50.77%
You are given an integer array arr[]. The value of a subarray is defined as the bitwise XOR of all elements in that subarray.
Your task is to compute the bitwise XOR of the values of all possible subarrays of arr[].
Examples:
Input: arr[] = [1, 2, 3]
Output: 2
Explanation:
xor[1] = 1
xor[1, 2] = 3
xor[2, 3] = 1
xor[1, 2, 3] = 0
xor[2] = 2
xor[3] = 3
Result : 1 ^ 3 ^ 1 ^ 0 ^ 2 ^ 3 = 2
Input: arr[] = [1, 2]
Output: 0
Explanation:
xor[1] = 1
xor[1, 2] = 3
xor[2] = 2
Result : 1 ^ 3 ^ 2 = 0
Constraints:
1 β€ arr.size() β€ 105
0 β€ arr[i] β€ 109
Solution:
class Solution:
def subarrayXor(self, arr):
n = len(arr)
ans = 0
for i in range(n):
if ((i + 1) * (n - i)) % 2 == 1:
ans ^= arr[i]
return ans
Top comments (0)