FastPrepFind First and Last Position of Element in Sorted Array

Find First and Last Position of Element in Sorted Array

EPAM logoEPAM● MediumFULLTIMEPHONE SCREEN
Learn

Problem statement

Given an integer array nums sorted in nondecreasing order and an integer target, return a two-element array containing the first and last indices at which target appears.

If target is absent, return [-1,-1]. Your algorithm must run in O(log n) time.

Function

searchRange(nums: int[], target: int) → int[]

Examples

Example 1

nums = [5,7,7,8,8,10]target = 8return = [3,4]

The target occupies indices 3 through 4.

Example 2

nums = [5,7,7,8,8,10]target = 6return = [-1,-1]

The target is absent.

Constraints

  • 0 <= nums.length <= 100000.
  • Every value and target is in [-10^9, 10^9].
  • nums is sorted in nondecreasing order.

Source note: Author-written prompt excerpt retained for source fidelity.

More EPAM problems

See EPAM hiring insights
public int[] searchRange(int[] nums, int target) {
    // Write your code here.
}
nums[5,7,7,8,8,10]
target8
expected[3,4]
Checking account…