Maximum XOR Score Subarray Queries - Problem
Maximum XOR Score Subarray Queries

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 a
2. 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
Maximum XOR Score Subarray Queries INPUT nums array: 2 [0] 8 [1] 4 [2] 32 [3] 16 [4] 1 [5] Queries: Q1: [0, 2] range Q2: [1, 4] range Q3: [0, 5] range XOR Score Process: [a,b,c] XOR adjacent [a^b, b^c] [(a^b)^(b^c)] = a^c (final score) ALGORITHM (DP) 1 Build XOR Score DP dp[i][j] = XOR score of subarray nums[i..j] 2 Recurrence Relation dp[i][j] = dp[i][j-1] ^ dp[i+1][j] 3 Build Max DP max[i][j] = max XOR score in range [i,j] 4 Answer Queries O(1) lookup per query result = max[li][ri] DP Table (partial): i\j 0 1 2 0 2 10 12 1 - 8 12 FINAL RESULT Query Results: Q1: [0,2] nums[0..2] [2, 8, 4] max XOR score = 12 Q2: [1,4] nums[1..4] [8,4,32,16] max XOR score = 44 Q3: [0,5] nums[0..5] [2,8,4,32,16,1] max score = 60 Output Array: [12, 44, 60] OK - All queries answered Time: O(n^2) preprocess + O(1) per query Key Insight: The XOR score of subarray [i,j] follows the recurrence: dp[i][j] = dp[i][j-1] XOR dp[i+1][j]. We precompute two DP tables: one for XOR scores and one for maximum scores in each range. This allows O(n^2) preprocessing and O(1) query answering, making it efficient for multiple queries. TutorialsPoint - Maximum XOR Score Subarray Queries | DP Approach
Asked in
Google 35 Meta 28 Microsoft 22 Amazon 18
21.2K Views
Medium Frequency
~25 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen