Skip to main content
Back to problems
#2089
Easy Algorithms

Find target indices after sorting array

Array Binary Search Sorting
77.8% acceptance
Feb 25, 2026
1950
106
You are given a 0-indexed integer array nums and a target element target. A target index is an index i such that nums[i] == target. Return a list of the target indices of nums after sorting nums in non-decreasing order. If there are no target indices, return an empty list. The returned list must be sorted in increasing order.

Solution

Rust
Time O(n)
Space O(1)
LeetCode
solution.rs
impl Solution {
  pub fn target_indices(nums: Vec<i32>, target: i32) -> Vec<i32> {
    let less = nums.iter().filter(|&&x| x < target).count() as i32;
    let equal = nums.iter().filter(|&&x| x == target).count() as i32;
    (less..less + equal).collect()
  }
}