Find Target Indices After Sorting Array - Problem

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.

Input & Output

Example 1 — Basic Case
$ Input: nums = [1,2,5,2,3], target = 2
Output: [1,2]
💡 Note: After sorting: [1,2,2,3,5]. Target 2 appears at indices 1 and 2.
Example 2 — No Target Found
$ Input: nums = [1,2,5,2,3], target = 4
Output: []
💡 Note: After sorting: [1,2,2,3,5]. Target 4 is not found, so return empty list.
Example 3 — Single Element
$ Input: nums = [1], target = 1
Output: [0]
💡 Note: Array has one element which matches target, so return [0].

Constraints

  • 1 ≤ nums.length ≤ 100
  • -100 ≤ nums[i] ≤ 100
  • -100 ≤ target ≤ 100

Visualization

Tap to expand
Find Target Indices After SortingInput: nums = [1,2,5,2,3], target = 212523SortAfter Sorting:1223501234Output: [1,2]
Understanding the Visualization
1
Input
Unsorted array and target value
2
Process
Sort array and find target positions
3
Output
List of indices where target appears
Key Takeaway
🎯 Key Insight: We can calculate target positions without sorting by counting smaller and equal elements
Asked in
Amazon 15 Microsoft 12 Google 8
28.5K Views
Medium Frequency
~15 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen