2089. Find Target Indices After Sorting Array LeetCode Solution
In this guide, you will get 2089. Find Target Indices After Sorting Array LeetCode Solution with the best time and space complexity. The solution to Find Target Indices After Sorting Array problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Find Target Indices After Sorting Array solution in C++
- Find Target Indices After Sorting Array solution in Java
- Find Target Indices After Sorting Array solution in Python
- Additional Resources

Problem Statement of Find Target Indices After Sorting Array
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.
Example 1:
Input: nums = [1,2,5,2,3], target = 2
Output: [1,2]
Explanation: After sorting, nums is [1,2,2,3,5].
The indices where nums[i] == 2 are 1 and 2.
Example 2:
Input: nums = [1,2,5,2,3], target = 3
Output: [3]
Explanation: After sorting, nums is [1,2,2,3,5].
The index where nums[i] == 3 is 3.
Example 3:
Input: nums = [1,2,5,2,3], target = 5
Output: [4]
Explanation: After sorting, nums is [1,2,2,3,5].
The index where nums[i] == 5 is 4.
Constraints:
1 <= nums.length <= 100
1 <= nums[i], target <= 100
Complexity Analysis
- Time Complexity: O(n)
- Space Complexity: O(1)
2089. Find Target Indices After Sorting Array LeetCode Solution in C++
class Solution {
public:
vector<int> targetIndices(vector<int>& nums, int target) {
vector<int> ans;
const int count = ranges::count(nums, target);
int lessThan =
ranges::count_if(nums, [&](int num) { return num < target; });
for (int i = 0; i < count; ++i)
ans.push_back(lessThan++);
return ans;
}
};
/* code provided by PROGIEZ */
2089. Find Target Indices After Sorting Array LeetCode Solution in Java
class Solution {
public List<Integer> targetIndices(int[] nums, int target) {
List<Integer> ans = new ArrayList<>();
final int count = (int) Arrays.stream(nums).filter(num -> num == target).count();
int lessThan = (int) Arrays.stream(nums).filter(num -> num < target).count();
for (int i = 0; i < count; ++i)
ans.add(lessThan++);
return ans;
}
}
// code provided by PROGIEZ
2089. Find Target Indices After Sorting Array LeetCode Solution in Python
class Solution:
def targetIndices(self, nums: list[int], target: int) -> list[int]:
count = nums.count(target)
lessThan = sum(num < target for num in nums)
return [i for i in range(lessThan, lessThan + count)]
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.