LeetCode Solutions

810. Chalkboard XOR Game

Time: $O(n)$

Space: $O(1)$

			

class Solution {
 public:
  bool xorGame(vector<int>& nums) {
    const int xors = accumulate(begin(nums), end(nums), 0, bit_xor<int>());
    return xors == 0 || nums.size() % 2 == 0;
  }
};
			

class Solution {
  public boolean xorGame(int[] nums) {
    final int xors = Arrays.stream(nums).reduce((a, b) -> a ^ b).getAsInt();
    return xors == 0 || nums.length % 2 == 0;
  }
}
			

class Solution:
  def xorGame(self, nums: List[int]) -> bool:
    return functools.reduce(operator.xor, nums) == 0 or len(nums) % 2 == 0