1486. XOR Operation in an Array LeetCode Solution
In this guide, you will get 1486. XOR Operation in an Array LeetCode Solution with the best time and space complexity. The solution to XOR Operation in an Array problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- XOR Operation in an Array solution in C++
- XOR Operation in an Array solution in Java
- XOR Operation in an Array solution in Python
- Additional Resources

Problem Statement of XOR Operation in an Array
You are given an integer n and an integer start.
Define an array nums where nums[i] = start + 2 * i (0-indexed) and n == nums.length.
Return the bitwise XOR of all elements of nums.
Example 1:
Input: n = 5, start = 0
Output: 8
Explanation: Array nums is equal to [0, 2, 4, 6, 8] where (0 ^ 2 ^ 4 ^ 6 ^ 8) = 8.
Where “^” corresponds to bitwise XOR operator.
Example 2:
Input: n = 4, start = 3
Output: 8
Explanation: Array nums is equal to [3, 5, 7, 9] where (3 ^ 5 ^ 7 ^ 9) = 8.
Constraints:
1 <= n <= 1000
0 <= start <= 1000
n == nums.length
Complexity Analysis
- Time Complexity: O(n)
- Space Complexity: O(1)
1486. XOR Operation in an Array LeetCode Solution in C++
class Solution {
public:
int xorOperation(int n, int start) {
int ans = 0;
for (int i = 0; i < n; ++i)
ans ^= start + 2 * i;
return ans;
}
};
/* code provided by PROGIEZ */
1486. XOR Operation in an Array LeetCode Solution in Java
class Solution {
public int xorOperation(int n, int start) {
int ans = 0;
for (int i = 0; i < n; ++i)
ans ^= start + 2 * i;
return ans;
}
}
// code provided by PROGIEZ
1486. XOR Operation in an Array LeetCode Solution in Python
class Solution:
def xorOperation(self, n: int, start: int) -> int:
return functools.reduce(operator.xor,
[start + 2 * i for i in range(n)])
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.