XOR Operation in an Array (LeetCode 1486): Solution and Explanation

Easy
Math
Bit Manipulation

Build the values start, start + 2, start + 4, ... (n of them) and return the XOR of all of them. For n = 5, start = 0 the values are 0, 2, 4, 6, 8 and their XOR is 8.

Intuition

The array is fully described by its formula, so there is no reason to store it. Element i is start + 2 * i, which can be computed when it is needed.

XOR folds a list into one value: start with 0, which leaves any number unchanged, and apply result ^= value for each element. In binary, each XOR flips exactly the columns where the incoming value has a 1, so a bit of the final result is 1 when an odd number of values had a 1 in that column.

With n at most 1000 the loop is already instant, but the same view of columns leads to a constant time formula, described under alternatives.

Approach: Fold the generated values

  1. Set result = 0.
  2. For i from 0 to n - 1, compute num = start + 2 * i.
  3. Apply result ^= num.
  4. Return result.

Step-by-step example

n = 5, start = 0
  1. result = 0.
  2. i = 0: num = 0, result = 0 ^ 0 = 0.
  3. i = 1: num = 2 (010), result = 2.
  4. i = 2: num = 4 (100), result = 010 ^ 100 = 110 = 6.
  5. i = 3: num = 6 (110), result = 110 ^ 110 = 0.
  6. i = 4: num = 8 (1000), result = 8. Return 8.

Code: Python, Java, C++

1class Solution:
2    def xorOperation(self, n: int, start: int) -> int:
3        result = 0
4        for i in range(n):
5            num = start + 2 * i
6            result ^= num
7        return result
Press play on the visualizer and the running line lights up here.

Time and space complexity

Work as the input growsinput size n →steps
Closed form with XOR of a range O(1)
Fold the generated values O(n)
Build the array then reduce O(n)
Time

O(n)

One addition and one XOR per element.

Space

O(1)

The array is never built; only result and the loop index are kept.

Other approaches

Closed form with XOR of a range (time O(1), space O(1))

All values share the lowest bit of start, so that bit of the answer is start & n & 1. Shifting everything right by one leaves the consecutive integers s, s + 1, ..., s + n - 1 with s = start >> 1, whose XOR comes from the pattern of XOR(0..k) (k, 1, k + 1, 0 by k mod 4). Shift that back left and add the low bit.

Build the array then reduce (time O(n), space O(n))

Create nums explicitly and XOR its elements. Same result with O(n) extra memory and no benefit.

Common mistakes

  • Starting result at 1 or at nums[0] and then also XORing nums[0] again, which cancels it.
  • Writing start + 2 * i with operator precedence mistakes such as (start + 2) * i.
  • In the closed form, forgetting the low bit: it is 1 only when start is odd and n is odd.
  • Using ^ as exponentiation in languages where it means XOR, or the reverse.

Frequently asked questions

Why start the result at 0?

0 is the identity for XOR: x ^ 0 = x. Starting there means the first element passes through unchanged.

Does the order of the XORs matter?

No. XOR is associative and commutative, so any order gives the same final value.

Why can the result be 0 even though every value is positive?

Each column flips once per value that has a 1 there. When every column is flipped an even number of times, all bits return to 0, as with 0, 2, 4, 6.