Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
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 <= 1050 <= nums[i] <= 109Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Bit Manipulation
[1,2,3]
[2,3,4]
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3702: Longest Subsequence With Non-Zero Bitwise XOR
class Solution {
public int longestSubsequence(int[] nums) {
int xor = 0, cnt0 = 0;
int n = nums.length;
for (int x : nums) {
xor ^= x;
cnt0 += x == 0 ? 1 : 0;
}
if (xor != 0) {
return n;
}
return cnt0 == n ? 0 : n - 1;
}
}
// Accepted solution for LeetCode #3702: Longest Subsequence With Non-Zero Bitwise XOR
func longestSubsequence(nums []int) int {
var xor, cnt0 int
for _, x := range nums {
xor ^= x
if x == 0 {
cnt0++
}
}
n := len(nums)
if xor != 0 {
return n
}
if cnt0 == n {
return 0
}
return n - 1
}
# Accepted solution for LeetCode #3702: Longest Subsequence With Non-Zero Bitwise XOR
class Solution:
def longestSubsequence(self, nums: List[int]) -> int:
n = len(nums)
xor = cnt0 = 0
for x in nums:
xor ^= x
cnt0 += int(x == 0)
if xor:
return n
if cnt0 == n:
return 0
return n - 1
// Accepted solution for LeetCode #3702: Longest Subsequence With Non-Zero Bitwise XOR
// Rust example auto-generated from java reference.
// Replace the signature and local types with the exact LeetCode harness for this problem.
impl Solution {
pub fn rust_example() {
// Port the logic from the reference block below.
}
}
// Reference (java):
// // Accepted solution for LeetCode #3702: Longest Subsequence With Non-Zero Bitwise XOR
// class Solution {
// public int longestSubsequence(int[] nums) {
// int xor = 0, cnt0 = 0;
// int n = nums.length;
// for (int x : nums) {
// xor ^= x;
// cnt0 += x == 0 ? 1 : 0;
// }
// if (xor != 0) {
// return n;
// }
// return cnt0 == n ? 0 : n - 1;
// }
// }
// Accepted solution for LeetCode #3702: Longest Subsequence With Non-Zero Bitwise XOR
function longestSubsequence(nums: number[]): number {
let [xor, cnt0] = [0, 0];
for (const x of nums) {
xor ^= x;
cnt0 += x === 0 ? 1 : 0;
}
const n = nums.length;
if (xor) {
return n;
}
if (cnt0 === n) {
return 0;
}
return n - 1;
}
Use this to step through a reusable interview workflow for this problem.
Sort the array in O(n log n), then scan for the missing or unique element by comparing adjacent pairs. Sorting requires O(n) auxiliary space (or O(1) with in-place sort but O(n log n) time remains). The sort step dominates.
Bitwise operations (AND, OR, XOR, shifts) are O(1) per operation on fixed-width integers. A single pass through the input with bit operations gives O(n) time. The key insight: XOR of a number with itself is 0, which eliminates duplicates without extra space.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.