Partition Array for Maximum XOR and AND

You are given an integer array nums.

Partition the array into three possibly empty subsequences A, B, and C such that every element of nums belongs to exactly one subsequence.

Your goal is to maximize the value of XOR(A) + AND(B) + XOR(C), where:

  • XOR(arr) denotes the bitwise XOR of all elements in arr; if arr is empty, its value is defined as 0.
  • AND(arr) denotes the bitwise AND of all elements in arr; if arr is empty, its value is defined as 0.

Return the maximum value achievable.

Note: If multiple partitions result in the same maximum sum, you can consider any one of them.

Example 1
Inputnums = [2,3]
Output5
One optimal partition is A = [3], B = [2], and C = [], giving 3 + 2 + 0 = 5.
Example 2
Inputnums = [1,3,2]
Output6
One optimal partition is A = [1], B = [2], and C = [3], giving 1 + 2 + 3 = 6.

Constraints

  • 1 <= nums.length <= 19
  • 1 <= nums[i] <= 10^9

Asked at 1 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate