#3925
Easy Algorithms Concatenate array with reverse
92.0% acceptance
May 13, 2026
16
1
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.
Solution
Rust
Time O(n)
Space O(n)
impl Solution {
pub fn concat_with_reverse(nums: Vec<i32>) -> Vec<i32> {
let n = nums.len();
let mut ans = Vec::with_capacity(2 * n);
ans.extend_from_slice(&nums);
ans.extend(nums.iter().rev());
ans
}
}