Skip to main content

Command Palette

Search for a command to run...

Maximum XOR for Each Query

Published
1 min read
Maximum XOR for Each Query
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 a sorted array nums of n non-negative integers and an integer maximumBit. You want to perform the following query n times:

  1. Find a non-negative integer k < 2<sup>maximumBit</sup> such that nums[0] XOR nums[1] XOR ... XOR nums[nums.length-1] XOR k is maximized. k is the answer to the i<sup>th</sup> query.

  2. Remove the last element from the current array nums.

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

LeetCode Problem - 1829

import java.util.*;

class Solution {
    public int[] getMaximumXor(int[] nums, int maximumBit) {
        int n = nums.length;
        int[] result = new int[n];

        int cumulativeXor = 0;
        int limit = (1 << maximumBit) - 1; // 2^maximumBit - 1

        // Fill the result array with the XOR of the prefix
        for (int i = 0; i < n; i++) {
            cumulativeXor ^= nums[i];
            result[i] = cumulativeXor ^ limit;
        }

        // Reverse the result array to match the required order
        for (int i = 0; i < n / 2; i++) {
            int temp = result[i];
            result[i] = result[n - 1 - i];
            result[n - 1 - i] = temp;
        }

        return result;
    }
}

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.