Skip to main content

Longest Subsequence With Non-Zero Bitwise XOR - Solution & Explanation

MediumArrayBit Manipulation7 min read
Practice this problem

Problem Statement

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

Approach Overview

Problem Overview: Given an integer array, select the longest subsequence whose bitwise XOR is not equal to 0. The challenge is recognizing how XOR behaves when elements are included or removed from a sequence.

Approach 1: Brute Force Subsequence Enumeration (O(2^n) time, O(1) space)

Generate every possible subsequence and compute its XOR. Track the maximum length among those whose XOR is non-zero. This can be done with recursion or bitmask enumeration where each mask represents a subsequence. For every mask, iterate through the array and XOR the selected elements. The approach quickly becomes infeasible because the number of subsequences grows as 2^n. It mainly serves as a conceptual baseline for understanding the problem.

Approach 2: XOR Observation / Brain Teaser (O(n) time, O(1) space)

The key insight comes from XOR properties. Compute the XOR of the entire array. If the result is already non-zero, the optimal subsequence is simply the entire array because adding more elements cannot increase the length. If the total XOR equals 0, removing exactly one element changes the XOR to that element's value because total_xor ^ nums[i] = nums[i]. As long as the removed element is non-zero, the remaining subsequence will have a non-zero XOR and length n - 1.

The only case where a non-zero XOR subsequence cannot exist is when every element in the array is 0. Any subsequence XOR will still be 0. In that scenario, the answer is 0. Otherwise, if the total XOR is zero but at least one number is non-zero, remove one non-zero element and return n - 1.

This solution relies purely on XOR algebra rather than complex data structures. You only need one pass to compute the total XOR and check whether all elements are zero. The algorithm works efficiently even for very large arrays because it performs constant-time operations per element.

Understanding XOR identities like a ^ a = 0 and x ^ 0 = x is essential for problems involving bit manipulation. Many interview problems involving subsequences or parity rely on similar observations. Since the input is simply processed sequentially, the logic fits naturally with common array traversal patterns.

Recommended for interviews: Interviewers expect the XOR observation approach. Starting with the brute force explanation demonstrates understanding of subsequences, but recognizing the XOR identity that reduces the problem to a simple check shows strong problem-solving intuition. The optimal solution runs in O(n) time with O(1) extra space.

Solution

If the bitwise XOR of all elements in the array is non-zero, then the entire array is the desired longest subsequence, with length equal to the array length.

If all elements in the array are zero, then there is no subsequence with non-zero bitwise XOR, so we return 0.

Otherwise, we can remove one non-zero element from the array to make the bitwise XOR of the remaining elements non-zero. The length of the longest subsequence is the array length minus 1.

The time complexity is O(n), where n is the length of array nums. The space complexity is O(1).

Code

Python

Java

C++

Go

TypeScript

Try this approach in the editor →

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Brute Force Subsequence EnumerationO(2^n)O(1)Conceptual understanding or very small arrays
XOR Observation (Brain Teaser)O(n)O(1)Optimal solution for all input sizes

Video Solution

Longest Subsequence With Non-Zero Bitwise XOR | LeetCode 3702 | Weekly Contest 470 • Sanyam IIT Guwahati • 1,012 views views

Watch 9 more video solutions →

Frequently Asked Questions

Is Longest Subsequence With Non-Zero Bitwise XOR easy or hard?
The problem is typically rated Medium because the trick is recognizing the XOR identity rather than brute forcing subsequences. Once the XOR property is understood, the implementation becomes straightforward with O(n) time and constant space.
Longest Subsequence With Non-Zero Bitwise XOR Python/Java solution
The implementation is very short in Python, Java, C++, Go, or TypeScript. Compute the XOR of all elements in one loop, check if the result is zero, and verify whether all numbers are zero. Based on those checks, return n, n-1, or 0 accordingly.
How to solve Longest Subsequence With Non-Zero Bitwise XOR in O(n)?
First compute the XOR of all elements in the array. If the result is non-zero, return the array length n. If the total XOR equals zero, remove one non-zero element so the remaining XOR becomes that element's value, producing a subsequence of length n-1. If all elements are zero, no valid subsequence exists and the answer is 0.
What is the best approach for Longest Subsequence With Non-Zero Bitwise XOR?
The optimal approach uses a simple XOR observation. Compute the XOR of the entire array. If it is non-zero, the longest valid subsequence is the full array. If it equals zero, removing any non-zero element makes the remaining XOR non-zero, giving a subsequence of length n-1. This runs in O(n) time and O(1) space.
Is Longest Subsequence With Non-Zero Bitwise XOR asked at Google/Amazon/Meta?
Problems involving XOR properties and subsequences appear frequently in interviews at companies like Google, Amazon, and Meta. While this exact question may vary, the XOR reasoning pattern and bit manipulation insights are commonly tested in medium-level coding interviews.
What data structure is used in Longest Subsequence With Non-Zero Bitwise XOR?
The optimal solution does not require advanced data structures. It uses simple array traversal and bitwise XOR operations. Understanding bit manipulation identities is more important than using extra storage or complex structures.
What is the time complexity of Longest Subsequence With Non-Zero Bitwise XOR?
The optimal solution runs in O(n) time because you only scan the array once to compute the total XOR and check for non-zero elements. The space complexity is O(1) since only a few variables are used regardless of input size.

Ready to solve this problem?

Practice Longest Subsequence With Non-Zero Bitwise XOR with our built-in code editor and test cases.

Practice on FleetCode