1389. Create Target Array in the Given Order LeetCode Solution

In this guide, you will get 1389. Create Target Array in the Given Order LeetCode Solution with the best time and space complexity. The solution to Create Target Array in the Given Order problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Create Target Array in the Given Order solution in C++
  4. Create Target Array in the Given Order solution in Java
  5. Create Target Array in the Given Order solution in Python
  6. Additional Resources
1389. Create Target Array in the Given Order LeetCode Solution image

Problem Statement of 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

See also  627. Swap Salary LeetCode Solution

Complexity Analysis

  • Time Complexity: O(n^2)
  • Space Complexity: O(n)

1389. Create Target Array in the Given Order LeetCode Solution in 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;
  }
};
/* code provided by PROGIEZ */

1389. Create Target Array in the Given Order LeetCode Solution in 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]);
    return ans.stream().mapToInt(Integer::intValue).toArray();
  }
}
// code provided by PROGIEZ

1389. Create Target Array in the Given Order LeetCode Solution in Python

class Solution:
  def createTargetArray(self, nums, index):
    ans = []
    for num, i in zip(nums, index):
      ans.insert(i, num)
    return ans
# code by PROGIEZ

Additional Resources

Happy Coding! Keep following PROGIEZ for more updates and solutions.