Skip to main content
Back to problems
#1486
Easy Algorithms

Xor operation in an array

Math Bit Manipulation
87.4% acceptance
Feb 25, 2026
1502
340
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.

Solution

Rust
Time O(1)
Space O(1)
LeetCode
solution.rs
impl Solution {
  pub fn xor_operation(n: i32, start: i32) -> i32 {
    (0..n).fold(0, |acc, i| acc ^ (start + 2 * i))
  }
}