XOR Operation in an Array

Easy Bit Manipulation XOR Basics Original on LeetCode

XOR Operation in an Array is a easy bit manipulation problem solved with the xor basics pattern. The best approach, optimal (prefix xor in o(1)), runs in O(1) time and O(1) space. Below are 2 approaches in Java, Python, C++ and JavaScript, from brute force (simulation) up.

Problem

You are given two integers n and start. They describe an array nums of length n where nums[i] = start + 2 * i (0-indexed), so the values are start, start + 2, start + 4, ….

Return the bitwise XOR of all elements of nums.

Examples

Example 1

Input
n = 5, start = 0
Output
8
Why
The values are 0, 2, 4, 6, 8. 0 ^ 2 ^ 4 ^ 6 = 0, and 0 ^ 8 = 8.

Example 2

Input
n = 3, start = 3
Output
1
Why
The values are 3, 5, 7. 3 ^ 5 = 6 and 6 ^ 7 = 1.

Example 3

Input
n = 1, start = 9
Output
9
Why
A single value is its own XOR.

Constraints

  • 1 <= n <= 1000
  • 0 <= start <= 1000

Solve it here

Write your solution in Java, Python, C++ or JavaScript and run it against the examples, right in the page. Submit runs the hidden tests too and, with a free account, records the solve in your revision log.

00:00
Testcase

Run checks the testcases above (3 examples, or your own). Submit also runs 14 hidden tests and saves the solve to your log. Signed in, every Run and Submit is saved under My submissions; your draft stays in this browser.

Solutions

Try it yourself first. Then compare: each approach lists its idea, the steps, its time and space, and the code in Java, Python, C++ and JavaScript.

ApproachTimeSpace
Brute force (simulation)O(n)O(1)
Optimal (prefix XOR in O(1))O(1)O(1)

1Brute force (simulation)

TimeO(n)One XOR per value.
SpaceO(1)Just the accumulator.

Generate each value start + 2 * i and XOR it into an accumulator.

  1. res = 0.
  2. For i from 0 to n - 1: res ^= start + 2 * i.
  3. Return res.
class Solution {
    public int xorOperation(int n, int start) {
        int res = 0;
        for (int i = 0; i < n; i++) {
            res ^= start + 2 * i;
        }
        return res;
    }
}

2Optimal (prefix XOR in O(1))

TimeO(1)Two lookups of the 4-cycle formula.
SpaceO(1)A few integers.

Write each value as 2 * (h + i) + b with h = start >> 1 and b = start & 1. The high parts are the consecutive integers h .. h + n - 1, whose XOR is f(h + n - 1) ^ f(h - 1) where f(x) is the XOR of 0..x (it repeats every 4). The low bit is b XOR-ed n times, so it survives only when both b and n are odd.

  1. f(x): if x < 0 return 0; by x mod 4 return x, 1, x + 1 or 0.
  2. h = start >> 1; high = f(h + n - 1) ^ f(h - 1).
  3. low = (start & 1) & (n & 1).
  4. Return (high << 1) | low.
class Solution {
    public int xorOperation(int n, int start) {
        int h = start >> 1;
        int high = xorUpTo(h + n - 1) ^ xorUpTo(h - 1);
        int low = (start & 1) & (n & 1);
        return (high << 1) | low;
    }

    private int xorUpTo(int x) {
        if (x < 0) return 0;
        switch (x % 4) {
            case 0: return x;
            case 1: return 1;
            case 2: return x + 1;
            default: return 0;
        }
    }
}

Edge cases to test

  • n = 1: the answer is start
  • start = 0
  • Odd start: every value is odd, so the lowest bit of the answer depends on whether n is odd

Hints

Hint 1

The direct way: build each value start + 2·i and XOR it into a running result.

Hint 2

For an O(1) answer, split each value into its lowest bit and the rest. The rest is 2 × (start/2 + i), a run of consecutive integers.

Hint 3

The XOR of 0..x follows a cycle of 4: x, 1, x + 1, 0 for x mod 4 = 0, 1, 2, 3.

FAQ

What is the best time complexity for XOR Operation in an Array?

Optimal (prefix XOR in O(1)) runs in O(1) time and O(1) extra space. Two lookups of the 4-cycle formula.

Which pattern does XOR Operation in an Array use?

It is a bit manipulation problem that uses the xor basics pattern. Other problems with the same pattern: Swap two numbers, Hamming Distance, Count Paths That Can Form a Palindrome in a Tree.

Is there a brute force solution for XOR Operation in an Array?

Yes. Brute force (simulation) takes O(n) time and O(1) space. Generate each value start + 2 i and XOR it into an accumulator.

Which edge cases should I test for XOR Operation in an Array?

n = 1: the answer is start; start = 0; Odd start: every value is odd, so the lowest bit of the answer depends on whether n is odd.