Similar Problems
Similar Problems not available
Create Target Array In The Given Order - Leetcode Solution
Companies:
LeetCode: Create Target Array In The Given Order Leetcode Solution
Difficulty: Easy
Topics: array simulation
Problem Statement: 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. Return the target array.
Example: 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]
Solution: To solve this problem we need to iterate through the given input arrays one by one. While iterating through the arrays we need to take the value from the nums array and insert it at the given index in the target array.
For insertion into the target array, we will start by initializing an empty array called target. Next, we will iterate through the index array and insert values from the nums array at the given index. For insertion, we can use built-in insert() method of python lists.
Here is the python code for the solution of Create Target Array in the Given Order problem:
class Solution: def createTargetArray(self, nums: List[int], index: List[int]) -> List[int]: target = [] for i in range(len(index)): target.insert(index[i], nums[i]) return target
The above code will give the output for the given example:
Output: [0,4,1,3,2]
For nums = [0,1,2,3,4], index = [0,1,2,2,1]
Create Target Array In The Given Order Solution Code
1