Advertisement

XOR Queries of a Subarray - LeetCode 1310 Solution

XOR Queries of a Subarray - Complete Solution Guide

XOR Queries of a Subarray is LeetCode problem 1310, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

You are given an array arr of positive integers. You are also given the array queries where queries[i] = [left i, right i ] . For each query i compute the XOR of elements from left i to right i (that is, arr[left i ] XOR arr[left i + 1] XOR ... XOR arr[right i ] ). Return an array answer where answer[i] is the answer to the i th query. Example 1: Input: arr = [1,3,4,8], queries = [[0,1],[1,2],[0,3],[3,3]] Output: [2,7,14,8] Explanation: The binary representation of the elements in the array are:

Detailed Explanation

The problem asks us to calculate the XOR of elements within specified subarrays of a given array `arr`. We are provided with a list of queries, where each query `queries[i]` is a pair `[left_i, right_i]` representing the start and end indices of a subarray. For each query, we need to compute the XOR of all elements in `arr` from index `left_i` to `right_i` (inclusive) and return an array containing the XOR results for each query.

Solution Approach

The solution employs the prefix XOR technique. First, it computes the prefix XOR array, where each element `prefix[i]` stores the XOR of all elements from `arr[0]` to `arr[i-1]`. Then, for each query `[left, right]`, it calculates the XOR of the subarray `arr[left...right]` using the formula `prefix[right + 1] ^ prefix[left]`. This approach efficiently computes the XOR for each query in O(1) time after the initial prefix XOR array is built.

Step-by-Step Algorithm

  1. Step 1: Create a prefix XOR array `prefix` of size `len(arr) + 1`. Initialize `prefix[0] = 0`.
  2. Step 2: Iterate through the input array `arr` from index 0 to `len(arr) - 1`.
  3. Step 3: For each element `arr[i]`, calculate `prefix[i+1] = prefix[i] ^ arr[i]`. This builds the prefix XOR array.
  4. Step 4: Create an empty array `answer` to store the XOR results for each query.
  5. Step 5: Iterate through the `queries` array.
  6. Step 6: For each query `[left, right]`, calculate the XOR of the subarray using the formula `prefix[right + 1] ^ prefix[left]`.
  7. Step 7: Append the calculated XOR value to the `answer` array.
  8. Step 8: Return the `answer` array.

Key Insights

  • Insight 1: Calculating the XOR of a subarray naively for each query would result in a high time complexity. The key is to precompute XOR prefixes.
  • Insight 2: The XOR operation is its own inverse, i.e., `a ^ a = 0`. This property is crucial for efficiently calculating the XOR of subarrays using prefix XORs.
  • Insight 3: Using prefix XORs allows us to calculate the XOR of a subarray `arr[left...right]` as `prefix[right+1] ^ prefix[left]`. This is because `prefix[right+1]` represents the XOR of `arr[0...right]`, and `prefix[left]` represents the XOR of `arr[0...left-1]`. XORing these two cancels out the common prefix, leaving only the XOR of `arr[left...right]`.

Complexity Analysis

Time Complexity: O(N + Q), where N is the length of the array `arr` and Q is the number of queries. Building the prefix XOR array takes O(N) time. Calculating the XOR for each query takes O(1) time, and since we have Q queries, the total time for processing queries is O(Q). Therefore, the overall time complexity is O(N + Q).

Space Complexity: O(N), where N is the length of the array `arr`. The prefix XOR array `prefix` requires O(N) space. The `answer` array requires O(Q) space which is dominated by O(N) given the constraints, so it's still O(N).

Topics

This problem involves: Array, Bit Manipulation, Prefix Sum.

Companies

Asked at: Airtel.