Problem Description
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.
Key Insights
- The output array should be exactly double the size of the input array, containing two copies of the input array.
- The indices of the output array can be split into two segments: the first segment (0 to n-1) mirrors the input array, and the second segment (n to 2n-1) is identical to the first segment.
- The problem requires simple array manipulation and is straightforward in terms of implementation.
Space and Time Complexity
Time Complexity: O(n)
Space Complexity: O(n)
Solution
To solve this problem, we can use a simple array concatenation approach. We will create a new array of size 2n and fill it with the elements of the input array nums
. The first half of the new array will be filled with the elements of nums
, and the second half will be a duplicate of the first half. This can be achieved using a loop that iterates through the input array.