Given two arrays of integers nums and index, create a target array: it starts empty, and from left to right you read nums[i] and index[i] and insert the value nums[i] at position index[i] in target. Repeat until there are no elements left to read. Return the target array. It is guaranteed that the insertion operations will be valid.
Given two arrays of integers nums and index, create a target array: it starts empty, and from left to right you read nums[i] and index[i] and insert the value nums[i] at position index[i] in target. Repeat until there are no elements left to read. 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]
Constraints:
1≤nums.length,index.length≤100
nums.length==index.length
0≤nums[i]≤100
0≤index[i]≤i
Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result
Run code to see results.
Input
arr ={"nums":[0,1,2,3,4],"index":[0,1,2,2,1]}
target starts empty. Read the 5 (value, position) pairs left to right and insert each value at its position.
instructions: insert nums[i] at index[i]
i
0
1
2
3
4
nums
0
1
2
3
4
index
0
1
2
2
1
0
1
2
3
4
target has 0 of 5 values; orange cells just slid right, blue is the value being placed