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
.
Input: nums = [1, 2, 1]
Output: [1, 2, 1, 1, 2, 1]
Input: nums = [1, 3, 2, 1]
Output: [1, 3, 2, 1, 1, 3, 2, 1]
Q:
Are the elements in nums
guaranteed to be integers?
A:
Yes, the problem statement specifies an integer array.
Q:
Can nums
be empty?
A:
According to the problem description, no constraints are specified regarding the emptiness of the nums
array. It’s assumed to be a non-empty array as per usual array input conventions in such problems.
Q:
Is there a constraint on the size of nums
?
A:
The typical constraint for such problems is usually around 1 <= nums.length <= 1000
and -1000 <= nums[i] <= 1000
, but these specifics would normally be found in the problem details.
The problem can be solved directly using list concatenation operations in Python:
nums
array.The naive and efficient way to achieve this is by using the +
operator to concatenate the list with itself.
def getConcatenation(nums: List[int]) -> List[int]:
return nums + nums
O(n)
, where n
is the length of the input array nums
. This is because the list concatenation involves copying each element exactly once.O(n)
, because we are creating a new list of size 2n
.This approach should efficiently solve the given problem within the typical constraints expected in such coding interviews.
Got blindsided by a question you didn’t expect?
Spend too much time studying?
Or simply don’t have the time to go over all 3000 questions?