Member-only story
Leetcode 1486: XOR Operation in an Array
1 min readJun 24, 2020
In this Leetcode problem, we compute
start XOR start + 2 XOR ... XOR start + 2n
This is the problem statement:
Solution
To resolve the problem, simply compute the result with brute force, here is the full code:
class Solution {
public int xorOperation(int n, int start) {
int res = 0;
int curr = start;
for (int i = 0; i < n; i++) {
res ^= curr;
curr += 2;
}
return res;
}
}
Happy Coding :)