Talentd
FleetCode

3702. Longest Subsequence With Non-Zero Bitwise XOR

Medium55.1% acceptance

You are given an integer array nums.

Return the length of the longest subsequence in nums whose bitwise XOR is non-zero. If no such subsequence exists, return 0.

Example 1:

Input: nums = [1,2,3]

Output: 2

Explanation:

One longest subsequence is [2, 3]. The bitwise XOR is computed as 2 XOR 3 = 1, which is non-zero.

Example 2:

Input: nums = [2,3,4]

Output: 3

Explanation:

The longest subsequence is [2, 3, 4]. The bitwise XOR is computed as 2 XOR 3 XOR 4 = 5, which is non-zero.

Constraints:

  • 1 <= nums.length <= 105
  • 0 <= nums[i] <= 109

Hints open one at a time

Hint 1
What happens if you take the entire array?
Hint 2
If the XOR of the entire array is 0, can removing one element help?
Hint 3
What if all elements are 0?
7:39
Still stuck?Read the written solution, or watch a walkthrough.
Open solution
Loading editor…
1class Solution:
2 def longestSubsequence(self, nums: List[int]) -> int:
3
Starter codePython 3Spaces: 4
Examples
Input
[1,2,3]
Input
[2,3,4]
Your code is saved on this device.Solution