Skip to content

810. Chalkboard XOR Game 👎

  • Time: $O(n)$
  • Space: $O(1)$
1
2
3
4
5
6
7
class Solution {
 public:
  bool xorGame(vector<int>& nums) {
    const int xors = accumulate(nums.begin(), nums.end(), 0, bit_xor<int>());
    return xors == 0 || nums.size() % 2 == 0;
  }
};
1
2
3
4
5
6
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;
  }
}
1
2
3
class Solution:
  def xorGame(self, nums: list[int]) -> bool:
    return functools.reduce(operator.xor, nums) == 0 or len(nums) % 2 == 0