-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy path1829-maximum-xor-for-each-query.js
More file actions
36 lines (32 loc) · 1 KB
/
1829-maximum-xor-for-each-query.js
File metadata and controls
36 lines (32 loc) · 1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/**
* 1829. Maximum XOR for Each Query
* https://leetcode.com/problems/maximum-xor-for-each-query/
* Difficulty: Medium
*
* 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:
* - Find a non-negative integer k < 2maximumBit such that nums[0] XOR nums[1] XOR ... XOR
* nums[nums.length-1] XOR k is maximized. k is the answer to the ith query.
* - Remove the last element from the current array nums.
*
* Return an array answer, where answer[i] is the answer to the ith query.
*/
/**
* @param {number[]} nums
* @param {number} maximumBit
* @return {number[]}
*/
var getMaximumXor = function(nums, maximumBit) {
const result = [];
let currentXor = 0;
const maxValue = (1 << maximumBit) - 1;
for (const num of nums) {
currentXor ^= num;
}
for (let i = nums.length - 1; i >= 0; i--) {
const optimalK = currentXor ^ maxValue;
result.push(optimalK);
currentXor ^= nums[i];
}
return result;
};