Create Target Array in the Given Order - Complete Solution Guide
Create Target Array in the Given Order is LeetCode problem 1389, a Easy level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
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. 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] Explana
Detailed Explanation
The problem asks you to create a target array from two input arrays: `nums` and `index`. `nums` contains the values to be inserted into the target array, and `index` specifies the positions where these values should be inserted. The insertion happens sequentially, starting from the first element of `nums` and `index`. The target array is initially empty. For each `i`, `nums[i]` is inserted into the target array at the index specified by `index[i]`. The problem guarantees that the `index` values are always valid (within the bounds of the current size of the target array during insertion).
Solution Approach
The provided solutions use a straightforward approach: they iterate through the `nums` and `index` arrays simultaneously. For each pair `(nums[i], index[i])`, they insert `nums[i]` into the target array at position `index[i]`. This is done using built-in functions such as `insert()` for `vector` (C++), `add()` for `ArrayList` (Java), and the `insert()` method for Python lists. The C solution manually handles the element shifting, reflecting the lower level control.
Step-by-Step Algorithm
- Initialize an empty target array (or list).
- Iterate through the `nums` and `index` arrays using a loop, from `i = 0` to `i = length(nums) - 1`.
- In each iteration, insert `nums[i]` at the index `index[i]` in the target array. This may involve shifting existing elements if `index[i]` is not at the end of the array.
- After the loop completes, the target array will contain all the elements from `nums` inserted at the positions specified by `index`.
- Return the target array.
Key Insights
- The problem highlights the importance of understanding array insertion operations and how they affect existing elements. Inserting an element into an array at a specific position requires shifting the subsequent elements to make space.
- Using a dynamic array (like `ArrayList` in Java or `vector` in C++) or a list data structure is crucial for efficient insertion at arbitrary indices because it handles resizing automatically. Using a fixed-size array would necessitate manual resizing and element shifting.
- The provided constraints (`0 <= index[i] <= i`) imply that we never need to insert at an index beyond the current end of the target array. This simplifies the insertion process somewhat.
Complexity Analysis
Time Complexity: O(n^2)
Space Complexity: O(n)
Topics
This problem involves: Array, Simulation.
Companies
Asked at: Visa.