Create Target Array in the Given Order (LeetCode 1389): Solution and Explanation
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
- Start with an empty list target.
- 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.
- 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]
- Insert 0 at position 0: target = [0].
- Insert 1 at position 1 (the end): target = [0,1].
- Insert 2 at position 2 (the end): target = [0,1,2].
- Insert 3 at position 2: 2 slides right, target = [0,1,3,2].
- 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 target1class Solution {
2 public int[] createTargetArray(int[] nums, int[] index) {
3 List<Integer> target = new ArrayList<>();
4 for (int i = 0; i < nums.length; i++) {
5 target.add(index[i], nums[i]);
6 }
7 int[] result = new int[target.size()];
8 for (int i = 0; i < result.length; i++) {
9 result[i] = target.get(i);
10 }
11 return result;
12 }
13}1class Solution {
2public:
3 vector<int> createTargetArray(vector<int>& nums, vector<int>& index) {
4 vector<int> target;
5 for (int i = 0; i < (int)nums.size(); i++) {
6 target.insert(target.begin() + index[i], nums[i]);
7 }
8 return target;
9 }
10};Time and space complexity
O(n^2)
Each insert can shift up to n values. With n at most 100 that is at most a few thousand moves.
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.