Skip to content

1486. XOR Operation in an Array 👍

  • Time: $O(n)$
  • Space: $O(1)$
1
2
3
4
5
6
7
8
9
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;
  }
};
1
2
3
4
5
6
7
8
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;
  }
}
1
2
3
4
class Solution:
  def xorOperation(self, n: int, start: int) -> int:
    return functools.reduce(operator.xor,
                            [start + 2 * i for i in range(n)])