Given an integer array nums of length n, you want to 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,2,1] Output: [1,2,1,1,2,1] Explanation: The array ans is formed as follows: - ans = [nums[0],nums[1],nums[2],nums[0],nums[1],nums[2]] - ans = [1,2,1,1,2,1]
Example 2:
Input: nums = [1,3,2,1] Output: [1,3,2,1,1,3,2,1] Explanation: The array ans is formed as follows: - ans = [nums[0],nums[1],nums[2],nums[3],nums[0],nums[1],nums[2],nums[3]] - ans = [1,3,2,1,1,3,2,1]
Constraints:
n == nums.length1 <= n <= 10001 <= nums[i] <= 1000When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
Imagine you have a row of items and need to create a brand new row that contains the exact same items followed by a duplicate copy of those same items. The brute force approach achieves this by physically gathering a fresh collection and copying everything over twice from the original.
Here's how the algorithm would work step-by-step:
def get_concatenation(original_array):
# Allocate space for twice the items to fit both copies sequentially without resizing
result_array = [0] * (len(original_array) * 2)
# Iterate through the original items twice to populate the first and second halves
for current_index in range(len(original_array) * 2):
# Map the doubled index back to the original array bounds using modulo arithmetic
source_index = current_index % len(original_array)
result_array[current_index] = original_array[source_index]
return result_arrayTo double a line of items efficiently, the best strategy is to create a fresh container that is twice as long as the original. You then simply copy the entire original group of items into the first half of the new container, and then copy the exact same group a second time right into the remaining empty half.
Here's how the algorithm would work step-by-step:
class Solution:
def get_concatenation(self, input_array: list[int]) -> list[int]:
# We need a new container twice the size to hold the original elements twice.
original_length = len(input_array)
new_container = [0] * (original_length * 2)
# Copying elements twice ensures the array is successfully concatenated.
for index in range(original_length):
new_container[index] = input_array[index]
new_container[index + original_length] = input_array[index]
return new_container| Case | How to Handle |
|---|---|
| Empty array input where n equals 0 | The algorithm should allocate an empty array of length zero and return it safely. |
| Single element array where n equals 1 | The allocation creates a new array of length 2 and populates both indices with the single element. |
| Maximum allowed array length reaching memory limits | The solution must efficiently allocate a contiguous block of memory of size 2n without excessive overhead. |
| Array containing negative numbers and zeros | Value types do not affect structural concatenation, so elements are copied directly regardless of their sign. |
| Array with all identical elements | Duplicating the array simply repeats the same value seamlessly across the first and second halves. |
| Memory allocation failure due to very large input sizes | The system will throw an out-of-memory exception if the resulting array exceeds available heap space. |
| Language-specific primitive type overflow during sizing | Computing the new length as 2n must use standard integer types that comfortably fit within valid array bounds. |
| Read-only or immutable input array structures | Since we only read from the input and build a brand new array, immutability of the source is fully preserved. |