Skip to content
← All posts

Create Target Array in the Given Order: Simulation Pattern

4 min read
leetcodeproblemeasyarrayssimulation

You are given two integer arrays nums and index, both of the same length. Your task is to create a target array by processing each element in order: for each position i, insert nums[i] at position index[i] in the target array. Return the resulting target array. This is LeetCode 1389: Create Target Array in the Given Order.

nums0011223344index01221
For each i, insert nums[i] at position index[i] in the target array. Blue = nums, orange = index.

Why this problem matters

Array insertion is one of the most fundamental operations you will use in programming. This problem forces you to think carefully about what happens when you insert an element at a specific index: existing elements shift to the right to make room. Understanding this shifting behavior is critical for problems involving list manipulation, reordering, and simulation. It also reinforces your understanding of how Python's list.insert() method works under the hood.

The key insight

You do not need any clever algorithm here. The problem is a direct simulation: walk through nums and index together, and for each pair, call insert on the target list. Python's list.insert(i, x) places x at index i and shifts everything at index i and beyond one position to the right. That is exactly what the problem asks you to do.

The solution

def create_target_array(nums: list[int], index: list[int]) -> list[int]:
    target = []
    for i in range(len(nums)):
        target.insert(index[i], nums[i])
    return target

The code mirrors the problem statement almost word for word. We start with an empty target list. For each position i, we insert nums[i] at position index[i] using Python's built-in list.insert(). The method handles all the shifting of existing elements automatically.

The loop runs once for each element in nums, and each call to insert places the value at exactly the right spot. After all insertions are done, target contains the final array.

Python's list.insert(i, x) inserts x before the element currently at index i. If i equals the length of the list, it appends to the end. This means you never need to handle "insert at the end" as a special case.

Visual walkthrough

Let's trace through the example with nums = [0, 1, 2, 3, 4] and index = [0, 1, 2, 2, 1]. Watch how elements shift when a new value is inserted in the middle of the array.

Step 1: Insert nums[0]=0 at index[0]=0

target:00

Target is empty. Insert 0 at position 0. target = [0]

Step 2: Insert nums[1]=1 at index[1]=1

target:0011

Insert 1 at position 1 (the end). target = [0, 1]

Step 3: Insert nums[2]=2 at index[2]=2

target:001122

Insert 2 at position 2 (the end). target = [0, 1, 2]

Step 4: Insert nums[3]=3 at index[3]=2

target:00113223

Insert 3 at position 2. The existing element 2 shifts right. target = [0, 1, 3, 2]

Step 5: Insert nums[4]=4 at index[4]=1

target:0041123324

Insert 4 at position 1. Elements 1, 3, 2 all shift right. target = [0, 4, 1, 3, 2]

The interesting steps are 4 and 5. In step 4, inserting 3 at position 2 pushes the existing value 2 to the right. In step 5, inserting 4 at position 1 pushes three elements (1, 3, and 2) to the right. The final target array is [0, 4, 1, 3, 2].

Complexity analysis

ApproachTimeSpace
List insertionO(n^2)O(n)

Time is O(n^2) in the worst case. There are n insertion operations, and each list.insert() call takes O(n) time because it may need to shift all existing elements to the right. If every insertion goes at position 0, the total work is 1 + 2 + 3 + ... + n = O(n^2).

Space is O(n) for the target array, which holds exactly n elements when the algorithm finishes.

The building blocks

1. List insertion at a specific index

target = [10, 20, 30]
target.insert(1, 99)

After this call, target is [10, 99, 20, 30]. The value 99 goes at index 1, and everything from index 1 onward shifts right by one position. This is the core operation for the entire problem. Once you know how insert works, the solution writes itself.

2. Parallel iteration over two arrays

for i in range(len(nums)):
    process(nums[i], index[i])

Walking two arrays in lockstep by index is a pattern you will see constantly. Here it pairs each value with its target position. You could also use zip(nums, index) for a more Pythonic version, but indexing is clearer when the problem uses explicit indices.

Edge cases

  • Single element: nums = [1], index = [0]. Just one insertion, result is [1].
  • All insertions at position 0: Every new element goes to the front, pushing all previous elements right. This produces the reverse of nums.
  • All insertions at the end: Each index[i] equals i, so no shifting happens. The target array is a copy of nums.
  • Duplicate values in nums: The algorithm handles duplicates naturally since it is just inserting values regardless of whether they already exist.

From understanding to recall

The simulation pattern is simple to understand, but you want to be able to write it without hesitation. The key detail to internalize is how list.insert() shifts elements. Under interview pressure, people sometimes confuse whether insert(i, x) places x before or after index i, or they worry about off-by-one errors at the boundaries. Practicing this pattern with spaced repetition removes that uncertainty. You write target.insert(index[i], nums[i]) from muscle memory, and you move on to the next problem.

Related posts

  • Merge Sorted Array - Another problem involving inserting elements at specific positions
  • Remove Element - The inverse operation: removing elements while maintaining order
  • Rotate Array - Array manipulation that requires understanding index-based operations

CodeBricks breaks Create Target Array in the Given Order into its list insertion and parallel iteration building blocks, then drills them independently with spaced repetition. You type each piece from scratch until the pattern is automatic. When a simulation problem shows up in your interview, you do not think about it. You just write it.