# Leetcode 34 - Find first and last position of element in sorted array

Note:

• Update:
• Just use 2 binary search.
• Pre-check nums[middle - 1] and nums[middle + 1] in case we’re right on the boundary!
• The required time complexity is O(logn), so we need to use binary search, and DON’T USE DOUBLE POINTERS!
• Double pointers is O(n) but not O(logn).
• Take findLeftBoundary for example:
• When nums[middle] < target, left = middle + 1.
• When nums[middle] > target, right = middle - 1.
• When nums[middle] === target, left must be on the left side. But we need to check edge cases:
• middle === 0 or nums[middle - 1] !== target. Return middle in this case.
• Otherwise right = middle - 1;

Question:

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

Example:
Code: