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.

30:00

Find Target Indices After Sorting Array
easy
Free written solution
Topics
Companies

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]
Constraints:
  • 1≤nums.length≤1001 \leq \text{nums.length} \leq 100

  • 1≤nums[i],target≤1001 \leq \text{nums}[i], \text{target} \leq 100

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"nums":[1,2,5,2,3],"target":2}

No need to sort. In sorted order the copies of 2 sit right after every smaller value, so count the smaller values and the copies.

nums (unsorted), target = 2

1

0

2

1

5

2

2

3

3

4

sorted positions (never actually sorted)

0

1

2

3

4

less = 0 equal = 0 greater = 0

result

...

Variables
VariableValue
target2
less0
equal0
DepthFunction Call
Stack empty
0/8