Please use Laptop/Desktop or any other large screen for the Mock Interview Session.

Find First and Last Position of Element in Sorted Array



YouTube Video Thumbnail
Link

Watch above sample mock interview video to see how it works.
Login and Buy Premium to Start the Interview



Find First and Last Index of Element in Sorted List

Find First and Last Index of Element in Sorted List

Problem Statement

Given a sorted list of integers arr in ascending order, determine the first and last positions where a specified key appears.

Your solution should aim for a runtime complexity of approximately O(log n).

If the key is not present in the list, return [-1, -1].

Examples

Input: arr = [2, 4, 4, 6, 6, 9], key = 6
Output: [3, 4]
    
Input: arr = [2, 4, 4, 6, 6, 9], key = 5
Output: [-1, -1]
    
Input: arr = [1, 1, 2, 3, 4, 4, 4, 5], key = 4
Output: [4, 6]
    

Constraints

  • 1 ≤ length of arr ≤ 15,000
  • -105 ≤ elements of arr ≤ 105
  • arr is sorted in ascending order