Show problems as
Classic problem #3702MediumArrayBit Manipulation

Longest Subsequence With Non-Zero Bitwise XOR

Real-world version: Longest Pick With a Non-Zero Combination
Tracked in this browser only
Read Longest Subsequence With Non-Zero Bitwise XOR on LeetCode(opens in a new tab)

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

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