Concatenation of Array

Easy Top 250
Associated Patterns
Interviewed At (Company Tags)
GoogleAdobeApple

Problem Description

Given an integer array nums of length n, create an array ans of length 2n where ans[i] == nums[i] and ans[i + n] == nums[i] for 0 <= i < n (0-indexed). Specifically, ans is the concatenation of two nums arrays.

Return the array ans.


Examples

Example 1:

Input: nums = [1,2,1] Output: [1,2,1,1,2,1] Explanation: The array ans is formed as follows: ans = [nums[0], nums[1], nums[2], nums[0], nums[1], nums[2]] = [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

Doubling Array Space With Offsets

The solution relies on copying elements from the input array into a new array of double the size. By using a single loop, you can copy each element at index i directly to both i and the offset index i + n in the target array. This avoids running two separate loop passes or dynamic resizes.


Solution 1: Single Pass Copying

Initialize the output array with size 2n and fill both halves in a single traversal.

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]; // copy to the second half
        }
        return ans;
    }
}
class Solution:
    def getConcatenation(self, nums: list[int]) -> list[int]:
        n = len(nums)
        ans = [0] * (2 * n)
        for i in range(n):
            ans[i] = nums[i]
            ans[i + n] = nums[i]  # copy to the second half
        return ans
#include <vector>

class Solution {
public:
    std::vector<int> getConcatenation(std::vector<int>& nums) {
        int n = nums.size();
        std::vector<int> ans(2 * n);
        for (int i = 0; i < n; i++) {
            ans[i] = nums[i];
            ans[i + n] = nums[i]; // copy to the second half
        }
        return ans;
    }
};

Complexity Analysis

  • Time Complexity: O(n) where n is the number of elements in the input array. We visit each element once and copy it.
  • Space Complexity: O(n) auxiliary space to store the output array of size 2n.

Where It Breaks

This solution is highly robust but will run into integer overflow constraints if n approaches the maximum capacity of signed integers (though constrained to 1000 in this problem).


Common Mistakes

  • Incorrect allocation size: Allocating n + 1 instead of 2 * n for the result array.
  • Index out of bounds: Using the incorrect offset, such as i + 1 instead of i + n for the second copy.

Frequently Asked Questions

Is there a built-in function to solve this? In Python, you can simply write nums * 2 or nums + nums to achieve the same result. In interviews, it is usually expected to show the index-level assignment.

How does this behave with empty inputs? Since the constraints specify nums.length >= 1, empty inputs are not possible. If they were, the solution would return an empty array.


← All Problems