Intuition¶
XOR of a set of numbers is zero when the bits cancel out perfectly. Look at the XOR of the entire array:
- If the total XOR is already non-zero, the whole array is the answer — you can't do better than taking every element.
- If the total XOR is zero, taking all elements fails, but dropping a single
non-zero element leaves a subsequence whose XOR equals that dropped value
(non-zero). So the answer is
n - 1, provided at least one non-zero element exists. - If every element is zero, any subsequence XORs to zero, so no valid subsequence
exists and the answer is
0.
Approach: XOR Parity Check¶
- Compute
totalXorover all elements and track whether the array is all zeroes. - If
totalXor != 0, returnn(the full array works). - Otherwise, if the array is all zeroes, return
0(impossible). - Otherwise return
n - 1(drop one non-zero element to break the cancellation).
Complexity¶
- Time complexity: $$O(n)$$, where
nis the length ofnums— a single pass. - Space complexity: $$O(1)$$.
Code¶
Go¶
func longestSubsequence(nums []int) int {
totalXor := 0
hasAllZeroes := true
for _, num := range nums {
totalXor ^= num
if num > 0 {
hasAllZeroes = false
}
}
if totalXor > 0 {
return len(nums)
}
if hasAllZeroes {
return 0
}
return len(nums) - 1
}