{x}
blog image

Create Target Array in the Given Order

Given two arrays of integers nums and index. Your task is to create target array under the following rules:

  • Initially target array is empty.
  • From left to right read nums[i] and index[i], insert at index index[i] the value nums[i] in target array.
  • Repeat the previous step until there are no elements to read in nums and index.

Return the target array.

It is guaranteed that the insertion operations will be valid.

 

Example 1:

Input: nums = [0,1,2,3,4], index = [0,1,2,2,1]
Output: [0,4,1,3,2]
Explanation:
nums       index     target
0            0        [0]
1            1        [0,1]
2            2        [0,1,2]
3            2        [0,1,3,2]
4            1        [0,4,1,3,2]

Example 2:

Input: nums = [1,2,3,4,0], index = [0,1,2,3,0]
Output: [0,1,2,3,4]
Explanation:
nums       index     target
1            0        [1]
2            1        [1,2]
3            2        [1,2,3]
4            3        [1,2,3,4]
0            0        [0,1,2,3,4]

Example 3:

Input: nums = [1], index = [0]
Output: [1]

 

Constraints:

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

Solution Explanation: Creating a Target Array

The problem requires creating a target array based on two input arrays: nums and index. nums[i] represents the value to be inserted, and index[i] represents the position in the target array where nums[i] should be inserted.

Approach: Simulation

The most straightforward approach is to simulate the insertion process directly. We iterate through the nums and index arrays simultaneously. For each pair (nums[i], index[i]), we insert nums[i] at the position specified by index[i] in the target array.

Code Implementation and Explanation (Python)

class Solution:
    def createTargetArray(self, nums: List[int], index: List[int]) -> List[int]:
        target = []  # Initialize an empty target array
        for i in range(len(nums)):
            target.insert(index[i], nums[i]) # Insert nums[i] at index[i] in the target array
        return target
 

Explanation:

  1. Initialization: An empty list target is created to store the resulting array.
  2. Iteration: The code iterates through the nums and index arrays using a for loop and range(len(nums)). We could also use zip to iterate over both arrays simultaneously.
  3. Insertion: The insert() method is used to insert nums[i] at the position index[i] within the target array. The insert() method shifts existing elements to the right to make space for the new element.

Time and Space Complexity Analysis

  • Time Complexity: The insert() operation in a list has a time complexity of O(n) in the worst case, where n is the current size of the list because it requires shifting elements. Since we perform this operation for each element in nums, the overall time complexity of this approach is O(n^2).

  • Space Complexity: The space complexity is O(n) because we are creating a new list target with the same size as the input nums array.

Code Implementation and Explanation (Java)

import java.util.ArrayList;
import java.util.List;
 
class Solution {
    public int[] createTargetArray(int[] nums, int[] index) {
        List<Integer> target = new ArrayList<>(); //Using ArrayList for dynamic sizing
        for (int i = 0; i < nums.length; i++) {
            target.add(index[i], nums[i]); //Add at the specified index
        }
        int[] result = new int[nums.length]; //Convert back to int[] for return
        for(int i=0; i<nums.length; i++){
            result[i] = target.get(i);
        }
        return result;
    }
}

The Java code uses an ArrayList which is more efficient for insertions than a standard array. However, the time complexity analysis remains the same as Python. We convert back to an int[] for the return value.

Other Languages

The same basic approach (simulating the insertions) can be implemented in other languages like C++, Go, Javascript, etc., with similar time and space complexity. The primary difference would be in the syntax used for list/array manipulation. For example, in C++ std::vector provides efficient insertion, while in Go, we might need to use copy to shift elements when inserting.