Skip to main content

Concatenate Array With Reverse - Solution & Explanation

EasyArraySimulation6 min read
Practice this problem

Problem Statement

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] = 3
  • ans[4] = nums[1] = 2
  • ans[5] = nums[0] = 1

Thus, 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 <= 100
  • 1 <= nums[i] <= 100

Approach Overview

Problem 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.

Solution

We create an array ans of length 2 times n. The first n elements are the same as nums, and the next n elements are nums in reverse order.

Specifically, for 0 leq i leq n - 1, we set ans[i] = nums[i] and ans[i + n] = nums[n - i - 1].

Finally, return the array ans.

The time complexity is O(n), and the space complexity is O(n), where n is the length of the array nums.

Code

Python

Java

C++

Go

TypeScript

Try this approach in the editor →

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Build Reverse Then ConcatenateO(n)O(n)Simplest implementation; good for readability and quick coding
Single Pass ConstructionO(n)O(n)Most efficient and clean approach for interviews
Two-Pointer FillO(n)O(n)Useful when reasoning about mirrored indices or symmetric array operations

Video Solution

Concatenate Array With Reverse | LeetCode 3925 | Weekly Contest 501 | Java | Developer Coder • Developer Coder • 144 views views

Watch 8 more video solutions →

Frequently Asked Questions

Is Concatenate Array With Reverse easy or hard?
Concatenate Array With Reverse is considered an Easy problem. It focuses on understanding array indexing and reversing logic rather than complex algorithms or data structures.
Concatenate Array With Reverse Python/Java solution
In Python, you can build the result with nums + nums[::-1] or by filling a preallocated list using index calculations. In Java, create an int array of size 2*n and assign result[i] = nums[i] and result[2*n-1-i] = nums[i] inside a loop.
How to solve Concatenate Array With Reverse in O(n)?
Allocate a result array with length twice the input size. Iterate from i = 0 to n-1, assign result[i] = nums[i] and result[2n - 1 - i] = nums[i]. This fills the original and reversed parts simultaneously using one linear traversal.
What is the best approach for Concatenate Array With Reverse?
The best approach constructs the result array in a single pass. Allocate an array of size 2n and fill the first half with nums[i] while placing the same value at index 2n-1-i for the reverse portion. This avoids building a separate reversed array and runs in O(n) time with O(n) space.
Is Concatenate Array With Reverse asked at Google/Amazon/Meta?
This exact problem is categorized as an Easy array manipulation question. While the exact wording may not appear in major interviews, similar tasks involving reversing arrays, constructing mirrored arrays, or handling index symmetry appear frequently in coding screens.
What data structure is used in Concatenate Array With Reverse?
The problem relies on basic array operations. The solution typically uses a new array of size 2n to store the original sequence followed by its reversed order. No advanced data structures are required.
What is the time complexity of Concatenate Array With Reverse?
The optimal solution runs in O(n) time because each element of the input array is processed exactly once. A result array of size 2n must be created, so the auxiliary space complexity is O(n).

Ready to solve this problem?

Practice Concatenate Array With Reverse with our built-in code editor and test cases.

Practice on FleetCode