{x}
blog image

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

Solution Explanation for LeetCode 1486: XOR Operation in an Array

This problem asks to calculate the bitwise XOR of all elements in an array nums, where nums[i] = start + 2 * i and the array's length is n.

Approach 1: Direct Simulation

The most straightforward approach is to iterate through the array, calculating each element using the formula start + 2 * i, and accumulating the XOR result.

Time Complexity: O(n), as we iterate through the array once. Space Complexity: O(1), as we only use a constant amount of extra space.

Code Implementation in Different Languages

Python

from functools import reduce
import operator
 
class Solution:
    def xorOperation(self, n: int, start: int) -> int:
        nums = [(start + 2 * i) for i in range(n)]  #create the nums array
        result = reduce(operator.xor, nums) #using reduce for XOR operation
        return result
 

The python code utilizes list comprehension to efficiently generate the nums array and then employs the reduce function from the functools module along with operator.xor to perform the XOR operation cumulatively. This approach is concise and readable.

Java

class Solution {
    public int xorOperation(int n, int start) {
        int xorResult = 0;
        for (int i = 0; i < n; i++) {
            xorResult ^= (start + 2 * i);
        }
        return xorResult;
    }
}

The Java solution uses a simple for loop to iterate and directly calculate the XOR. It's efficient and easy to understand.

C++

class Solution {
public:
    int xorOperation(int n, int start) {
        int xorResult = 0;
        for (int i = 0; i < n; ++i) {
            xorResult ^= (start + 2 * i);
        }
        return xorResult;
    }
};

The C++ code is very similar to the Java version, employing a for loop to perform the XOR operation iteratively.

Go

func xorOperation(n int, start int) int {
    xorResult := 0
    for i := 0; i < n; i++ {
        xorResult ^= (start + 2 * i)
    }
    return xorResult
}

The Go solution mirrors the Java and C++ implementations, iterating and XORing in a straightforward manner.

TypeScript

function xorOperation(n: number, start: number): number {
    let xorResult = 0;
    for (let i = 0; i < n; i++) {
        xorResult ^= (start + 2 * i);
    }
    return xorResult;
};

The TypeScript version is analogous to the Java, C++, and Go solutions, using a for loop for iteration and XOR calculation.

All these solutions achieve the same result with a time complexity of O(n) and a space complexity of O(1), making them efficient for the given constraints. While there might be more mathematically optimized solutions for specific XOR properties, the direct simulation is the easiest to understand and implement for this problem.