Skip to content

Commit 2af099f

Browse files
chore: add LeetCode daily solution
1 parent 2aa7f94 commit 2af099f

5 files changed

Lines changed: 77 additions & 0 deletions

File tree

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# XOR After Range Multiplication Queries I (Medium)
2+
3+
**Problem ID:** 3653
4+
**Date:** 2026-04-08
5+
**Link:** https://leetcode.com/problems/xor-after-range-multiplication-queries-i/
6+
7+
## Approach
8+
9+
To solve the problem "XOR After Range Multiplication Queries I," we can follow a systematic approach that efficiently processes the queries and computes the final XOR of the modified array.
10+
11+
### Main Idea:
12+
The goal is to apply a series of multiplication operations specified in the queries to the elements of the array `nums`, and then compute the XOR of the entire array after all operations have been applied. Given the constraints, a naive approach that directly modifies the array for each query could lead to inefficiencies, particularly when the number of queries and the size of the array are both large.
13+
14+
### Approach:
15+
1. **Iterate Through Queries**: For each query, which consists of four parameters `[l_i, r_i, k_i, v_i]`, we will:
16+
- Start from index `l_i` and apply the multiplication operation to elements at indices `l_i`, `l_i + k_i`, `l_i + 2*k_i`, ..., up to `r_i`.
17+
- This means we will update `nums[idx]` to `(nums[idx] * v_i) % (10^9 + 7)` for each valid index in the specified range.
18+
19+
2. **Efficient Indexing**: Instead of updating each element in a nested loop, we can directly compute the indices to be updated using the formula:
20+
- `idx = l_i + j * k_i` where `j` is a non-negative integer such that `idx` remains within the bounds of `r_i`.
21+
22+
3. **Final XOR Calculation**: After processing all the queries, we will compute the XOR of all elements in the modified `nums` array. This can be done in a single pass through the array.
23+
24+
### Data Structures:
25+
- We primarily use an integer array `nums` to store the values and a list (or array) to hold the queries.
26+
27+
### Complexity:
28+
- **Time Complexity**: The overall time complexity is O(q * (r_i - l_i) / k_i), where `q` is the number of queries. In the worst case, this can approach O(n * q) if every query affects most of the elements in the array. However, since the maximum constraints are relatively small (both `n` and `q` up to 1000), this is manageable.
29+
- **Space Complexity**: O(1) additional space is used aside from the input arrays, as we are modifying `nums` in place and using a few variables for computations.
30+
31+
By following this approach, we efficiently apply the range multiplication operations and compute the final XOR without unnecessary overhead, ensuring that we stay within acceptable time limits for the given constraints.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
public int xorAfterQueries(int[] nums, int[][] queries) {
3+
int MOD = 1000000007;
4+
for (int[] query : queries) {
5+
int l = query[0], r = query[1], k = query[2], v = query[3];
6+
for (int idx = l; idx <= r; idx += k) {
7+
nums[idx] = (int)((long)nums[idx] * v % MOD);
8+
}
9+
}
10+
11+
int result = 0;
12+
for (int num : nums) {
13+
result ^= num;
14+
}
15+
16+
return result;
17+
}
18+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
var xorQueries = function(nums, queries) {
2+
const MOD = 1e9 + 7;
3+
const n = nums.length;
4+
5+
for (const [l, r, k, v] of queries) {
6+
for (let idx = l; idx <= r; idx += k) {
7+
nums[idx] = (nums[idx] * v) % MOD;
8+
}
9+
}
10+
11+
return nums.reduce((acc, num) => acc ^ num, 0);
12+
};
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution:
2+
def xorQueries(self, nums: List[int], queries: List[List[int]]) -> int:
3+
MOD = 10**9 + 7
4+
5+
for l, r, k, v in queries:
6+
idx = l
7+
while idx <= r:
8+
nums[idx] = (nums[idx] * v) % MOD
9+
idx += k
10+
11+
result = 0
12+
for num in nums:
13+
result ^= num
14+
15+
return result

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,3 +417,4 @@ Through completing the Blind 75 and NeetCode 150, you will have mastered:
417417
- 2026-04-05 — [Robot Return to Origin](https://leetcode.com/problems/robot-return-to-origin/) (Easy) → `Easy/2026-04-05-657-Robot-Return-to-Origin`
418418
- 2026-04-06 — [Walking Robot Simulation](https://leetcode.com/problems/walking-robot-simulation/) (Medium) → `Medium/2026-04-06-874-Walking-Robot-Simulation`
419419
- 2026-04-07 — [Walking Robot Simulation II](https://leetcode.com/problems/walking-robot-simulation-ii/) (Medium) → `Medium/2026-04-07-2069-Walking-Robot-Simulation-II`
420+
- 2026-04-08 — [XOR After Range Multiplication Queries I](https://leetcode.com/problems/xor-after-range-multiplication-queries-i/) (Medium) → `Medium/2026-04-08-3653-XOR-After-Range-Multiplication-Queries-I`

0 commit comments

Comments
 (0)