Dsa
Binary Search
Medium
Find First and Last Position of Target
Given a sorted array of integers, your task is to find the first and last positions of a target integer in the array. If the target does not exist, return [-1, -1]. If the array is [5, 7, 7, 8, 8, 10] and the target is 8, the result should be [3, 4].
Input Format:
- The first line contains an integer n (1 ≤ n ≤ 1000), the number of elements in the array.
- The second line contains n space-separated integers representing the array elements.
- The third line contains the target integer.
Output Format:
- A list of two integers representing the first and last positions of the target.
Example:
Input:
6
5 7 7 8 8 10
8
Output:
3 4
Key concepts
binary_searcharraysearching
Practise this out loud — free
Start a mock interview on THIS exact question — a voice AI interviewer opens with it, pushes back like a real onsite, then hands you an instant scorecard.
🎙 Practise this question now