Create Target Array in the Given Order (LeetCode 1389): Solution and Explanation

Easy
Array
Simulation

Build an array by inserting nums[i] at position index[i], one pair at a time from left to right, and return the result.

Intuition

The problem describes a process, and the direct way to get its result is to run that process. Start with an empty list and perform each insertion in order.

Inserting at position p means every value from p to the end moves one place right to open a gap, then the new value fills the gap. That is exactly what list.insert, ArrayList.add(index, value) or vector::insert do.

The guarantee index[i] <= i matters: when the ith value arrives, target holds i values, so position index[i] is either inside the list or right at its end. No insertion ever needs an empty hole.

Approach: Simulate the insertions

  1. Start with an empty list target.
  2. For i from 0 to n - 1, insert nums[i] at position index[i]. Values at that position and after it shift one place right.
  3. Return target (convert to an array in languages that need one).

Step-by-step example

nums = [0,1,2,3,4], index = [0,1,2,2,1]
  1. Insert 0 at position 0: target = [0].
  2. Insert 1 at position 1 (the end): target = [0,1].
  3. Insert 2 at position 2 (the end): target = [0,1,2].
  4. Insert 3 at position 2: 2 slides right, target = [0,1,3,2].
  5. Insert 4 at position 1: 1, 3 and 2 slide right, target = [0,4,1,3,2].

Code: Python, Java, C++

1class Solution:
2    def createTargetArray(self, nums: List[int], index: List[int]) -> List[int]:
3        target = []
4        for i in range(len(nums)):
5            target.insert(index[i], nums[i])
6        return target
Press play on the visualizer and the running line lights up here.

Time and space complexity

Time

O(n^2)

Each insert can shift up to n values. With n at most 100 that is at most a few thousand moves.

Space

O(n)

The target array itself; no other storage grows with n.

Other approaches

Compute final positions afterwards (time O(n log n), space O(n))

Walk the pairs from last to first and, for each value, find the index[i]-th position not yet taken. With a Fenwick tree that search is O(log^2 n) or O(log n), which pays off only for very large inputs.

Adjust earlier positions (time O(n^2), space O(n))

Record a position for each value; when a new value lands at p, add one to every recorded position that is at least p. It avoids moving values but still does O(n) work per step.

Common mistakes

  • Writing target[index[i]] = nums[i] into a fixed-size array. That overwrites a value instead of shifting the tail right.
  • Shifting from the left when doing it by hand. Copy the tail starting from the end, or you smear one value across the whole tail.
  • Allocating the result before the loop and forgetting that the list grows by one per step.

Frequently asked questions

Why is index[i] <= i important?

Before step i the target has exactly i values, so valid insert positions are 0 to i. The guarantee means every insert lands inside the list or at its end.

Is O(n^2) acceptable here?

Yes. n is at most 100, so the simulation is tiny. An O(n log n) method exists but is not worth its complexity at this size.

What does a later insertion do to earlier values?

It pushes every value at or after its position one place right. Values before that position never move.