Sorted Positions of a Target Value

Quick Overview

Sorted Positions of a Target Value. Return all zero-based indices occupied by target after nums is sorted in nondecreasing order. The returned indices must be increasing. If target is absent, return an empty vector.

Sorted Positions of a Target Value

Company: Google

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

### Problem Given `nums` and a value `target`, consider the array in nondecreasing sorted order. Return every zero-based position occupied by `target` in that sorted array. The returned positions must be in increasing order. If `target` is absent, return an empty vector; return sorted-array positions rather than positions in the original input. ```cpp class Solution { public: vector<int> targetIndices(vector<int>& nums, int target); }; ``` ### Examples **Example 1:** ```text nums = [4,1,4,2], target = 4 Output: [2,3] Explanation: Sorting gives [1,2,4,4]. ``` **Example 2:** ```text nums = [3,1], target = 2 Output: [] Explanation: Neither sorted entry equals 2. ``` ### Constraints - `1 <= nums.length <= 100`. - `1 <= nums[i],target <= 100`. ### Clarifying Questions The result contains sorted-array positions, not original positions. Output order is specified and must not be treated as any-order freedom. ```hint Inspect the structure How many values necessarily precede the first target in sorted order? ``` ### Reasoning checkpoints - Identify exactly the target's positions in nondecreasing order, including every duplicate. - Return positions in increasing order and distinguish them from original input indices. - Handle an absent target and targets at the beginning or end of the sorted array. ### Optional extensions How could you answer the query efficiently if nums were already sorted?

Overview: Sorted Positions of a Target Value. Return all zero-based indices occupied by target after nums is sorted in nondecreasing order. The returned indices must be increasing. If target is absent, return an empty vector.

Google logo
Google
Aug 8, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

Problem

Given nums and a value target, consider the array in nondecreasing sorted order. Return every zero-based position occupied by target in that sorted array. The returned positions must be in increasing order. If target is absent, return an empty vector; return sorted-array positions rather than positions in the original input.

class Solution {
public:
    vector<int> targetIndices(vector<int>& nums, int target);
};

Examples

Example 1:

nums = [4,1,4,2], target = 4
Output: [2,3]
Explanation: Sorting gives [1,2,4,4].

Example 2:

nums = [3,1], target = 2
Output: []
Explanation: Neither sorted entry equals 2.

Constraints

  • 1 <= nums.length <= 100 .
  • 1 <= nums[i],target <= 100 .

Clarifying Questions Guidance

The result contains sorted-array positions, not original positions. Output order is specified and must not be treated as any-order freedom.

Reasoning checkpoints

  • Identify exactly the target's positions in nondecreasing order, including every duplicate.
  • Return positions in increasing order and distinguish them from original input indices.
  • Handle an absent target and targets at the beginning or end of the sorted array.

Optional extensions

How could you answer the query efficiently if nums were already sorted?