Data Structures & AlgorithmsHardCodeforces Div2 C · prefix XOR

Problem: What is the maximum XOR of any contiguous subarray of [8, 1, 2, 12]? Run the brute force.

Problem: What is the maximum XOR of any contiguous subarray of [8, 1, 2, 12]? Run the brute force.
a = [8, 1, 2, 12]
best = 0
for i in range(len(a)):
    x = 0
    for j in range(i, len(a)):
        x ^= a[j]
        best = max(best, x)
print(best)
What is printed?
  1. A. 12
  2. B. 15
  3. C. 14
  4. D. 10

Correct answer: B. 15

The subarray [1, 2, 12] has XOR 1 ^ 2 ^ 12 = 15, which is the largest value over all contiguous subarrays.

More Coding Interview Problems questions

Exams that ask this topic