1486. XOR Operation in an Array
Approach
XOR all elements of the sequence start, start+2, start+4, ..., start+2*(n-1).
Key Techniques
Array problems involve manipulating elements stored in a contiguous block of memory. Key techniques include two-pointer traversal, prefix sums, sliding windows, and in-place partitioning. In C#, arrays are zero-indexed and fixed in size — use List<T> when you need dynamic resizing.
Math problems test number theory, combinatorics, and modular arithmetic. Common tools: GCD/LCM (Euclidean algorithm), prime sieve, modular inverse (Fermat's little theorem), digit manipulation, and bit tricks. Overflow is a key concern in C# — use long when products may exceed 2³¹.
Bit manipulation uses bitwise operators (&, |, ^, ~, <<, >>) for compact and fast solutions. Key tricks: x & (x-1) clears lowest set bit, x ^ x = 0 (XOR cancellation), and bitmask DP represents subsets as integers. In C#, use int (32-bit) or long (64-bit) for bitmasking.
// 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;
}
}