Range XOR Queries with Subarray Reversals - Problem

You are given an integer array nums of length n and a 2D integer array queries of length q, where each query is one of the following three types:

Update: queries[i] = [1, index, value] - Set nums[index] = value.

Range XOR Query: queries[i] = [2, left, right] - Compute the bitwise XOR of all elements in the subarray nums[left...right], and record this result.

Reverse Subarray: queries[i] = [3, left, right] - Reverse the subarray nums[left...right] in place.

Return an array of the results of all range XOR queries in the order they were encountered.

Input & Output

Example 1 — Basic Mixed Operations
$ Input: nums = [1,3,4,8], queries = [[2,0,1],[1,1,5],[2,0,1],[3,0,3],[2,0,3]]
Output: [2,4,6]
💡 Note: Query [2,0,1]: XOR of nums[0..1] = 1⊕3 = 2. Query [1,1,5]: Update nums[1] = 5. Query [2,0,1]: XOR of nums[0..1] = 1⊕5 = 4. Query [3,0,3]: Reverse nums[0..3] → [8,4,5,1]. Query [2,0,3]: XOR of nums[0..3] = 8⊕4⊕5⊕1 = 6.
Example 2 — Only XOR Queries
$ Input: nums = [2,3,4], queries = [[2,0,2],[2,1,2],[2,0,1]]
Output: [5,7,1]
💡 Note: Query [2,0,2]: XOR of nums[0..2] = 2⊕3⊕4 = 5. Query [2,1,2]: XOR of nums[1..2] = 3⊕4 = 7. Query [2,0,1]: XOR of nums[0..1] = 2⊕3 = 1.
Example 3 — Update and Reverse
$ Input: nums = [5,6], queries = [[1,0,2],[3,0,1],[2,0,1]]
Output: [4]
💡 Note: Query [1,0,2]: Update nums[0] = 2, so nums = [2,6]. Query [3,0,1]: Reverse nums[0..1] → [6,2]. Query [2,0,1]: XOR of nums[0..1] = 6⊕2 = 4.

Constraints

  • 1 ≤ nums.length ≤ 105
  • 1 ≤ queries.length ≤ 105
  • 0 ≤ nums[i] ≤ 106
  • For update queries: 0 ≤ index < nums.length, 0 ≤ value ≤ 106
  • For range queries: 0 ≤ left ≤ right < nums.length

Visualization

Tap to expand
Range XOR Queries with Subarray Reversals INPUT nums array: 1 [0] 3 [1] 4 [2] 8 [3] Queries: [2,0,1] XOR range 0-1 [1,1,5] Update idx 1=5 [2,0,1] XOR range 0-1 [3,0,3] Reverse 0-3 [2,0,3] XOR range 0-3 Type 2: XOR Type 1: Update Type 3: Reverse ALGORITHM STEPS 1 Query [2,0,1]: XOR 1 XOR 3 = 2 2 2 Query [1,1,5]: Update nums[1] = 5 [1,5,4,8] 3 Query [2,0,1]: XOR 1 XOR 5 = 4 4 4 Query [3,0,3]: Reverse Reverse array [8,4,5,1] 5 Query [2,0,3]: XOR 8^4^5^1 = 6 6 Collect XOR results in order FINAL RESULT XOR Query Results: [2, 4, 6] Result Breakdown: Query 1: nums[0..1] 1 XOR 3 = 2 Query 3: nums[0..1] (updated) 1 XOR 5 = 4 Query 5: nums[0..3] (reversed) 8^4^5^1 = 6 OK Key Insight: Use helper functions to handle each query type separately. XOR has the property that a XOR a = 0, making range XOR computations efficient. Process queries sequentially, maintaining array state after updates and reversals, collecting only XOR query results. TutorialsPoint - Range XOR Queries with Subarray Reversals | Optimized with Helper Functions
Asked in
Google 25 Microsoft 20 Amazon 15
12.0K Views
Medium Frequency
~25 min Avg. Time
450 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