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?- A. 12
- B. 15
- C. 14
- 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
- Problem: Sort [1, 1, 2, 2, 2, 3] by frequency ascending, breaking ties by value ascending. What is printed? python from collections import…
- Problem: How many set bits are there in total across the numbers 0 to 7? Run the code. python print(sum(bin(i).count('1') for i in…
- Problem: How many integers in [1, 20] are powers of two? Run the code. python print(sum(1 for x in range(1, 21) if x & (x - 1) == 0)) What…
- Problem: How many integers in [1, 100] are divisible by 7 or by 11? Run the code. python print(sum(1 for x in range(1, 101) if x % 7 == 0…
- Problem: What is 2^100 mod 7? Run the code. python print(pow(2, 100, 7)) What is printed?
- Problem: What is the length of the shortest path from node 1 to node 5 in the undirected graph with edges (1,2), (2,3), (1,4), (4,5)…