Maximum XOR Score Subarray Queries - Problem
Maximum XOR Score Subarray Queries
You are given an array
The XOR score of an array is calculated through an iterative process:
1. Start with your array
2. Create a new array by XORing adjacent elements:
3. Remove the last element from the new array
4. Repeat until only one element remains - this is your XOR score
Example: Array [4, 2, 5] → [4⊕2, 2⊕5] = [6, 7] → [6⊕7] = [1] → Score = 1
Your task is to efficiently answer multiple queries, each asking for the maximum XOR score among all possible subarrays in a given range.
You are given an array
nums of n integers and a 2D array queries where each query [li, ri] asks you to find the maximum XOR score among all subarrays within the range nums[li..ri].The XOR score of an array is calculated through an iterative process:
1. Start with your array
a2. Create a new array by XORing adjacent elements:
a[i] XOR a[i+1]3. Remove the last element from the new array
4. Repeat until only one element remains - this is your XOR score
Example: Array [4, 2, 5] → [4⊕2, 2⊕5] = [6, 7] → [6⊕7] = [1] → Score = 1
Your task is to efficiently answer multiple queries, each asking for the maximum XOR score among all possible subarrays in a given range.
Input & Output
example_1.py — Basic XOR Score Calculation
$
Input:
nums = [2,8,4,32,16,1], queries = [[0,2],[1,4],[0,5]]
›
Output:
[12, 44, 60]
💡 Note:
For query [0,2] with subarray [2,8,4]: Round 1: [2⊕8, 8⊕4] = [10,12], Round 2: [10⊕12] = [6]. Among all subarrays in range [0,2], the maximum XOR score is 12 from subarray [8,4].
example_2.py — Single Element Query
$
Input:
nums = [1,3,5,7], queries = [[2,2]]
›
Output:
[5]
💡 Note:
Query [2,2] only contains one element nums[2] = 5. The XOR score of a single element is the element itself.
example_3.py — Full Array Query
$
Input:
nums = [4,2], queries = [[0,1]]
›
Output:
[6]
💡 Note:
For the full array [4,2]: XOR process gives [4⊕2] = [6]. Also check single elements: 4 and 2. Maximum is 6.
Constraints
- 1 ≤ n ≤ 2000
- 1 ≤ q ≤ 105
- 1 ≤ nums[i] ≤ 28
- queries[i] = [li, ri]
- 0 ≤ li ≤ ri < n
Visualization
Tap to expand
Understanding the Visualization
1
Initial Players
Start with array elements as tournament participants
2
Round 1
Adjacent players compete using XOR operation
3
Advance Winners
Winners move to next round, losers eliminated
4
Final Champion
Last remaining player is the XOR score
Key Takeaway
🎯 Key Insight: The XOR elimination process creates a triangular pattern of values that can be precomputed using dynamic programming, enabling efficient query processing.
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code