3845. Maximum Subarray XOR with Bounded Range Hard

@problem@discussion
#Array#Bit Manipulation#Trie#Queue#Sliding Window#Prefix Sum#Monotonic Queue



1/**
2 * [3845] Maximum Subarray XOR with Bounded Range
3 *
4 * You are given a non-negative integer array nums and an integer k.
5 * You must select a <span data-keyword="subarray-nonempty">subarray</span> of nums such that the difference between its maximum and minimum elements is at most k. The value of this subarray is the bitwise XOR of all elements in the subarray.
6 * Return an integer denoting the maximum possible value of the selected subarray.
7 *  
8 * <strong class="example">Example 1:
9 * <div class="example-block">
10 * Input: <span class="example-io">nums = [5,4,5,6], k = 2</span>
11 * Output: <span class="example-io">7</span>
12 * Explanation:
13 * 
14 * 	Select the subarray [5, <u>4, 5, 6</u>].
15 * 	The difference between its maximum and minimum elements is 6 - 4 = 2 <= k.
16 * 	The value is 4 XOR 5 XOR 6 = 7.
17 * </div>
18 * <strong class="example">Example 2:
19 * <div class="example-block">
20 * Input: <span class="example-io">nums = [5,4,5,6], k = 1</span>
21 * Output: <span class="example-io">6</span>
22 * Explanation:
23 * 
24 * 	Select the subarray [5, 4, 5, <u>6</u>].
25 * 	The difference between its maximum and minimum elements is 6 - 6 = 0 <= k.
26 * 	The value is 6.
27 * </div>
28 *  
29 * Constraints:
30 * 
31 * 	1 <= nums.length <= 4 * 10^4
32 * 	0 <= nums[i] < 2^15
33 * 	0 <= k < 2^15
34 * 
35 */
36pub struct Solution {}
37
38// problem: https://leetcode.com/problems/maximum-subarray-xor-with-bounded-range/
39// discuss: https://leetcode.com/problems/maximum-subarray-xor-with-bounded-range/discuss/?currentPage=1&orderBy=most_votes&query=
40
41// submission codes start here
42
43impl Solution {
44    pub fn max_xor(nums: Vec<i32>, k: i32) -> i32 {
45        0
46    }
47}
48
49// submission codes end
50
51#[cfg(test)]
52mod tests {
53    use super::*;
54
55    #[test]
56    fn test_3845() {
57    }
58}
59

Back
© 2026 bowen.ge All Rights Reserved.