DDSA Solutions

1486. XOR Operation in an Array

Time: O(n)
Space: O(1)

Problem Overview

XOR Operation in an Array (Easy) asks you to solve a structured algorithmic task. This is a common Array / Math pattern in coding interviews. XOR all elements of the sequence start, start+2, start+4, ..., start+2*(n-1).

A full step-by-step explanation is being added. See the study guide for pattern-based practice.

Approach

XOR all elements of the sequence start, start+2, start+4, ..., start+2*(n-1).

Related patterns: Array, Math, Bit Manipulation

1486.cs
C#
// Approach: XOR all elements of the sequence start, start+2, start+4, ..., start+2*(n-1).
// Time: O(n) Space: O(1)

public 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;
    }
}
Was this solution helpful?

Related Problems