Skip to content

Bug Report for majority-element-ii #4804

@sujayrao1

Description

@sujayrao1

Bug Report for https://neetcode.io/problems/majority-element-ii

Please describe the bug below and include any steps to reproduce the bug or screenshots if possible.

Not much of a bug but not good test cases.

public List majorityElement(int[] nums) {
List res = new ArrayList<>();
int num1=-1, num2=-1, c1=0, c2=0;
for(int num: nums){
if(num==num1) c1++;
else if(num==num1) c2++;
else if(c1==0){ c1=1; num1=num;}
else if(c2==0){ c2=1; num2=num;}
else {c1--; c2--;}
}
c1 = c2 = 0;
for(int num: nums){
if(num==num1) c1++;
if(num==num2) c2++;
}
if(c1>nums.length/3) res.add(num1);
if(c2>nums.length/3) res.add(num2);
return res;
}

This code is accepted even though it is wrong. Check the first two if and else if statements.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions