Concatenation of Array
Easy
Topics
Given an array nums of length n, return an array ans of length 2n where ans[i] = nums[i] and ans[i+n] = nums[i].
Example 1
ans[i] = nums[i % n] for 6 cells. Simple, but the modulo indexing is the same idea that circular-array problems lean on.Input: nums = [1,2,1] Output: [1,2,1,1,2,1]
Example 2
Input: nums = [1,3,2,1] Output: [1,3,2,1,1,3,2,1]
Constraints
- 1 <= nums.length <= 1000
- 1 <= nums[i] <= 1000
Editorial — intuition, approach, complexity and pitfalls
Intuition
The task is to return an array of length 2n where ans[i] == nums[i] and ans[i + n] == nums[i] — the input written out twice, back to back.
There is no algorithm to find here, and that is worth being honest about. The value of the problem is as a check on index arithmetic and on knowing what your language gives you. In Python nums + nums says it exactly. In Java and C++ there is no concatenation operator for arrays, so you allocate 2n and fill it, and then the only thing that can go wrong is the index.
The one genuinely instructive point: a single loop writing both halves is better than two loops, because it halves the loop overhead and makes the relationship between the two positions explicit:
for i in 0..n-1:
ans[i] = nums[i]
ans[i + n] = nums[i]Approach
- Allocate the result with
2nslots, or use the language's concatenation if it has one. - For each
i, writenums[i]to bothiandi + n.
Dry Run
nums = [1, 2, 1] n = 3
i=0: ans[0] = 1, ans[3] = 1 -> [1, _, _, 1, _, _]
i=1: ans[1] = 2, ans[4] = 2 -> [1, 2, _, 1, 2, _]
i=2: ans[2] = 1, ans[5] = 1 -> [1, 2, 1, 1, 2, 1]
answer [1,2,1,1,2,1]Complexity
- Time — O(n): each of the
2noutput slots is written once. - Space — O(n): the output array, which is
2nand therefore O(n). This is unavoidable here — the result is larger than the input, so there is no in-place variant, and the output does not count as auxiliary space by the usual convention. If an interviewer asks for O(1) extra space, the honest answer is that you already have it: nothing beyond the required output is allocated.
Common Pitfalls
- Appending in a loop without pre-sizing, in a language where that reallocates. Amortised it is still O(n), but pre-allocating
2navoids the copies outright. - Writing
ans[i + nums.length]after already mutating or resizingnums. Capturenbefore touching anything. - Using
nums + numsin JavaScript, where+on arrays coerces both to strings and produces"1,2,11,2,1". It needsnums.concat(nums)or a spread. - Returning a view or a shared reference rather than a copy, in languages where slicing aliases. Mutating the result must not change the input.
- Reaching for
memcpy-style tricks to look clever. The single loop writing both halves is clearer and the same cost.
Full Solution — all languages
class Solution:
def getConcatenation(self, nums):
return nums + numsimport java.util.*;
class Solution {
public int[] getConcatenation(int[] nums) {
int n = nums.length;
int[] ans = new int[2 * n];
for (int i = 0; i < n; i++) {
ans[i] = nums[i];
ans[i + n] = nums[i];
}
return ans;
}
}#include <bits/stdc++.h>
using namespace std;
class Solution {
public: vector<int> getConcatenation(vector<int>& nums) {
vector<int> ans(nums);
ans.insert(ans.end(), nums.begin(), nums.end());
return ans;
}
};/**
* @param {number[]} nums
* @return {number[]}
*/ var getConcatenation = function(nums) {
return nums.concat(nums);
};