462. Find All Positions of Target in Sorted Array

EasyBinary SearchArrayBinary Search

Given a sorted array nums and an integer target, return a list of all indices where target appears, in ascending order. Return an empty list if target is not present. Solve in O(log n + k) where k is the count.

Input: A sorted integer array nums and integer target.

Output: List of all 0-indexed positions of target, sorted ascending. Empty list if not found.

Examples

Example 1
Input: [1,2,2,3,3,3,4], 3
Output: [3,4,5]
Explanation: 3 appears at indices 3, 4, and 5.
Example 2
Input: [1,2,3,4,5], 6
Output: []
Explanation: 6 not present — empty list returned.
Example 3
Input: [5,5,5,5,5], 5
Output: [0,1,2,3,4]
Explanation: All 5 elements equal target.

Constraints

Asked by

TCSInfosysWiproCognizantCapgemini
Solve this problem in the editor →