Skip to content

Commit 2106cd1

Browse files
Sync LeetCode submission Runtime - 0 ms (100.00%), Memory - 7.7 MB (80.35%)
1 parent 48c032b commit 2106cd1

2 files changed

Lines changed: 40 additions & 0 deletions

File tree

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<p>You are given an integer <code>n</code> and an integer <code>start</code>.</p>
2+
3+
<p>Define an array <code>nums</code> where <code>nums[i] = start + 2 * i</code> (<strong>0-indexed</strong>) and <code>n == nums.length</code>.</p>
4+
5+
<p>Return <em>the bitwise XOR of all elements of</em> <code>nums</code>.</p>
6+
7+
<p>&nbsp;</p>
8+
<p><strong class="example">Example 1:</strong></p>
9+
10+
<pre>
11+
<strong>Input:</strong> n = 5, start = 0
12+
<strong>Output:</strong> 8
13+
<strong>Explanation:</strong> Array nums is equal to [0, 2, 4, 6, 8] where (0 ^ 2 ^ 4 ^ 6 ^ 8) = 8.
14+
Where &quot;^&quot; corresponds to bitwise XOR operator.
15+
</pre>
16+
17+
<p><strong class="example">Example 2:</strong></p>
18+
19+
<pre>
20+
<strong>Input:</strong> n = 4, start = 3
21+
<strong>Output:</strong> 8
22+
<strong>Explanation:</strong> Array nums is equal to [3, 5, 7, 9] where (3 ^ 5 ^ 7 ^ 9) = 8.
23+
</pre>
24+
25+
<p>&nbsp;</p>
26+
<p><strong>Constraints:</strong></p>
27+
28+
<ul>
29+
<li><code>1 &lt;= n &lt;= 1000</code></li>
30+
<li><code>0 &lt;= start &lt;= 1000</code></li>
31+
<li><code>n == nums.length</code></li>
32+
</ul>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class Solution {
2+
public:
3+
int xorOperation(int n, int start) {
4+
int ans = 0;
5+
for (int i=0; i<n; i++) ans ^= start + 2 * i;
6+
return ans;
7+
}
8+
};

0 commit comments

Comments
 (0)