Find Target Indices After Sorting Array (LeetCode 2089): Solution and Explanation
Return the positions that the value target would occupy if the array were sorted in non-decreasing order, listed in increasing order.
Intuition
Sorting puts every copy of target next to each other in one block. The question is only where that block starts and how long it is.
The block starts right after all the values that are smaller than target, because each of them sorts in front of it. So if less values are smaller, the first copy lands at index less.
The block is as long as the number of copies, equal. Values larger than target sort after the block and do not move it at all.
So one pass that counts less and equal is enough. The answer is the run of indices from less to less + equal - 1, and no sort is ever performed.
Approach: Counting (no sort)
- Set
less = 0andequal = 0. - For every value
x: ifx < target, incrementless; ifx == target, incrementequal; otherwise do nothing. - Return the list
less, less + 1, ..., less + equal - 1. It is empty whenequalis 0.
Step-by-step example
nums = [1,2,5,2,3], target = 2
- 1 is smaller than 2, so less = 1.
- 2 equals the target, so equal = 1.
- 5 is larger, so nothing changes.
- 2 equals the target, so equal = 2.
- 3 is larger, so nothing changes.
- The block starts at index 1 and holds 2 slots, so the answer is [1,2]. Sorted, the array would be [1,2,2,3,5], which agrees.
Code: Python, Java, C++
1class Solution:
2 def targetIndices(self, nums: List[int], target: int) -> List[int]:
3 less = equal = 0
4 for x in nums:
5 if x < target:
6 less += 1
7 elif x == target:
8 equal += 1
9 return list(range(less, less + equal))1class Solution {
2 public List<Integer> targetIndices(int[] nums, int target) {
3 int less = 0, equal = 0;
4 for (int x : nums) {
5 if (x < target) {
6 less++;
7 } else if (x == target) {
8 equal++;
9 }
10 }
11 List<Integer> result = new ArrayList<>();
12 for (int i = less; i < less + equal; i++) {
13 result.add(i);
14 }
15 return result;
16 }
17}1class Solution {
2public:
3 vector<int> targetIndices(vector<int>& nums, int target) {
4 int less = 0, equal = 0;
5 for (int x : nums) {
6 if (x < target) {
7 less++;
8 } else if (x == target) {
9 equal++;
10 }
11 }
12 vector<int> result;
13 for (int i = less; i < less + equal; i++) {
14 result.push_back(i);
15 }
16 return result;
17 }
18};Time and space complexity
O(n)
One pass to count, plus writing out at most n indices.
O(1)
Two counters besides the output list.
Other approaches
Sort and scan (time O(n log n), space O(n))
Sort a copy of the array and collect every index whose value equals target. Straightforward, and fine for 100 elements.
Sort and binary search (time O(n log n), space O(n))
After sorting, binary search for the first and last copy of target and return the range between them. The search is fast but the sort still dominates.
Common mistakes
- Returning the original indices of the target values. The question asks for indices in the sorted array.
- Counting values that are less than or equal to target as
less, which shifts the whole block right byequal. - Returning
[less]alone when target appears several times instead of the full range.
Frequently asked questions
Why can I skip the sort entirely?
Only the position of the target block matters, and that is fixed by how many values are smaller than target and how many equal it. Counting gives both numbers directly.
Why is this under binary search?
A common solution sorts the array and binary searches for the first and last copy of target, like Find First and Last Position of Element in Sorted Array. Counting reaches the same range in linear time.
What if target is not in the array?
Then equal is 0 and the range from less to less - 1 is empty, so the function returns an empty list.