XOR Operation in an Array
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 <= 10000 <= 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.
Your own testcases: Run checks these, and the expected output comes from our reference solution.
My submissions
Nothing yet. Every Run and Submit you make while signed in shows up here.
| When | Result | Tests | Runtime | Your time | Type |
|---|
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.
| Approach | Time | Space |
|---|---|---|
| Brute force (simulation) | O(n) | O(1) |
| Optimal (prefix XOR in O(1)) | O(1) | O(1) |
1Brute force (simulation)
O(n)One XOR per value.O(1)Just the accumulator.Generate each value start + 2 * i and XOR it into an accumulator.
- res = 0.
- For i from 0 to n - 1: res ^= start + 2 * i.
- 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;
}
}class Solution:
def xorOperation(self, n: int, start: int) -> int:
res = 0
for i in range(n):
res ^= start + 2 * i
return resclass Solution {
public:
int xorOperation(int n, int start) {
int res = 0;
for (int i = 0; i < n; i++) {
res ^= start + 2 * i;
}
return res;
}
};function xorOperation(n, start) {
let res = 0;
for (let i = 0; i < n; i++) {
res ^= start + 2 * i;
}
return res;
}2Optimal (prefix XOR in O(1))
O(1)Two lookups of the 4-cycle formula.O(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.
- f(x): if x < 0 return 0; by x mod 4 return x, 1, x + 1 or 0.
- h = start >> 1; high = f(h + n - 1) ^ f(h - 1).
- low = (start & 1) & (n & 1).
- 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;
}
}
}class Solution:
def xorOperation(self, n: int, start: int) -> int:
def xor_up_to(x: int) -> int:
if x < 0:
return 0
return [x, 1, x + 1, 0][x % 4]
h = start >> 1
high = xor_up_to(h + n - 1) ^ xor_up_to(h - 1)
low = (start & 1) & (n & 1)
return (high << 1) | lowclass Solution {
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;
}
}
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;
}
};function xorOperation(n, start) {
const xorUpTo = (x) => {
if (x < 0) return 0;
return [x, 1, x + 1, 0][x % 4];
};
const h = start >> 1;
const high = xorUpTo(h + n - 1) ^ xorUpTo(h - 1);
const low = (start & 1) & (n & 1);
return (high << 1) | low;
}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.