You are given an integer array nums of length n.
Construct a new array ans of length 2 * n such that the first n elements are the same as nums, and the next n elements are the elements of nums in reverse order.
Formally, for 0 <= i <= n - 1:
ans[i] = nums[i]ans[i + n] = nums[n - i - 1]Return an integer array ans.
Example 1:
Input: nums = [1,2,3]
Output: [1,2,3,3,2,1]
Explanation:
The first n elements of ans are the same as nums.
For the next n = 3 elements, each element is taken from nums in reverse order:
ans[3] = nums[2] = 3ans[4] = nums[1] = 2ans[5] = nums[0] = 1Thus, ans = [1, 2, 3, 3, 2, 1].
Example 2:
Input: nums = [1]
Output: [1,1]
Explanation:
The array remains the same when reversed. Thus, ans = [1, 1].
Constraints:
1 <= nums.length <= 1001 <= nums[i] <= 100Problem Overview: You receive an integer array nums. Build a new array where the first half is the original array and the second half is the reverse of that same array. If nums = [1,2,3], the result becomes [1,2,3,3,2,1]. The task mainly tests basic array manipulation and careful index handling.
Approach 1: Build Reverse Then Concatenate (O(n) time, O(n) space)
The most straightforward method is to explicitly compute the reversed array and append it to the original. Iterate through nums from the end to the beginning, pushing elements into a new list. After that, concatenate the original array with the reversed one. This approach mirrors how many high‑level languages implement reversing internally. The algorithm performs two linear traversals: one to copy the original and one to generate the reversed sequence. Because a new array of size 2n is created, the extra space complexity is O(n). This version is easy to read and often preferred in quick implementation scenarios.
Approach 2: Single Pass Construction (O(n) time, O(n) space)
A cleaner solution constructs the result array of size 2n in a single loop. Allocate an output array with length 2 * n. For every index i, place nums[i] at position i and place the mirrored element at position 2n - 1 - i. This effectively writes the reversed portion while iterating forward. The key insight is that the reverse index can be derived directly without creating a temporary reversed array. Only one traversal is required, making the algorithm O(n) time while still using O(n) space for the result.
Approach 3: Two-Pointer Fill (O(n) time, O(n) space)
This approach uses a two‑pointer style technique to fill the result array from both ends simultaneously. Initialize pointers at the start and end of the result array. Iterate through nums, placing each value at the front pointer and the same value at the mirrored position at the back. Move the front pointer forward and the back pointer backward each step. Conceptually this is similar to the single-pass approach but framed as a symmetric fill operation. It highlights how mirror indices work in many array problems.
Recommended for interviews: The single-pass construction approach is usually what interviewers expect. It demonstrates that you understand index relationships and can avoid unnecessary intermediate arrays. The brute-force reverse + concatenate method still shows basic problem solving, but the single-pass version communicates stronger control over array operations and memory usage while keeping the same O(n) time complexity.
Solutions for this problem are being prepared.
Try solving it yourself| Approach | Time | Space | When to Use |
|---|---|---|---|
| Build Reverse Then Concatenate | O(n) | O(n) | Simplest implementation; good for readability and quick coding |
| Single Pass Construction | O(n) | O(n) | Most efficient and clean approach for interviews |
| Two-Pointer Fill | O(n) | O(n) | Useful when reasoning about mirrored indices or symmetric array operations |
Practice Concatenate Array With Reverse with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor