810. Chalkboard XOR Game Hard

@problem@discussion
#Array#Math#Bit Manipulation#Brainteaser#Game Theory



1/**
2 * [810] Chalkboard XOR Game
3 *
4 * You are given an array of integers nums represents the numbers written on a chalkboard.
5 * Alice and Bob take turns erasing exactly one number from the chalkboard, with Alice starting first. If erasing a number causes the bitwise XOR of all the elements of the chalkboard to become 0, then that player loses. The bitwise XOR of one element is that element itself, and the bitwise XOR of no elements is 0.
6 * Also, if any player starts their turn with the bitwise XOR of all the elements of the chalkboard equal to 0, then that player wins.
7 * Return true if and only if Alice wins the game, assuming both players play optimally.
8 *  
9 * Example 1:
10 * 
11 * Input: nums = [1,1,2]
12 * Output: false
13 * Explanation: 
14 * Alice has two choices: erase 1 or erase 2. 
15 * If she erases 1, the nums array becomes [1, 2]. The bitwise XOR of all the elements of the chalkboard is 1 XOR 2 = 3. Now Bob can remove any element he wants, because Alice will be the one to erase the last element and she will lose. 
16 * If Alice erases 2 first, now nums become [1, 1]. The bitwise XOR of all the elements of the chalkboard is 1 XOR 1 = 0. Alice will lose.
17 * 
18 * Example 2:
19 * 
20 * Input: nums = [0,1]
21 * Output: true
22 * 
23 * Example 3:
24 * 
25 * Input: nums = [1,2,3]
26 * Output: true
27 * 
28 *  
29 * Constraints:
30 * 
31 * 	1 <= nums.length <= 1000
32 * 	0 <= nums[i] < 2^16
33 * 
34 */
35pub struct Solution {}
36
37// problem: https://leetcode.com/problems/chalkboard-xor-game/
38// discuss: https://leetcode.com/problems/chalkboard-xor-game/discuss/?currentPage=1&orderBy=most_votes&query=
39
40// submission codes start here
41
42impl Solution {
43    pub fn xor_game(nums: Vec<i32>) -> bool {
44        false
45    }
46}
47
48// submission codes end
49
50#[cfg(test)]
51mod tests {
52    use super::*;
53
54    #[test]
55    fn test_810() {
56    }
57}
58


Back
© 2025 bowen.ge All Rights Reserved.