Are the test cases broken?

  • 0

    I did a simple binary search solution in c++ but it gets reject by the final test case when I submit. However, the same test case works on my local machine AND when I use the "Run Code" button. Here is the code:

    class Solution {
        int findDuplicate(vector<int>& nums) {
            int l = 1;
            int h = nums.size()-1;
            int m;
            int count = 0;
            while (h-l>0) {
                m = (l+h)/2;
                count = 0;
                for (int i = 0; i <= nums.size(); i++) {
                    if (nums[i]>m && nums[i]<=h) count++;
                if (count > h-m) {
                    l = m+1;
                else {
                    h = m;
            return l;

    The test case is:


    When I use "Run Code" or compile on my local machine, this outputs 12 (the correct answer). When I hit submit, the system claims my code gave the incorrect answer of: 49. Is this a glitch in leetcode's backend or did I actually manage to do something wrong. I'll point out that this solution passes all the other test cases...

  • 0

    Using nums[i] when i is nums.size() is a bad idea.

  • 0

    Ah. Hence the unpredictable behavior. Thanks for catching that!

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.