First Last Position in Sorted Array Problem
First Last Position in Sorted Array Problem — ExecCode Easy DSA Practice
Solve the First Last Position in Sorted Array problem on ExecCode. Free online easy DSA practice in Binary Search. Write and run code in Java, C++, Python — no signup required to run.
Problem description
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.
Examples
Input nums = [5, 7, 7, 8, 8, 8, 10, 12], target = 8; Output [3, 5]. Input nums = [5, 7, 7, 8, 8, 8, 10, 12], target = 6; Output [-1, -1]. Input nums = [1], target = 1; Output [0, 0]
Constraints
0 ≤ nums.length ≤ 10⁵ -10⁹ ≤ nums[i] ≤ 10⁹ nums is a non-decreasing array -10⁹ ≤ target ≤ 10⁹
Practice First Last Position in Sorted Array free on ExecCode. Browse DSA problems, topic map, and placement guides.