1829. Maximum XOR for Each Query Medium
1/**
2 * [1829] Maximum XOR for Each Query
3 *
4 * 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:
5 * <ol>
6 * Find a non-negative integer k < 2^maximumBit such that nums[0] XOR nums[1] XOR ... XOR nums[nums.length-1] XOR k is maximized. k is the answer to the i^th query.
7 * Remove the last element from the current array nums.
8 * </ol>
9 * Return an array answer, where answer[i] is the answer to the i^th query.
10 *
11 * Example 1:
12 *
13 * Input: nums = [0,1,1,3], maximumBit = 2
14 * Output: [0,3,2,3]
15 * Explanation: The queries are answered as follows:
16 * 1^st query: nums = [0,1,1,3], k = 0 since 0 XOR 1 XOR 1 XOR 3 XOR 0 = 3.
17 * 2^nd query: nums = [0,1,1], k = 3 since 0 XOR 1 XOR 1 XOR 3 = 3.
18 * 3^rd query: nums = [0,1], k = 2 since 0 XOR 1 XOR 2 = 3.
19 * 4^th query: nums = [0], k = 3 since 0 XOR 3 = 3.
20 *
21 * Example 2:
22 *
23 * Input: nums = [2,3,4,7], maximumBit = 3
24 * Output: [5,2,6,5]
25 * Explanation: The queries are answered as follows:
26 * 1^st query: nums = [2,3,4,7], k = 5 since 2 XOR 3 XOR 4 XOR 7 XOR 5 = 7.
27 * 2^nd query: nums = [2,3,4], k = 2 since 2 XOR 3 XOR 4 XOR 2 = 7.
28 * 3^rd query: nums = [2,3], k = 6 since 2 XOR 3 XOR 6 = 7.
29 * 4^th query: nums = [2], k = 5 since 2 XOR 5 = 7.
30 *
31 * Example 3:
32 *
33 * Input: nums = [0,1,2,2,5,7], maximumBit = 3
34 * Output: [4,3,6,4,6,7]
35 *
36 *
37 * Constraints:
38 *
39 * nums.length == n
40 * 1 <= n <= 10^5
41 * 1 <= maximumBit <= 20
42 * 0 <= nums[i] < 2^maximumBit
43 * nums is sorted in ascending order.
44 *
45 */
46pub struct Solution {}
47
48// problem: https://leetcode.com/problems/maximum-xor-for-each-query/
49// discuss: https://leetcode.com/problems/maximum-xor-for-each-query/discuss/?currentPage=1&orderBy=most_votes&query=
50
51// submission codes start here
52
53impl Solution {
54 pub fn get_maximum_xor(nums: Vec<i32>, maximum_bit: i32) -> Vec<i32> {
55 vec![]
56 }
57}
58
59// submission codes end
60
61#[cfg(test)]
62mod tests {
63 use super::*;
64
65 #[test]
66 fn test_1829() {
67 }
68}
69
Back
© 2025 bowen.ge All Rights Reserved.