XOR After Range Multiplication Queries II - Solution & Explanation
Problem Statement
You are given an integer array nums of length n and a 2D integer array queries of size q, where queries[i] = [li, ri, ki, vi].
For each query, you must apply the following operations in order:
- Set
idx = li. - While
idx <= ri:- Update:
nums[idx] = (nums[idx] * vi) % (109 + 7). - Set
idx += ki.
- Update:
Return the bitwise XOR of all elements in nums after processing all queries.
Example 1:
Input: nums = [1,1,1], queries = [[0,2,1,4]]
Output: 4
Explanation:
- A single query
[0, 2, 1, 4]multiplies every element from index 0 through index 2 by 4. - The array changes from
[1, 1, 1]to[4, 4, 4]. - The XOR of all elements is
4 ^ 4 ^ 4 = 4.
Example 2:
Input: nums = [2,3,1,5,4], queries = [[1,4,2,3],[0,2,1,2]]
Output: 31
Explanation:
- The first query
[1, 4, 2, 3]multiplies the elements at indices 1 and 3 by 3, transforming the array to[2, 9, 1, 15, 4]. - The second query
[0, 2, 1, 2]multiplies the elements at indices 0, 1, and 2 by 2, resulting in[4, 18, 2, 15, 4]. - Finally, the XOR of all elements is
4 ^ 18 ^ 2 ^ 15 ^ 4 = 31.โโโโโโโโโโโโโโ
Constraints:
1 <= n == nums.length <= 1051 <= nums[i] <= 1091 <= q == queries.length <= 105โโโโโโโqueries[i] = [li, ri, ki, vi]0 <= li <= ri < n1 <= ki <= n1 <= vi <= 105
Approach Overview
Problem Overview: You maintain an array while processing queries that multiply every element in a subarray by a value. After applying updates, you need the XOR of elements. A naive update per query quickly becomes too slow because every multiplication changes the stored values.
Approach 1: Brute Force Range Updates (O(n ยท q) time, O(1) space)
Apply each query directly to the array. Iterate from l to r, multiply every element, then recompute the XOR when needed by scanning the array. This approach is simple but inefficient because each range update may touch up to n elements. With up to q operations, the runtime becomes O(n ยท q). It only works for very small constraints.
Approach 2: Segment Tree with Lazy Range Multiplication (O((n + q) log n) time, O(n) space)
Build a segment tree where every node stores the XOR of its segment. Range multiplication queries are applied using lazy propagation so the update does not immediately touch every element. When a node is fully covered by a query range, store the pending multiplier in a lazy tag and update the node value accordingly. During traversal, propagate the multiplier to children before accessing them. Each update or query touches at most O(log n) nodes, which keeps the total complexity manageable even with large inputs.
The key idea is separating updates from evaluation. Instead of modifying each element in the range, you mark segments as "pending multiplication" and only resolve them when that segment is visited again. This reduces repeated work dramatically compared to brute force. Data structures like a divide and conquer segment tree combined with lazy propagation allow updates and aggregation to coexist efficiently.
Recommended for interviews: The segment tree approach is what interviewers expect for large range update problems. Showing the brute force first demonstrates you understand the direct simulation. Implementing the optimized version with lazy propagation shows mastery of array range operations and divide and conquer data structures.
Solution
Try this approach in the editor โDetailed Complexity Analysis
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Brute Force Range Updates | O(n ยท q) | O(1) | Small arrays or when query count is very low |
| Segment Tree with Lazy Multiplication | O((n + q) log n) | O(n) | General case with many range updates and queries |
| Divide and Conquer Segment Tree | O((n + q) log n) | O(n) | When implementing structured range updates and XOR aggregation |
Video Solution
XOR After Range Multiplication Queries II | Leetcode 3655 | Difference Array & Questions - 9 | MIK โข codestorywithMIK โข 11,764 views views
Watch 9 more video solutions โFrequently Asked Questions
Is XOR After Range Multiplication Queries II easy or hard?
XOR After Range Multiplication Queries II Python/Java solution
What is the best approach for XOR After Range Multiplication Queries II?
Is XOR After Range Multiplication Queries II asked at Google/Amazon/Meta?
What data structure is used in XOR After Range Multiplication Queries II?
What is the time complexity of XOR After Range Multiplication Queries II?
How to solve XOR After Range Multiplication Queries II in O((n + q) log n)?
Ready to solve this problem?
Practice XOR After Range Multiplication Queries II with our built-in code editor and test cases.
Practice on FleetCodeTable of Contents
Practice this problem
Open in Editor