Algorithm II | Study Plan | Leetcode

Posted on Dec 25, 2020

34. Find First and Last Position of Element in Sorted Array

Given an array of integers nums sorted in non-decreasing order, find the starting and ending position of a given target value.

If target is not found in the array, return [-1, -1].

You must write an algorithm with O(log n) runtime complexity.

class Solution {
public:
    vector<int> searchRange(vector<int>& nums, int target) {

        int len(nums.size()), first = -1, last = -1;
        int l(0), r(len-1);
        while(l<=r){
            int mid = l + (r - l)/2;
            if(nums[mid] == target)
                first =  mid;
            if(nums[mid] >= target)
                r = mid - 1;
            else
                l = mid + 1;
        }

        l = 0, r = len-1;
        while(l<=r){
            int mid = l + (r - l)/2;
            if(nums[mid] == target)
                last =  mid;
            if(nums[mid] > target)
                r = mid -1;
            else
                l = mid + 1;
        }

        return {first, last};
    }
};