XOR Operation in an Array - Problem
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.
Input & Output
Example 1 — Basic Case
$
Input:
n = 5, start = 0
›
Output:
8
💡 Note:
Array is [0,2,4,6,8]. XOR: 0⊕2⊕4⊕6⊕8 = 8
Example 2 — Different Start
$
Input:
n = 4, start = 3
›
Output:
8
💡 Note:
Array is [3,5,7,9]. XOR: 3⊕5⊕7⊕9 = 8
Example 3 — Single Element
$
Input:
n = 1, start = 7
›
Output:
7
💡 Note:
Array is [7]. XOR of single element is itself: 7
Constraints
- 1 ≤ n ≤ 1000
- 0 ≤ start ≤ 1000
Visualization
Tap to expand
Understanding the Visualization
1
Input
n=5, start=0 - generate 5 elements starting from 0
2
Generate Sequence
Create [0,2,4,6,8] using formula start + 2*i
3
XOR All
0⊕2⊕4⊕6⊕8 = 8
Key Takeaway
🎯 Key Insight: Generate arithmetic sequence with formula start + 2*i, then XOR all elements efficiently
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code