All problems
0925MediumArrayBit Manipulation

Longest Pick With a Non-Zero Combination

Tracked in this browser only
Write code

Trains the technique from

LeetCode 3702Longest Subsequence With Non-Zero Bitwise XOR

This 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.

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 is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.

Python
def longest_subsequence(nums: list[int]) -> int:
Java
public int longestSubsequence(int[] nums)
September 7
Apply