Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Build confidence with an intuition-first walkthrough focused on array fundamentals.
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 <= 1001 <= nums[i], target <= 100Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Binary Search
[1,2,5,2,3] 2
[1,2,5,2,3] 3
[1,2,5,2,3] 5
find-first-and-last-position-of-element-in-sorted-array)rank-transform-of-an-array)find-words-containing-character)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2089: Find Target Indices After Sorting Array
class Solution {
public List<Integer> targetIndices(int[] nums, int target) {
Arrays.sort(nums);
List<Integer> ans = new ArrayList<>();
for (int i = 0; i < nums.length; ++i) {
if (nums[i] == target) {
ans.add(i);
}
}
return ans;
}
}
// Accepted solution for LeetCode #2089: Find Target Indices After Sorting Array
func targetIndices(nums []int, target int) (ans []int) {
sort.Ints(nums)
for i, v := range nums {
if v == target {
ans = append(ans, i)
}
}
return
}
# Accepted solution for LeetCode #2089: Find Target Indices After Sorting Array
class Solution:
def targetIndices(self, nums: List[int], target: int) -> List[int]:
nums.sort()
return [i for i, v in enumerate(nums) if v == target]
// Accepted solution for LeetCode #2089: Find Target Indices After Sorting Array
struct Solution;
impl Solution {
fn target_indices(mut nums: Vec<i32>, target: i32) -> Vec<i32> {
nums.sort_unstable();
let n = nums.len();
let mut res = vec![];
for i in 0..n {
if nums[i] == target {
res.push(i as i32);
}
}
res
}
}
#[test]
fn test() {
let nums = vec![1, 2, 5, 2, 3];
let target = 2;
let res = vec![1, 2];
assert_eq!(Solution::target_indices(nums, target), res);
let nums = vec![1, 2, 5, 2, 3];
let target = 3;
let res = vec![3];
assert_eq!(Solution::target_indices(nums, target), res);
let nums = vec![1, 2, 5, 2, 3];
let target = 5;
let res = vec![4];
assert_eq!(Solution::target_indices(nums, target), res);
}
// Accepted solution for LeetCode #2089: Find Target Indices After Sorting Array
function targetIndices(nums: number[], target: number): number[] {
nums.sort((a, b) => a - b);
let ans: number[] = [];
for (let i = 0; i < nums.length; ++i) {
if (nums[i] == target) {
ans.push(i);
}
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Check every element from left to right until we find the target or exhaust the array. Each comparison is O(1), and we may visit all n elements, giving O(n). No extra space needed.
Each comparison eliminates half the remaining search space. After k comparisons, the space is n/2ᵏ. We stop when the space is 1, so k = log₂ n. No extra memory needed — just two pointers (lo, hi).
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Setting `lo = mid` or `hi = mid` can stall and create an infinite loop.
Usually fails on: Two-element ranges never converge.
Fix: Use `lo = mid + 1` or `hi = mid - 1` where appropriate.