Given an integer n and an integer start.
Define an array nums where nums[i] = start + 2*i (0-indexed) and n == nums.length.
Your task is to return the bitwise XOR of all elements of nums.
n = 5, start = 080 ^ 2 ^ 4 ^ 6 ^ 8 which is 8.1 <= n <= 10000 <= start <= 1000start.result = 0.[0, n-1].nums[i] = start + 2*i.result with nums[i].result variable will hold the desired XOR value.def xorOperation(n: int, start: int) -> int:
result = 0
for i in range(n):
result ^= start + 2 * i
return result
# Example usage
print(xorOperation(5, 0)) # Output: 8
print(xorOperation(4, 3)) # Output: 8
print(xorOperation(1, 7)) # Output: 7
print(xorOperation(10, 5)) # Output: 2
The time complexity of this algorithm is O(n) because we are iterating through the array only once, performing constant-time operations for each element.
Got blindsided by a question you didn’t expect?
Spend too much time studying?
Or simply don’t have the time to go over all 3000 questions?