Skip to content

Missing Test Case - 3702. Longest Subsequence With Non-Zero Bitwise XOR #32782

Description

@AkiiSinghal

LeetCode Username

AkiiSinghal

Problem Number, Title, and Link

https://leetcode.com/problems/longest-subsequence-with-non-zero-bitwise-xor/

Bug Category

Missing test case (Incorrect/Inefficient Code getting accepted because of missing test cases)

Bug Description

It has been solved by simple steps:

  1. If all the elements are zero output should be zero.
  2. If the XOR of all elements is non-zero, then size of array.
  3. If the XOR of all elements is zero, then (size of array - 1).

If we have a test case like
[0,0,3,3,0,0,0]
It should return 4 as correct, but returning 6.

Language Used for Code

C++

Code used for Submit/Run operation

class Solution {
public:
    int longestSubsequence(vector<int>& nums) {
        int x = 0, res = nums.size();
        bool isAllZero = true;

        for(int n: nums) {
            x ^= n;
            if(n and isAllZero)
                isAllZero = false;
        }
        if(isAllZero)
            return 0;
        if(x)
            return res;
        return res-1;
    }
};

Expected behavior

Instead of returning size -1 when XOR is 0.
We should find the nearest non-zero element from both ends. And reduce the least number of elements (whichever is nearest to both ends) from the size of the array as a result.

Example:
Input: nums = [0,0,3,4,3,4,0,0,0]
Output: 6
Explanation: 3 is nearest to the left (3 element) and 4 is nearest to the right (4 element). Therefore, we reduce 3 elements from the size of the array, which will result in 6.

Screenshots

Image Image

Additional context

No response

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions