Update Logs by Symmetric XOR
Learn this problemProblem statement
You are given an integer array log of size n and a non-negative integer iterations.
The updates are applied sequentially and in-place. For each iteration i from 0 to iterations - 1:
- Compute
index = i mod n. - Compute
symmetric = n - index - 1. - Set
log[index] = log[index] XOR log[symmetric], using the current array values at that moment.
If index == symmetric, the element is updated to 0 because x XOR x = 0. Return the final array after all iterations are complete. Since iterations can be very large, solutions should reason about the repeated index cycle instead of simulating every iteration one by one.
Function
updateLogsBySymmetricXor(log: int[], iterations: long) → int[]Examples
Example 1
log = [1, 2, 3]iterations = 2return = [2, 0, 3]Iteration 0 updates index 0: 1 XOR 3 = 2, so the array becomes [2,2,3]. Iteration 1 updates the middle index, where index == symmetric, so 2 XOR 2 = 0. The result is [2,0,3].
Example 2
log = [4, 7, 1, 2]iterations = 5return = [2, 6, 7, 4]The updates are in-place at indices 0,1,2,3,0. The array changes as [4,7,1,2] -> [6,7,1,2] -> [6,6,1,2] -> [6,6,7,2] -> [6,6,7,4] -> [2,6,7,4].
Constraints
1 <= log.length <= 2 * 10^50 <= log[i] <= 10^90 <= iterations <= 10^18