213. Maximize XOR Score After Adding X
Asked in
Maximize XOR Score After Adding X

You are given a list of integers and an integer x. You may perform one operation at most once: choose any set of indices and add x to every chosen element.

After the operation, the score is the sum of XOR values of every pair of adjacent elements. Return the maximum possible score.

Method Signature

long maximizeXorScore(List<Integer> nums, int x)

Parameters

  • nums is the list of integers.
  • x is the value added to every selected element.
  • The selected indices may be non-contiguous.
  • You may also select no index.

Score

For a final list b, the score is:

(b[0] xor b[1]) + (b[1] xor b[2]) + ... + (b[n - 2] xor b[n - 1])

Return Value

Return the maximum score as a long.

Constraints

  • 1 ≤ nums.size() ≤ 100,000
  • 0 ≤ nums[i] ≤ 1,000,000,000
  • 0 ≤ x ≤ 1,000,000,000

Examples

Example 1

maximizeXorScore(nums = [4, 2, 7], x = 3)

Output: 17

Choose indices 0, 1, and 2. After adding 3 to these indices, the list becomes [7, 5, 10].

The score is (7 xor 5) + (5 xor 10) = 2 + 15 = 17.

Example 2

maximizeXorScore(nums = [1, 5, 2, 8], x = 4)

Output: 41

Choose indices 0, 1, and 2. After adding 4 to these indices, the list becomes [5, 9, 6, 8].

The score is (5 xor 9) + (9 xor 6) + (6 xor 8) = 12 + 15 + 14 = 41.

Example 3

maximizeXorScore(nums = [3, 10, 6], x = 0)

Output: 21

Since x = 0, adding 0 does not change any element. The list remains [3, 10, 6].

The score is (3 xor 10) + (10 xor 6) = 9 + 12 = 21.

Example 4

maximizeXorScore(nums = [6], x = 10)

Output: 0

There is only one element, so there are no adjacent pairs. Therefore, the score is always 0.



Please use Laptop/Desktop or any other large screen to add/edit code.