Skip to content

Commit b2b3536

Browse files
authored
Improved task 3702
1 parent e424117 commit b2b3536

1 file changed

Lines changed: 10 additions & 0 deletions

File tree

  • src/main/java/g3701_3800/s3702_longest_subsequence_with_non_zero_bitwise_xor
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
### Complexity Analysis
2+
3+
- **Time Complexity:** **O(N)**, where N is the length of the `nums` array. We iterate through the array exactly once to calculate the total XOR sum and check for non-zero elements.
4+
- **Space Complexity:** **O(1)**. The algorithm only uses two auxiliary variables (`xorSum` and `allZero`), which require constant extra space regardless of the input size.
5+
6+
### Intuition
7+
8+
- If all elements are `0`, no valid subsequence exists, so we return `0`.
9+
- If the XOR sum of the entire array is non-zero, the longest subsequence is the entire array itself, returning `N`.
10+
- If the total XOR sum is `0` (and the array has at least one non-zero element), removing exactly one non-zero element will flip the total XOR to a non-zero value. Thus, the maximum length becomes `N - 1`.

0 commit comments

Comments
 (0)