githubEdit

25. Game of XOR

βœ… GFG solution to the Game of XOR problem: compute the bitwise XOR of all subarray XORs using mathematical contribution analysis and parity optimization. πŸš€

The problem can be found at the following link: πŸ”— Question Linkarrow-up-right

🧩 Problem Description

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

Example 1

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

Example 2

πŸ”’ Constraints

  • $1 \le \text{arr.size()} \le 10^5$

  • $0 \le \text{arr}[i] \le 10^9$

βœ… My Approach

The optimal approach uses mathematical contribution analysis with parity checking to avoid the brute force generation of all subarrays:

Mathematical Contribution with Parity Optimization

  1. Key Observation:

    • Each element arr[i] appears in multiple subarrays.

    • The element at index i appears in exactly (i + 1) * (n - i) subarrays.

    • Due to XOR properties, an element only contributes to the final result if it appears an odd number of times.

  2. Parity Analysis:

    • Since a ^ a = 0, if an element appears an even number of times, it cancels itself out.

    • We only need to XOR elements that appear an odd number of times across all subarrays.

  3. Efficient Calculation:

    • For each element at index i, calculate its contribution count: (i + 1) * (n - i).

    • Check if this count is odd using bitwise AND: ((i + 1) * (n - i)) & 1.

    • If odd, include the element in the final XOR result.

  4. Single Pass Solution:

    • Iterate through the array once, checking the parity condition for each element.

    • Accumulate the XOR of elements with odd contribution counts.

πŸ“ Time and Auxiliary Space Complexity

  • Expected Time Complexity: O(n), where n is the size of the array. We perform a single traversal through the array, and for each element, we execute constant-time operations (multiplication, bitwise AND, and XOR).

  • Expected Auxiliary Space Complexity: O(1), as we only use a fixed number of variables (xor_sum, n, i) regardless of the input size. No additional data structures are required.

πŸ§‘β€πŸ’» Code (C++)

chevron-right⚑ View Alternative Approaches with Code and Analysishashtag

πŸ“Š 2️⃣ Mathematical Contribution Count

πŸ’‘ Algorithm Steps:

  1. Calculate contribution of each element based on subarray count.

  2. Element at index i appears in (i+1) * (n-i) subarrays.

  3. XOR only when contribution count is odd.

  4. Return final XOR result.

πŸ“ Complexity Analysis:

  • Time: ⏱️ O(n) - Single pass through array

  • Auxiliary Space: πŸ’Ύ O(1) - Constant space usage

βœ… Why This Approach?

  • Clear mathematical reasoning

  • Easy to understand contribution logic

  • Direct calculation without bit manipulation

πŸ“Š 3️⃣ Parity Check with Modulo

πŸ’‘ Algorithm Steps:

  1. Check parity of both left count and right count separately.

  2. Use modulo operation for explicit odd/even checking.

  3. XOR element when both counts are odd.

  4. Accumulate result and return.

πŸ“ Complexity Analysis:

  • Time: ⏱️ O(n) - Linear traversal

  • Auxiliary Space: πŸ’Ύ O(1) - No extra space

βœ… Why This Approach?

  • Explicit parity checking with modulo

  • Readable condition structure

  • Straightforward implementation

πŸ“Š 4️⃣ Bitwise AND Optimization

πŸ’‘ Algorithm Steps:

  1. Use bitwise AND with 1 to check odd parity efficiently.

  2. Check if both position and remaining elements are odd.

  3. Apply XOR when condition satisfies.

  4. Return accumulated XOR value.

πŸ“ Complexity Analysis:

  • Time: ⏱️ O(n) - Single iteration

  • Auxiliary Space: πŸ’Ύ O(1) - Constant space

βœ… Why This Approach?

  • Bitwise operations for faster execution

  • Compact and efficient condition checking

  • Optimal for competitive programming

πŸ†š πŸ” Comparison of Approaches

πŸš€ Approach

⏱️ Time Complexity

πŸ’Ύ Space Complexity

βœ… Pros

⚠️ Cons

πŸ”’ Multiplication Check

🟒 O(n)

🟒 O(1)

πŸš€ Most optimized

βž— Extra multiplication

πŸ“ Count Calculation

🟒 O(n)

🟒 O(1)

πŸ“– Clear mathematical logic

πŸ”’ Redundant calculation

βœ”οΈ Modulo Parity

🟒 O(n)

🟒 O(1)

🎯 Explicit condition check

🐌 Modulo slightly slower

⚑ Bitwise AND

🟒 O(n)

🟒 O(1)

⭐ Fastest bitwise operations

🧩 Less readable for beginners

πŸ† Best Choice Recommendation

🎯 Scenario

πŸŽ–οΈ Recommended Approach

πŸ”₯ Performance Rating

πŸ… Optimal performance needed

πŸ₯‡ Multiplication Check

β˜…β˜…β˜…β˜…β˜…

πŸ“– Readability priority

πŸ₯ˆ Count Calculation

β˜…β˜…β˜…β˜…β˜†

πŸ”§ Learning purpose

πŸ₯‰ Modulo Parity

β˜…β˜…β˜…β˜…β˜†

🎯 Competitive programming

πŸ… Bitwise AND

β˜…β˜…β˜…β˜…β˜…

β˜• Code (Java)

🐍 Code (Python)

🧠 Contribution and Support

For discussions, questions, or doubts related to this solution, feel free to connect on LinkedIn: πŸ“¬ Any Questions?arrow-up-right. Let's make this learning journey more collaborative!

⭐ If you find this helpful, please give this repository a star! ⭐


πŸ“Visitor Count

Visitor counter

Last updated