Skip to content

Missing Test Case - 540. Single Element in a Sorted Array #8632

@anshuman-gogate

Description

@anshuman-gogate

Your LeetCode username

anshuman-gogate

Category of the bug

  • Question
  • Solution
  • Language
  • [. ] Missing Test Cases

Description of the bug

There is a missing test case
when nums: [1, 1, 2]
for this test case some code might give error

Code you used for Submit/Run operation

class Solution {
public:
    int singleNonDuplicate(vector<int>& nums) {
        if (nums.size() == 1) return nums[0];
        
        int left = 0, right = nums.size()-1;
        
        while (left <= right) {
            int mid = left + (right-left) / 2;
            
            int num = mid % 2 == 0 ? mid+1 : mid-1;
            
            if (nums[mid] == nums[num]) { // we are in left part
                left = mid + 1;
                continue;
            }
            
            // right part
            right = mid-1;
        }
        
        return nums[left];
    }
};

// Two Sum
class Solution {
public:
vector twoSum(vector &a, int s) {

}

};


#### Language used for code
<!-- C++ -->
C++

#### Expected behavior
<!-- A clear and concise description of what you expected to happen in
contrast with what actually happened. -->
This should have resulted in an error
because according to the code we are trying
to access an invalid index in the array


#### Screenshots
<!-- If applicable, add screenshots to explain your issue. -->



#### Additional context
<!-- Add any other additional context about the bug. -->

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions