Skip to main content

Command Palette

Search for a command to run...

XOR Queries of a Subarray

Published
1 min read
XOR Queries of a Subarray
G

As a Systems Engineer at Tata Consultancy Services, I deliver exceptional software products for mobile and web platforms, using agile methodologies and robust quality maintenance. I am experienced in performance testing, automation testing, API testing, and manual testing, with various tools and technologies such as Jmeter, Azure LoadTest, Selenium, Java, OOPS, Maven, TestNG, and Postman.

I have successfully developed and executed detailed test plans, test cases, and scripts for Android and web applications, ensuring high-quality standards and user satisfaction. I have also demonstrated my proficiency in manual REST API testing with Postman, as well as in end-to-end performance and automation testing using Jmeter and selenium with Java, TestNG and Maven. Additionally, I have utilized Azure DevOps for bug tracking and issue management.

You are given an array arr of positive integers. You are also given the array queries where queries[i] = [left<sub>i, </sub> right<sub>i</sub>].

For each query i compute the XOR of elements from left<sub>i</sub> to right<sub>i</sub> (that is, arr[left<sub>i</sub>] XOR arr[left<sub>i</sub> + 1] XOR ... XOR arr[right<sub>i</sub>] ).

Return an array answer where answer[i] is the answer to the i<sup>th</sup> query.

LeetCode Problem - 1310

class Solution {
    public int[] xorQueries(int[] arr, int[][] queries) {
        // Initialize an array to store the results of each query
        int[] answer = new int[queries.length];

        // Loop through each query in the queries array
        for (int j = 0; j < queries.length; j++) {
            // Extract the start (right) and end (left) indices for the current query
            int right = queries[j][0];
            int left = queries[j][1];

            // Initialize a temporary variable to store the XOR result for the current query
            int tempAns = 0;

            // Iterate over the array 'arr' from index 'right' to 'left'
            for (int i = right; i <= left; i++) {
                // Perform XOR operation on each element in the range
                tempAns ^= arr[i];
            }

            // Store the result of the XOR operation for the current query
            answer[j] = tempAns;
        }
        // Return the array containing the XOR results for all queries
        return answer;
    }
}

More from this blog

S

Software and Performance Testing Insights

462 posts

Results-Driven Agile QA Specialist | Expert in Mobile & Web Testing | Proficient in Test Planning, Execution, and Root Cause Analysis.