3630. Partition Array for Maximum XOR and AND Hard

@problem@discussion
#Array#Math#Greedy#Bit Manipulation#Enumeration



1/**
2 * [3630] Partition Array for Maximum XOR and AND
3 *
4 * You are given an integer array nums.
5 * Partition the array into three (possibly empty) <span data-keyword="subsequence-array">subsequences</span> A, B, and C such that every element of nums belongs to exactly one subsequence.
6 * Your goal is to maximize the value of: XOR(A) + AND(B) + XOR(C)
7 * where:
8 * 
9 * 	XOR(arr) denotes the bitwise XOR of all elements in arr. If arr is empty, its value is defined as 0.
10 * 	AND(arr) denotes the bitwise AND of all elements in arr. If arr is empty, its value is defined as 0.
11 * 
12 * Return the maximum value achievable.
13 * Note: If multiple partitions result in the same maximum sum, you can consider any one of them.
14 *  
15 * <strong class="example">Example 1:
16 * <div class="example-block">
17 * Input: <span class="example-io">nums = [2,3]</span>
18 * Output: <span class="example-io">5</span>
19 * Explanation:
20 * One optimal partition is:
21 * 
22 * 	A = [3], XOR(A) = 3
23 * 	B = [2], AND(B) = 2
24 * 	C = [], XOR(C) = 0
25 * 
26 * The maximum value of: XOR(A) + AND(B) + XOR(C) = 3 + 2 + 0 = 5. Thus, the answer is 5.
27 * </div>
28 * <strong class="example">Example 2:
29 * <div class="example-block">
30 * Input: <span class="example-io">nums = [1,3,2]</span>
31 * Output: <span class="example-io">6</span>
32 * Explanation:
33 * One optimal partition is:
34 * 
35 * 	A = [1], XOR(A) = 1
36 * 	B = [2], AND(B) = 2
37 * 	C = [3], XOR(C) = 3
38 * 
39 * The maximum value of: XOR(A) + AND(B) + XOR(C) = 1 + 2 + 3 = 6. Thus, the answer is 6.
40 * </div>
41 * <strong class="example">Example 3:
42 * <div class="example-block">
43 * Input: <span class="example-io">nums = [2,3,6,7]</span>
44 * Output: <span class="example-io">15</span>
45 * Explanation:
46 * One optimal partition is:
47 * 
48 * 	A = [7], XOR(A) = 7
49 * 	B = [2,3], AND(B) = 2
50 * 	C = [6], XOR(C) = 6
51 * 
52 * The maximum value of: XOR(A) + AND(B) + XOR(C) = 7 + 2 + 6 = 15. Thus, the answer is 15.
53 * </div>
54 *  
55 * Constraints:
56 * 
57 * 	1 <= nums.length <= 19
58 * 	1 <= nums[i] <= 10^9
59 * 
60 */
61pub struct Solution {}
62
63// problem: https://leetcode.com/problems/partition-array-for-maximum-xor-and-and/
64// discuss: https://leetcode.com/problems/partition-array-for-maximum-xor-and-and/discuss/?currentPage=1&orderBy=most_votes&query=
65
66// submission codes start here
67
68impl Solution {
69    pub fn maximize_xor_and_xor(nums: Vec<i32>) -> i64 {
70        
71    }
72}
73
74// submission codes end
75
76#[cfg(test)]
77mod tests {
78    use super::*;
79
80    #[test]
81    fn test_3630() {
82    }
83}
84

Back
© 2026 bowen.ge All Rights Reserved.