Create Target Array in the Given Order
EasyUpdated: Aug 2, 2025
Practice on:
Problem
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 valuenums[i]in target array. - Repeat the previous step until there are no elements to read in
numsandindex.
Return the target array.
It is guaranteed that the insertion operations will be valid.
Examples
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 <= 100nums.length == index.length0 <= nums[i] <= 1000 <= index[i] <= i
Solution
Method 1 – Simulation with List Insertion
Intuition
The key idea is to simulate the process described in the problem: for each value in nums, insert it at the specified position in index. This is efficient for small arrays and directly models the problem statement.
Approach
- Initialize an empty list
ans. - Iterate through each pair
(num, idx)fromnumsandindex:- Insert
numat positionidxinans.
- Insert
- Return
ansafter all insertions.
Code
C++
class Solution {
public:
vector<int> createTargetArray(vector<int>& nums, vector<int>& index) {
vector<int> ans;
for (int i = 0; i < nums.size(); ++i) {
ans.insert(ans.begin() + index[i], nums[i]);
}
return ans;
}
};
Go
func createTargetArray(nums []int, index []int) []int {
ans := []int{}
for i, v := range nums {
idx := index[i]
ans = append(ans, 0)
copy(ans[idx+1:], ans[idx:])
ans[idx] = v
}
return ans
}
Java
class Solution {
public int[] createTargetArray(int[] nums, int[] index) {
List<Integer> ans = new ArrayList<>();
for (int i = 0; i < nums.length; i++) {
ans.add(index[i], nums[i]);
}
int[] res = new int[ans.size()];
for (int i = 0; i < ans.size(); i++) res[i] = ans.get(i);
return res;
}
}
Kotlin
class Solution {
fun createTargetArray(nums: IntArray, index: IntArray): IntArray {
val ans = mutableListOf<Int>()
for (i in nums.indices) {
ans.add(index[i], nums[i])
}
return ans.toIntArray()
}
}
Python
class Solution:
def createTargetArray(self, nums: list[int], index: list[int]) -> list[int]:
ans = []
for v, i in zip(nums, index):
ans.insert(i, v)
return ans
Rust
impl Solution {
pub fn create_target_array(nums: Vec<i32>, index: Vec<i32>) -> Vec<i32> {
let mut ans = Vec::new();
for (v, &i) in nums.iter().zip(index.iter()) {
ans.insert(i as usize, *v);
}
ans
}
}
TypeScript
class Solution {
createTargetArray(nums: number[], index: number[]): number[] {
const ans: number[] = [];
for (let i = 0; i < nums.length; i++) {
ans.splice(index[i], 0, nums[i]);
}
return ans;
}
}
Complexity
- ⏰ Time complexity:
O(n^2), as each insertion may take O(n) time and there are n insertions. - 🧺 Space complexity:
O(n), for the result array.