LeetCode-Feedback / LeetCode-Feedback

661 stars 314 forks source link

Test case #23296

Closed Keshav2407 closed 1 month ago

Keshav2407 commented 1 month ago

LeetCode Username

keshav2437

Problem Number, Title, and Link

https://leetcode.com/problems/majority-element/

Bug Category

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

Bug Description

When we pass the array {1, 2, 3, 4, 2, 10, 4, 5, 2, 7, 8, 2, 10}, the output should be 2, but it gives the output 10.

Language Used for Code

Java

Code used for Submit/Run operation

class Solution {
    public int majorityElement(int[] nums) {
        int candidate = nums[0];
        int count = 1;
        for (int i = 1; i < nums.length; i++) {
            if (nums[i] == candidate) {
                count++;
            } else {
                count--;
            }

            if (count == 0) {
                candidate = nums[i];
                count = 1;
            }
        }
        return candidate;
    }
}

Expected behavior

public class Main { public static void main(String[] args) { Solution solution = new Solution(); int nums2[] = { 1,2,3,4,2,10,4,5,2,7,8,2,10}; System.out.println("The majority element in nums2 is: " + solution.majorityElement(nums2)); } }

OUTPUT : - 2; but it give 10

Screenshots

No response

Additional context

No response

exalate-issue-sync[bot] commented 1 month ago

Winston Tang commented: Dear keshav2437,

Thank you for reaching out. However, it seems there might be a misunderstanding with respect to the problem's requirement. The problem requires finding the majority element in an array which appears more than ⌊𝑛/2⌋ times in the array. The array you provided, {1, 2, 3, 4, 2, 10, 4, 5, 2, 7, 8, 2, 10}, has no majority element as per this definition as neither the element 2 nor 10 appears more than 6 times.

Your provided code uses the correct Boyer-Moore Majority Vote algorithm, and thus, receiving '10' as output is not an incorrect outcome. I suggest revisiting the problem statement to ensure a clear understanding of the problem's requirements.

Do not hesitate to contact us if you have any more enquiries or need further clarification. Keep up the good work!

Best Regards, LeetCode Support Team