XOR Operation in an Array

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.

30:00

XOR Operation in an Array
easy
Free written solution
Topics
Companies

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.

Example 1:
Input: {"n":5,"start":0}
Output: 8
Constraints:
  • 1≤n≤10001 \leq n \leq 1000

  • 0≤start≤10000 \leq start \leq 1000

  • n==nums.lengthn == nums.length

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"n":5,"start":0}

nums holds 5 values: 0, 2, 4, ... each 2 apart. Start with result = 0, since x ^ 0 = x.

nums[i] = 0 + 2·i

folded 0 / 5

0

0

2

1

4

2

6

3

8

4

XOR flips each column where num has a 1

result
^ num
= result
0
0
0
0
8
4
2
1

before = 0

result = 0

Variables
VariableValue
n5
start0
result0
DepthFunction Call
Stack empty
0/11