Concatenation of Array

Easy

Topics
ArraySimulation

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

102112132415
first copysecond copy
The result is the input twice over, so ans[i] = nums[i % n] for 6 cells. Simple, but the modulo indexing is the same idea that circular-array problems lean on.
Cells are the array values; the small numbers underneath are indices.
Input:  nums = [1,2,1]
Output: [1,2,1,1,2,1]

Example 2

1031221314352617
first copysecond copy
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

  1. Allocate the result with 2n slots, or use the language's concatenation if it has one.
  2. For each i, write nums[i] to both i and i + 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 2n output slots is written once.
  • Space — O(n): the output array, which is 2n and 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 2n avoids the copies outright.
  • Writing ans[i + nums.length] after already mutating or resizing nums. Capture n before touching anything.
  • Using nums + nums in JavaScript, where + on arrays coerces both to strings and produces "1,2,11,2,1". It needs nums.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 + nums
SensAI
Beta
Tap the mic to stop voice mode