1929. Concatenation of Array - Explanation
Description
You are given an integer array nums of length n. Create an array ans of length 2n where ans[i] == nums[i] and ans[i + n] == nums[i] for 0 <= i < n (0-indexed).
Specifically, ans is the concatenation of two nums arrays.
Return the array ans.
Example 1:
Input: nums = [1,4,1,2]
Output: [1,4,1,2,1,4,1,2]Example 2:
Input: nums = [22,21,20,1]
Output: [22,21,20,1,22,21,20,1]Constraints:
1 <= nums.length <= 1000.1 <= nums[i] <= 1000
Topics
Prerequisites
Before attempting this problem, you should be comfortable with:
- Arrays - Understanding array indexing and how to create arrays of a specific size
- Basic Iteration - Using loops to traverse and populate arrays
1. Iteration (Two Pass)
Intuition
To concatenate an array with itself, we need to create a new array that contains all elements of the original array twice, maintaining the same order. The elements at indices to are followed by the same elements at indices to .
For example, if nums = [1, 2, 3]:
- The first three elements of
answill benums[0],nums[1],nums[2]->[1, 2, 3] - The next three elements of
answill also benums[0],nums[1],nums[2]->[1, 2, 3] - Result:
[1, 2, 3, 1, 2, 3]
Algorithm
- Initialize an empty result list or an array
ansof size2n, wherenis the length of the input array. - Use a loop that runs twice.
- Inside that loop, iterate through every element
numin the input arraynums. - Append
numto the result list or assign it to the next available index in the result array. - Return the resulting array.
Time & Space Complexity
- Time complexity: where is the length of the input array. We iterate through the array twice, performing operations.
- Space complexity: if we consider the space required for the output array of size .
2. Iteration (One Pass)
Intuition
The problem defines the result array ans such that ans[i] == nums[i] and ans[i + n] == nums[i] for 0 <= i < n. Instead of looping through the input twice, we can fill both required positions in the result array simultaneously while iterating through the input array just once. This utilizes the index mapping i and i + n directly.
Algorithm
- Determine the length
nof the input array. - Initialize a result array
ansof size2n. - Iterate through the input array
numsusing an indexifrom0ton - 1. - For each element at index
i:- Set
ans[i] = nums[i]. - Set
ans[i + n] = nums[i].
- Set
- Return the resulting array.
Time & Space Complexity
- Time complexity: where is the length of the input array. Although we iterate through the input once, we still perform total writes to the output array.
- Space complexity: as we must allocate an array of size for the output.
Common Pitfalls
Incorrect Result Array Size
Allocating an array of size n instead of 2n causes an index out of bounds error when writing to the second half.
# Wrong
ans = [0] * n
# Correct
ans = [0] * (2 * n)Off-by-One When Using Index Offset
When using the one-pass approach with ans[i + n] = nums[i], forgetting that indices are zero-based or miscalculating the offset leads to incorrect placement of elements in the second half.
Sign in to join the discussion