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:
- If all the elements are zero output should be zero.
- If the XOR of all elements is non-zero, then size of array.
- 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
Additional context
No response
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:
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
Additional context
No response