githubEdit

04. Max XOR Subarray of size K

βœ… GFG solution to Max XOR Subarray of size K: find maximum XOR value among all subarrays of fixed size using efficient sliding window 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, return the maximum XOR of a subarray of size k.

Note: A subarray is a contiguous part of any given array.

πŸ“˜ Examples

Example 1

Input: arr[] = [2, 5, 8, 1, 1, 3], k = 3
Output: 15
Explanation: arr[0] ^ arr[1] ^ arr[2] = 2 ^ 5 ^ 8 = 15, which is maximum.

Example 2

Input: arr[] = [1, 2, 4, 5, 6], k = 2
Output: 6
Explanation: arr[1] ^ arr[2] = 2 ^ 4 = 6, which is maximum.

Example 3

πŸ”’ Constraints

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

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

  • $1 \le k \le \text{arr.size()}$

βœ… My Approach

The optimal solution uses Sliding Window with XOR Properties:

Sliding Window Technique

  1. Key XOR Property:

    • XOR is self-inverse: a ^ b ^ b = a

    • To remove an element from XOR, simply XOR it again.

    • This allows efficient window sliding in O(1) time.

  2. Window Management:

    • Maintain current XOR of window elements.

    • When adding new element: currentXOR ^= arr[i]

    • When removing old element: currentXOR ^= arr[i-k]

  3. Algorithm Steps:

    • Start with empty XOR (0).

    • For each position, add new element to window XOR.

    • If window size exceeds k, remove leftmost element.

    • Track maximum XOR value seen.

  4. Optimization:

    • Single loop handles both initialization and sliding.

    • No separate first window computation needed.

    • Constant time per element.

Why This Works: The XOR self-inverse property means we can efficiently add and remove elements from the running XOR value without recalculating from scratch.

πŸ“ 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, performing constant time XOR operations for each element.

  • Expected Auxiliary Space Complexity: O(1), as we only use a constant amount of extra space for variables like current XOR, maximum XOR, and loop counters.

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

chevron-right⚑ View Alternative Approaches with Code and Analysishashtag

πŸ“Š 2️⃣ Two-Phase Sliding Window

πŸ’‘ Algorithm Steps:

  1. Compute XOR of first k elements separately.

  2. Initialize maximum with first window XOR.

  3. Slide window: add new element, remove old element.

  4. Update maximum after each slide.

πŸ“ Complexity Analysis:

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

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

βœ… Why This Approach?

  • Clear separation of initialization and sliding

  • Easy to understand window concept

  • Traditional sliding window pattern

πŸ“Š 3️⃣ Brute Force Approach

πŸ’‘ Algorithm Steps:

  1. Try all possible subarrays of size k.

  2. For each subarray, compute XOR from scratch.

  3. Track maximum XOR value found.

  4. Return the maximum.

πŸ“ Complexity Analysis:

  • Time: ⏱️ O(n Γ— k) - Nested loops

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

βœ… Why This Approach?

  • Simplest to understand

  • No window sliding concept needed

  • Good for very small k values

Note: This approach results in Time Limit Exceeded (TLE) for large inputs (fails ~1110/1111 test cases due to time constraints).

πŸ“Š 4️⃣ Prefix XOR Array

πŸ’‘ Algorithm Steps:

  1. Build prefix XOR array where prefix[i] = arr[0] ^ arr[1] ^ ... ^ arr[i-1].

  2. XOR of subarray [i, i+k-1] = prefix[i+k] ^ prefix[i].

  3. Try all windows and track maximum.

  4. Return maximum XOR found.

πŸ“ Complexity Analysis:

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

  • Auxiliary Space: πŸ’Ύ O(n) - Prefix array storage

βœ… Why This Approach?

  • Demonstrates prefix XOR concept

  • Useful for multiple queries

  • Clean mathematical formulation

πŸ†š πŸ” Comparison of Approaches

πŸš€ Approach

⏱️ Time Complexity

πŸ’Ύ Space Complexity

βœ… Pros

⚠️ Cons

🎯 Single Loop Sliding

🟒 O(n)

🟒 O(1)

πŸš€ Most efficient, elegant

πŸ”§ Requires XOR property knowledge

πŸ“Š Two-Phase Sliding

🟒 O(n)

🟒 O(1)

πŸ“– Clear window pattern

πŸ”§ Two separate loops

πŸ”„ Brute Force

πŸ”΄ O(n Γ— k)

🟒 O(1)

🎯 Simplest logic

🐌 Inefficient for large k

πŸ“ˆ Prefix XOR

🟒 O(n)

πŸ”΄ O(n)

πŸ” Good for multiple queries

πŸ’Ύ Extra space required

πŸ† Best Choice Recommendation

🎯 Scenario

πŸŽ–οΈ Recommended Approach

πŸ”₯ Performance Rating

πŸ… Optimal performance needed

πŸ₯‡ Single Loop Sliding

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

πŸ“– Learning sliding window

πŸ₯ˆ Two-Phase Sliding

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

🎯 Understanding basics

πŸ₯‰ Brute Force

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

πŸ” Multiple queries on same array

πŸ… Prefix XOR

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

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