Trains the technique from
LeetCode 3702Longest Subsequence With Non-Zero Bitwise XORThis is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.
Same function shape, different story and different numbers.
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.
Example 1
The four values combine to something other than zero, so the whole list is the longest pick.
Example 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
Every value is zero, so every pick combines to zero and there is no answer.
The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.
def longest_subsequence(nums: list[int]) -> int:public int longestSubsequence(int[] nums)See the step-by-step animation, the intuition, and clean code in every language — free, no credit card.