Create Target Array in the Given Order

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.

30:00

Create Target Array in the Given Order
easy
Free written solution
Topics
Companies

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≤1001 \leq \text{nums.length}, \text{index.length} \leq 100

  • nums.length==index.length\text{nums.length} == \text{index.length}

  • 0≤nums[i]≤1000 \leq \text{nums}[i] \leq 100

  • 0≤index[i]≤i0 \leq \text{index}[i] \leq 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

Variables
VariableValue
size0
DepthFunction Call
Stack empty
0/13