class Solution {\npublic:\n vector searchRange(vector& nums, int target) {\n if (nums.empty()) {\n return {-1, -1};\n }\n \n int left = lower_bound(nums.begin(), nums.end(), target) - nums.begin();\n int right = upper_bound(nums.begin(), nums.end(), target) - nums.begin() - 1;\n \n if (left < nums.size() && nums[left] == target) {\n return {left, right};\n }\n \n return {-1, -1};\n }\n};\nThe code provided has a runtime error. It is likely caused by accessing elements of the vector nums without checking if the vector is empty.\n\nTo fix this issue, you can add a check to see if nums is empty before accessing its elements. Here's an updated version of the code:\n\ncpp\nclass Solution {\npublic:\n vector<int> searchRange(vector<int>& nums, int target) {\n if (nums.empty()) {\n return {-1, -1};\n }\n \n int left = lower_bound(nums.begin(), nums.end(), target) - nums.begin();\n int right = upper_bound(nums.begin(), nums.end(), target) - nums.begin() - 1;\n \n if (left < nums.size() && nums[left] == target) {\n return {left, right};\n }\n \n return {-1, -1};\n }\n};\n\nThis updated code first checks if nums is empty and returns {-1, -1} if it is. Then, it finds the indices of the leftmost and rightmost occurrences of target using lower_bound and upper_bound. Finally, it checks if the leftmost index is within the vector's bounds and if the element at that index is equal to target. If yes, it returns the range [left, right], otherwise it returns {-1, -1}.

C++  Search Range in Sorted Array: Runtime Error Fix and Explanation