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 <= 100When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The basic idea is to rearrange the numbers from smallest to biggest. Afterward, we need to identify the positions where a specific number appears. This can be done by looking at each position one by one.
Here's how the algorithm would work step-by-step:
def find_target_indices_brute_force(numbers, target):
sorted_numbers = sorted(numbers)
target_indices = []
# Iterate through the sorted array to find target indices
for index in range(len(sorted_numbers)):
# Check if the number at the current index is the target
if sorted_numbers[index] == target:
# Append the index to the result if it matches
target_indices.append(index)
return target_indicesThe best way to solve this is not to sort the whole collection. Instead, we only need to count how many numbers are smaller than our target and how many are equal to it. This gives us all the information needed to figure out where the target numbers would be located after sorting, without actually doing the sort.
Here's how the algorithm would work step-by-step:
def find_target_indices(numbers, target): smaller_count = 0
equal_count = 0
for number in numbers:
if number < target:
smaller_count += 1
elif number == target:
equal_count += 1
# Starting index is the number of elements smaller than the target.
start_index = smaller_count
# Determine the end index based on the count of equal numbers.
end_index = start_index + equal_count
target_indices = []
# Populate the result with the range of indices.
for index in range(start_index, end_index):
target_indices.append(index)
return target_indices| Case | How to Handle |
|---|---|
| Null or empty input array | Return an empty list immediately as there are no indices to find. |
| Input array with only one element | Compare the single element to the target and return [0] if it matches, or [] if it doesn't. |
| Target is smaller than all elements in the array | The sorted array will have all elements larger than the target, so the algorithm should return an empty list. |
| Target is larger than all elements in the array | The sorted array will not contain the target, and the algorithm should return an empty list. |
| Array contains duplicate target values | The algorithm should correctly identify and return all indices where the target value is located in the sorted array. |
| Array contains only identical values, and the target is that value | The algorithm should return all indices in the array. |
| Large input array with potential integer overflow during sorting index calculations (if using indices) | Use data types that can handle large indices, or use a counting sort approach to avoid index-based comparisons during sorting. |
| Integer overflow if target is close to max or min int | Ensure the comparison `nums[i] == target` doesn't result in an overflow, especially if the target is derived from computations involving array elements. |