Problem
Ask
Submissions

Problem: Find First and Last Position of Element in Sorted Array

Medium
30 min
Explore how to efficiently find the first and last positions of a target value within a sorted integer array by applying a modified binary search technique. Understand the problem constraints, think through the logic, and implement a solution with logarithmic runtime suitable for coding interviews.

Statement

You are given an integer array, nums, that is sorted in non-decreasing order. Your task is to find the first and last indexes of a given value, target, within this array.

If the target does not appear in the array at all, return [-1, -1].

Note: You must write an algorithm with O(logn)O(\log n) runtime complexity.

Constraints:

  • 00 \le nums.length 105\leq 10^5

  • 109-10^{9} \leq nums[i] 109\leq 10^{9}

  • nums is a non-decreasing array.

  • 109-10^{9} \leq target 109\leq 10^{9}

Problem
Ask
Submissions

Problem: Find First and Last Position of Element in Sorted Array

Medium
30 min
Explore how to efficiently find the first and last positions of a target value within a sorted integer array by applying a modified binary search technique. Understand the problem constraints, think through the logic, and implement a solution with logarithmic runtime suitable for coding interviews.

Statement

You are given an integer array, nums, that is sorted in non-decreasing order. Your task is to find the first and last indexes of a given value, target, within this array.

If the target does not appear in the array at all, return [-1, -1].

Note: You must write an algorithm with O(logn)O(\log n) runtime complexity.

Constraints:

  • 00 \le nums.length 105\leq 10^5

  • 109-10^{9} \leq nums[i] 109\leq 10^{9}

  • nums is a non-decreasing array.

  • 109-10^{9} \leq target 109\leq 10^{9}