Skip to content
← All posts

Concatenation of Array: Simple Array Construction

4 min read
leetcodeproblemeasyarrays

LeetCode Concatenation of Array is problem 1929. Given an integer array nums of length n, you need to create an array ans of length 2n where ans[i] == nums[i] and ans[i + n] == nums[i] for 0 <= i < n. In plain terms, just concatenate nums with itself and return the result.

nums1i=02i=11i=2ans = nums + numsans1i=02i=11i=21i=32i=41i=5first copysecond copy
The result array is just nums repeated twice. The first half (blue) mirrors the original, and the second half (green) is an identical copy.

Why this problem matters

Concatenation of Array is rated easy, and the solution is short. But it is a great warm-up for understanding how arrays are constructed from scratch. The problem forces you to think about index mapping: for any position in the result, where does its value come from? That question shows up in dozens of harder problems.

If you can answer "what goes at index i?" clearly for this problem, you are building the same muscle you will need for rotation, permutation, and transformation problems later on.

The approach

The problem tells you exactly what to do. For each index i in the range [0, 2n):

  • If i < n, the value comes from nums[i]
  • If i >= n, the value comes from nums[i - n]

Both cases simplify to nums[i % n]. You are walking through the result array and pulling values from nums in a circular fashion. Once you reach the end of nums, you wrap around and start from the beginning.

def getConcatenation(nums):
    n = len(nums)
    ans = [0] * (2 * n)
    for i in range(2 * n):
        ans[i] = nums[i % n]
    return ans

There is an even simpler way in Python. Since list concatenation is built into the language, you can just add the list to itself:

def getConcatenation(nums):
    return nums + nums

Both approaches produce the same result. The one-liner is great for submissions, but the explicit loop is better for demonstrating that you understand the index mapping.

The nums + nums syntax creates a new list containing all elements of nums followed by all elements of nums again. It does not modify the original. This is Python's list concatenation operator, and it runs in O(n) time.

Visual walkthrough: index mapping step by step

Let's trace through nums = [1, 2, 1] to see how each index in the result maps back to the source array. Notice how the modulo operator wraps indices 3, 4, and 5 back to positions 0, 1, and 2.

i = 0: ans[0] = nums[0 % 3] = nums[0] = 1

102112nums10?1?2?3?4?5ans

Index 0 maps to nums[0]. Copy value 1 into ans[0].

i = 1: ans[1] = nums[1 % 3] = nums[1] = 2

102112nums1021?2?3?4?5ans

Index 1 maps to nums[1]. Copy value 2 into ans[1].

i = 2: ans[2] = nums[2 % 3] = nums[2] = 1

102112nums102112?3?4?5ans

Index 2 maps to nums[2]. Copy value 1 into ans[2]. First half complete.

i = 3: ans[3] = nums[3 % 3] = nums[0] = 1

102112nums10211213?4?5ans

Index 3 wraps around: 3 % 3 = 0. Copy nums[0] = 1 into ans[3].

i = 4: ans[4] = nums[4 % 3] = nums[1] = 2

102112nums1021121324?5ans

Index 4 wraps around: 4 % 3 = 1. Copy nums[1] = 2 into ans[4].

i = 5: ans[5] = nums[5 % 3] = nums[2] = 1

102112nums102112132415ans

Index 5 wraps around: 5 % 3 = 2. Copy nums[2] = 1 into ans[5].

Done! ans = [1, 2, 1, 1, 2, 1]

102112nums102112132415ans

Every index has been filled. The result is nums concatenated with itself.

The pattern is clear: the first half of ans is a direct copy, and the second half repeats the same values in the same order. The modulo operation handles both halves with a single formula.

Complexity analysis

ApproachTimeSpace
Explicit loopO(n)O(n)
nums + numsO(n)O(n)

Both approaches visit each element exactly twice (once for each copy) and allocate a result array of size 2n. There is no way to do better since the output itself is 2n elements long.

Edge cases to watch for

  • Single element. nums = [5] produces [5, 5]. The simplest case, but worth verifying your loop bounds handle n = 1.
  • All identical values. nums = [3, 3, 3] produces [3, 3, 3, 3, 3, 3]. The output looks like one long run, but the logic is the same.
  • Large arrays. The constraint allows n up to 1000. Both approaches handle this easily in O(n) time.
  • Two elements. nums = [1, 2] produces [1, 2, 1, 2]. Good for checking that the boundary between the first and second copy is correct.

Do not try to modify nums in place by appending to it. Some languages and some problem setups pass the array by reference, and extending it while iterating can cause unexpected behavior. Always build a fresh result array.

The building blocks

This problem uses one fundamental building block:

Modular index mapping. The formula ans[i] = nums[i % n] treats the source array as circular. When the index exceeds the array length, the modulo operation wraps it back around. This same technique appears in:

  • Rotate Array (LeetCode 189): each element moves to position (i + k) % n
  • Circular Queue: the read and write pointers wrap using modulo
  • Josephus Problem: elimination proceeds in a circle, tracked with modular arithmetic

Whenever you see "repeat," "cycle," or "wrap around" in a problem, modular indexing is likely part of the solution.

From understanding to recall

The solution is trivially short, which makes it tempting to skip practice. But the underlying concept of index mapping, deciding "where does the value at position i come from?", is a pattern you will use over and over. On a timed interview, you want that mapping to come to you instantly, not after re-deriving it from scratch.

Spaced repetition helps lock in even simple patterns. Type out the explicit loop version today. Revisit it in a few days. After two or three reps, the i % n mapping will be automatic, and you will recognize it immediately in harder problems that use the same idea.

Related posts