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

Find First and Last Position of Element in Sorted Array (LeetCode 34) is a Medium Data Structures and Algorithms interview problem you can solve, run and submit on Thita.ai. It belongs to the Binary Search Patterns pattern, in the Find First/Last Occurrence subpattern.

How to solve Find First and Last Position of Element in Sorted Array

Find First and Last Position of Element in Sorted Array is a Find First and Last Occurrence problem, part of Binary Search Patterns. Learn to find first and last occurrence of elements using binary search. Master lower bound and upper bound concepts.

Work towards the solution in these steps:

  1. Consider which algorithmic approach can efficiently search for elements in a sorted array with logarithmic time complexity.
  2. Think about how binary search can be adapted to find the first occurrence of a target value rather than any occurrence.
  3. Similarly, explore how to modify binary search to find the last occurrence of the target value in the array.

The reference solution runs in O(logn) time and O(1) space.

The Find First and Last Position of Element in Sorted Array solution and editorial walks through the full approach with reference code.

Problem statement

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.

Example 1:

Input: nums = [5,7,7,8,8,10], target = 8 Output: [3,4]

Example 2:

Input: nums = [5,7,7,8,8,10], target = 6 Output: [-1,-1]

Example 3:

Input: nums = [], target = 0 Output: [-1,-1]

Constraints:

- 0 <= nums.length <= 10^5

- -10^9 <= nums[i] <= 10^9

- nums is a non-decreasing array.

- -10^9 <= target <= 10^9

Topics and companies

Topics: Array, Binary Search.

Reported in interviews at Facebook, Amazon, Uber, Google, Microsoft, LinkedIn, Bloomberg, Snapchat, Oracle.

How to practise Find First and Last Position of Element in Sorted Array on Thita.ai

Starter code is provided in C, C#, C++, Go, Java, JavaScript and Python. Your solution runs against 5 test cases for this problem, with the AI coach available for a hint when you are stuck rather than a finished answer. The reference solution runs in O(logn) time and O(1) space.

Open Find First and Last Position of Element in Sorted Array in the code editor, or read the Find First and Last Position of Element in Sorted Array editorial for a worked solution with its approach and complexity analysis.

Where Find First and Last Position of Element in Sorted Array sits in the DSA pattern sheet

Problems related to Find First and Last Position of Element in Sorted Array

Other problems that use the same Find First/Last Occurrence and Binary Search Patterns techniques:

Find First and Last Position of Element in Sorted Array FAQ

What is the LeetCode number of Find First and Last Position of Element in Sorted Array?

Find First and Last Position of Element in Sorted Array is LeetCode problem 34, rated Medium.

What pattern is used to solve Find First and Last Position of Element in Sorted Array?

Find First and Last Position of Element in Sorted Array is a Find First and Last Occurrence problem. It belongs to the Binary Search Patterns pattern, in the Find First/Last Occurrence subpattern.

What is the time complexity of Find First and Last Position of Element in Sorted Array?

The reference solution runs in O(logn) time and O(1) space.

Which companies ask Find First and Last Position of Element in Sorted Array?

Find First and Last Position of Element in Sorted Array has been reported in interviews at Facebook, Amazon, Uber, Google, Microsoft, LinkedIn, Bloomberg, Snapchat, Oracle.

Preparing this workspace.