← Home

1310. XOR Queries of a Subarray

LeetCode article · C++ solution
Website made by wuisabel-gif · Original C++ code by keineahnung2345
dynamic programmingC++Markdown
131

A good way into this one is to ask: what do we need to remember so we never redo work blindly? For 1310. XOR Queries of a Subarray, the solution in this repository is mainly a dynamic programming solution.

Guide

What?

The code is easier to read if we treat it as a controlled search through possible states. Instead of trying to be clever immediately, read the code as a sequence of questions:

  • What state are we keeping?
  • How do we move from one state to the next?
  • When do we know the answer is already determined?

For this file, the main tools are: dynamic programming, two pointers, bit manipulation.

Guide

When?

Use this approach when the hard part is not syntax, but deciding what must stay true after every update. The accepted code reduces that pressure by storing exactly the information that remains useful later.

The important function names to track are xorQueries.

Guide

Why?

The code is doing bookkeeping so your brain does not have to keep the entire search space open at once.

  • The two-dimensional vector is the memory of the solution: grid state, DP state, or adjacency shape.
  • The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.

Guide

How?

Walk through the solution in this order:

  1. Read the setup variables first.
  2. Follow the main loop or recursive helper next.
  3. Watch where invalid states get skipped.
  4. Check which value survives to the return statement.

The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.

Guide

Complexity

  • Time: O(n) to O(n log n), depending on the dominant loop or data structure operation
  • Space: O(n) in the usual case for auxiliary containers or recursion

Guide

C++ Solution

Your submission

The accepted solution

solution.cpp
01//Runtime: 132 ms, faster than 71.89% of C++ online submissions for XOR Queries of a Subarray.
02//Memory Usage: 26.2 MB, less than 100.00% of C++ online submissions for XOR Queries of a Subarray.
03
04class Solution {
05public:
06    vector<int> xorQueries(vector<int>& arr, vector<vector<int>>& queries) {
07        vector<int> dp = arr;
08        vector<int> results;
09        int l, r;
10        
11        for(int i = 1; i < dp.size(); i++){
12            dp[i] ^= dp[i-1];
13        }
14        
15        for(vector<int>& query : queries){
16            l = query[0]; r = query[1];
17            int result = dp[r];
18            if(l > 0){
19                //doing XOR twice equals doing nothing
20                result ^= dp[l-1];
21            }
22            results.push_back(result);
23        }
24        
25        return results;
26    }
27};

Cost

Complexity

Time
O(n) to O(n log n), depending on the dominant loop or data structure operation
Dominated by the main traversal, recursion, or data-structure operations in the code.
Space
O(n) in the usual case for auxiliary containers or recursion
Auxiliary state plus the answer structure where the problem requires one.