Problem · Array

Maximum XOR Elimination Score

Learn this problem
HardRipplingOA

Problem statement

You are given an integer array nums. Initialize score = 0.

While more than one number remains:

  1. Choose two remaining numbers x and y.
  2. Add x XOR y to score.
  3. Remove either x or y; the other chosen number remains unchanged.

Return the maximum score obtainable when exactly one number remains.

Function

maximumXorScore(nums: int[]) → long

Examples

Example 1

nums = [1, 2, 3, 4, 5]return = 30

An optimal sequence accumulates a total XOR score of 30.

Example 2

nums = [1, 2, 4]return = 11

Choose 2 and 4 for a score of 6 and remove 2. Then choose 1 and 4 for 5 more, giving 11.

Constraints

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

More Rippling problems

drafts saved locally
public long maximumXorScore(int[] nums) {
  // write your code here
}
nums[1, 2, 3, 4, 5]
expected30
checking account