Longest Subsequence With Non-Zero Bitwise XOR
Real-world version: Longest Pick With a Non-Zero CombinationThe statement here is our own: the same inputs, outputs and technique.
Register values are given as nums. A pick takes some of them, keeping their order, and its combination is the bitwise exclusive-or of everything picked.
Return how many values the longest pick with a non-zero combination holds, or 0 when no pick has one.
Examples
Example 1
- Input
- nums = [13, 27, 6, 41]
- Output
- 4
The four values combine to something other than zero, so the whole list is the longest pick.
Example 2
- Input
- nums = [6, 10, 12]
- Output
- 2
All three combine to zero, so dropping one is the best available, and dropping any of them leaves that value behind as the combination, which is not zero.
Example 3
- Input
- nums = [0, 0, 0, 0]
- Output
- 0
Every value is zero, so every pick combines to zero and there is no answer.
Constraints
- 1 <= nums.length <= 10^5
- 0 <= nums[i] <= 10^9
The signature
The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.
def longest_subsequence(nums: list[int]) -> int:public int longestSubsequence(int[] nums)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 12 test cases: 12 passed
- Passed: 3 worked examples confirmed by running the code
- Passed: 0 eight-word sequences shared with the parent problem's text
- Passed: Agrees with an independent brute-force solution on random inputs
- Passed: Test set shown to catch deliberately broken solutions
- Passed: Reviewed by a separate quality pass
- Passed: Function arity matches the parent problem's