githubEdit

15. Count Subarrays with given XOR

βœ… GFG solution to Count Subarrays with given XOR: count subarrays with specific XOR value using prefix XOR and hash map technique. πŸš€

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

🧩 Problem Description

Given an array of integers arr[] and a number k, count the number of subarrays having XOR of their elements as k.

Note: It is guaranteed that the total count will fit within a 32-bit integer.

πŸ“˜ Examples

Example 1

Input: arr[] = [4, 2, 2, 6, 4], k = 6
Output: 4
Explanation: The subarrays having XOR of their elements as 6 are [4, 2], [4, 2, 2, 6, 4], 
[2, 2, 6], and [6]. Hence, the answer is 4.

Example 2

Input: arr[] = [5, 6, 7, 8, 9], k = 5
Output: 2
Explanation: The subarrays having XOR of their elements as 5 are [5] and [5, 6, 7, 8, 9]. 
Hence, the answer is 2.

Example 3

πŸ”’ Constraints

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

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

  • $0 \le k \le 10^5$

βœ… My Approach

The optimal solution uses Prefix XOR with Hash Map:

Prefix XOR Technique

  1. Key Observation:

    • For a subarray from index i to j, XOR equals prefixXOR[j] ^ prefixXOR[i-1].

    • If this XOR equals k, then prefixXOR[j] ^ prefixXOR[i-1] = k.

    • Rearranging: prefixXOR[i-1] = prefixXOR[j] ^ k.

  2. Hash Map Strategy:

    • Store frequency of each prefix XOR value encountered so far.

    • For current prefix XOR curr, check how many times curr ^ k appeared before.

    • Each occurrence represents a valid subarray ending at current position.

  3. Algorithm Steps:

    • Initialize hash map and prefix XOR as 0.

    • Add initial state: map[0] = 1 (empty prefix).

    • For each element, update prefix XOR.

    • Count subarrays: add map[prefixXOR ^ k] to result.

    • Update map with current prefix XOR.

  4. Return Result:

    • Total count of subarrays with XOR equal to k.

Mathematical Insight: XOR property: a ^ b = c implies a = b ^ c. This allows us to find required prefix XORs efficiently.

πŸ“ Time and Auxiliary Space Complexity

  • Expected Time Complexity: O(n), where n is the size of the array. We make a single pass through the array, and each hash map operation (insert and lookup) takes O(1) average time.

  • Expected Auxiliary Space Complexity: O(n), as in the worst case, all prefix XOR values could be distinct, requiring O(n) space in the hash map to store all unique prefix XORs.

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

chevron-right⚑ View Alternative Approaches with Code and Analysishashtag

πŸ“Š 2️⃣ Prefix XOR Array Approach

πŸ’‘ Algorithm Steps:

  1. Build a prefix XOR array where prefixXOR[i] = XOR of elements from 0 to i.

  2. For each pair (i, j), check if prefixXOR[j] ^ prefixXOR[i-1] equals k.

  3. Use hash map to count occurrences efficiently.

  4. Avoid explicit array by using running XOR.

πŸ“ Complexity Analysis:

  • Time: ⏱️ O(n) - Single pass with hash map operations

  • Auxiliary Space: πŸ’Ύ O(n) - Hash map storage

βœ… Why This Approach?

  • Slightly different order of operations

  • Checks current prefix XOR against k first

  • Clear demonstration of prefix XOR concept

πŸ†š πŸ” Comparison of Approaches

πŸš€ Approach

⏱️ Time Complexity

πŸ’Ύ Space Complexity

βœ… Pros

⚠️ Cons

🎯 Prefix XOR + HashMap

🟒 O(n)

🟑 O(n)

πŸš€ Optimal linear time

πŸ’Ύ Extra space for hash map

πŸ“Š Explicit Prefix Check

🟒 O(n)

🟑 O(n)

🎯 Clear logic flow

πŸ”§ Similar to main approach

πŸ† Best Choice Recommendation

🎯 Scenario

πŸŽ–οΈ Recommended Approach

πŸ”₯ Performance Rating

πŸ… Optimal performance needed

πŸ₯‡ Prefix XOR + HashMap

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

πŸ”§ Production code

πŸ₯ˆ Prefix XOR + HashMap

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

β˜• 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