FastPrepMaximize XOR-AND-XOR over Three Subsequences

Maximize XOR-AND-XOR over Three Subsequences

Google logoGoogle● HardINTERNONSITE INTERVIEW
Learn

Problem statement

You are given an array nums of non-negative integers. Partition its elements into three possibly empty subsequences A, B, and C. Every element must belong to exactly one subsequence.

For an empty subsequence, both its bitwise XOR and its bitwise AND are defined as 0. Return the maximum possible value of XOR(A) + AND(B) + XOR(C).

Function

maximumPartitionValue(nums: int[]) → long

Examples

Example 1

nums = [1,2,3]return = 6

Choose A = [1,2], B = [3], and C = []. The value is (1 XOR 2) + 3 + 0 = 6.

Example 2

nums = [7,7]return = 14

Put one 7 in A and the other in B. This gives 7 + 7 + 0 = 14.

Example 3

nums = [5,1,9,3]return = 16

One optimal partition is A = [5,1,9], B = [3], and C = []. Its value is 13 + 3 + 0 = 16.

Constraints

  • 0 <= nums.length <= 15.
  • 0 <= nums[i] <= 1000000000.

More Google problems

See Google hiring insights
public long maximumPartitionValue(int[] nums) {
    // Write your code here.
}
nums[1,2,3]
expected6
Checking account…