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:
The brute force approach to concatenating an array involves creating a new array that's twice the size of the original. We then directly copy the elements from the original array into the first half and then again into the second half.
Here's how the algorithm would work step-by-step:
def concatArrayBruteForce(originalArray):
arrayLength = len(originalArray)
# Create a new array with double the size of original
concatenatedArray = [0] * (2 * arrayLength)
# Copy the elements from the original array
for index in range(arrayLength):
concatenatedArray[index] = originalArray[index]
# Copy the original array again to the second half
for index in range(arrayLength):
secondHalfIndex = index + arrayLength
concatenatedArray[secondHalfIndex] = \
originalArray[index]
return concatenatedArrayWe want to create a new, longer list by simply sticking the original list to the end of itself. The best way to do this is by figuring out how long the final list needs to be and then filling it piece by piece in a clever way.
Here's how the algorithm would work step-by-step:
def concatenation_of_array(original_list):
original_length = len(original_list)
# The new list needs to be twice the size of the original.
concatenated_list = [0] * (2 * original_length)
# Copy the original list to the first half of the new list.
for index in range(original_length):
concatenated_list[index] = original_list[index]
# Copy the original list again to the second half of the new list.
for index in range(original_length):
concatenated_list[index + original_length] = original_list[index]
return concatenated_list| Case | How to Handle |
|---|---|
| Null input array | Throw an IllegalArgumentException or return null to indicate invalid input, depending on requirements. |
| Empty input array | Return an empty array as the concatenation of an empty array with itself is an empty array. |
| Array with a single element | The concatenation will be an array with two identical elements; handle normally. |
| Array with maximum allowed size (e.g., Integer.MAX_VALUE) leading to potential memory overflow | Ensure the target programming language and environment can allocate sufficient memory or return an error if allocation fails. |
| Array containing Integer.MAX_VALUE or Integer.MIN_VALUE | The solution should handle these extreme values without causing integer overflow errors, assuming operations like addition or multiplication are not performed on the numbers. |
| Array with all identical values | The concatenation will simply be the array repeated twice, which the general algorithm handles correctly. |
| Array with negative numbers | The solution should handle negative numbers correctly, as they are valid integers and should be concatenated normally. |
| Array with zero values | Zero values should be treated as any other integer and handled correctly during concatenation. |