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.length
1 <= n <= 1000
1 <= nums[i] <= 1000
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
. ans
is the concatenation of two nums
arrays. Return the array ans
.
The most straightforward approach is to simulate the concatenation directly. We can create a new array ans
twice the size of nums
and populate it by copying the elements of nums
into the first half and then again into the second half. Alternatively, many languages offer concise ways to concatenate arrays directly, providing a more efficient solution.
The time complexity is O(n), where n is the length of the input array nums
. This is because we iterate through the input array once to copy its elements, and then iterate through the newly created array to populate it. The array concatenation methods in most modern languages are typically optimized to perform in linear time.
The space complexity is O(n) because we create a new array ans
with twice the size of the input array nums
to store the result.
class Solution:
def getConcatenation(self, nums: List[int]) -> List[int]:
return nums + nums # Efficient array concatenation in Python
This Python solution leverages Python's list concatenation operator (+
) which is optimized for this operation.
class Solution {
public int[] getConcatenation(int[] nums) {
int n = nums.length;
int[] ans = new int[2 * n];
System.arraycopy(nums, 0, ans, 0, n); //Efficient copying in Java
System.arraycopy(nums, 0, ans, n, n);
return ans;
}
}
The Java solution uses System.arraycopy()
for efficient copying of array elements, which is generally faster than manual looping.
class Solution {
public:
vector<int> getConcatenation(vector<int>& nums) {
vector<int> ans = nums; //Efficient copy constructor
ans.insert(ans.end(), nums.begin(), nums.end()); //Efficient insertion at the end in C++
return ans;
}
};
C++ utilizes the copy constructor for creating a copy of the original vector and then the insert()
method for efficient concatenation.
var getConcatenation = function(nums) {
return nums.concat(nums); //Efficient concatenation in JavaScript
};
JavaScript's concat()
method provides a straightforward and efficient way to concatenate arrays.
func getConcatenation(nums []int) []int {
return append(nums, nums...) // Efficient append in Go
}
Go's append()
function is highly optimized for array manipulation, making this a concise and efficient solution.
Similar efficient approaches using built-in array concatenation or copying functions exist in other languages as well. The core idea remains the same: leverage language-specific features for optimized array manipulation to achieve linear time and space complexity.