Uber SDE-1 | 65LPA CTC | Please Help | DP + Xor
Anonymous User
1095

You are given:

An integer array A of length N
An integer X

You may perform the following operation at most once:

Select any subsequence of indices from the array.
Add X to every selected element.

A subsequence is not required to be contiguous.

After performing the operation, define the score as:

(A1 xor A2) + (A2 xor A3) + ... + (A(N-1) xor AN)

Your task is to maximize this score.

Example

Input:

A = [2, 1, 6, 3, 5]
X = 7

One possible operation:

Select subsequence [2, 3, 5]

New array becomes:

[9, 1, 6, 10, 12]

Score becomes:

(9 xor 1) + (1 xor 6) + (6 xor 10) + (10 xor 12)

Comments (3)