1621. Two Window XOR Score

A sensor logs one 16-bit reading per tick in the array nums. An analyst may pick two non-empty, non-overlapping blocks of consecutive ticks: the first block is nums[l1..r1] and the second is nums[l2..r2], with r1 < l2. The score of a block is the bitwise XOR of all readings inside it, and the score of the pair is the sum of the two block scores.

Return the largest possible pair score as a long. The array always has at least two readings, so a valid pair exists.

Trying all four boundaries is far too slow. The intended solution sweeps left to right with a prefix-XOR trie to get the best block score ending at or before each index, sweeps right to left for the best block starting at or after each index, and combines the two across every split point. Target O(n * 16) time and O(n * 16) space.

Example 1

Input:
nums = [3,5]
Output:
8
Explanation:

The two single-element blocks give 3 + 5 = 8.

Example 2

Input:
nums = [7,7,7]
Output:
14
Explanation:

Taking [7] and [7] gives 7 + 7 = 14.

Example 3

Input:
nums = [1,2,3,4]
Output:
10
Explanation:

Trying the splits shows the maximum total is 10.

Constraints

2 ≤ n ≤ 105; 0 ≤ nums[i] < 216

How this problem is judged

Answers
Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.
Time per case
Python 6,000 msC++ 1,500 msJava 3,000 msJavaScript 3,000 msTypeScript 3,000 ms

Expected complexity

Time
O(n * 16)
Space
O(n * 16)

What the author was aiming for. Your own solution is not measured against it.

Asked in an interview

Were you asked this in an interview? Say where, anonymously.

Code
Loading the editor…