Problem Description
Given a 0-indexed integer array nums and a target value, return a list of all indices where the target appears after sorting the array in non-decreasing order. If the target does not appear in the array, return an empty list.
Key Insights
- The problem requires sorting the array first.
- After sorting, the indices where the target resides must be collected.
- The constraints are small (array length up to 100), so a simple sort and linear scan is efficient.
- Edge case: if the target does not exist in the array, return an empty list.
Space and Time Complexity
Time Complexity: O(n log n) due to the sorting step. Space Complexity: O(n) for storing the sorted array (if not sorted in-place) and the list of target indices.
Solution
The solution approach involves first sorting the given array in non-decreasing order. Once sorted, iterate through the array and whenever a value equals the target, store its index in the results list. Finally, return the list of collected indices. We use an array (or list) to accumulate the indices. The sort step is the dominant cost in terms of time complexity. The solution is straightforward and leverages built-in sorting capabilities.