[LeetCode]#1486. XOR Operation in an Array

Input: n = 5, start = 0
Output: 8
Explanation: Array nums is equal to [0, 2, 4, 6, 8] where (0 ^ 2 ^ 4 ^ 6 ^ 8) = 8.
Where "^" corresponds to bitwise XOR operator.
  1. num=[0,2,4,6,8]
  2. Use XOR logic and answer is 8.
class Solution:
def xorOperation(self, n, start):
ans=0
for i in range(n):
ans^=(start+i*2)

return ans

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Fatboy Slim

Fatboy Slim

Interesting in any computer science.