Skip to main content

Concatenation of Array

In this tutorial, we will solve the Concatenate Array problem using a simple approach and an optimized approach. We will provide the implementation of the solution in C++, Java, and Python.

Problem Description​

Given an integer array nums of length n, you want to 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]

Example 2:

Input: nums = [1,3,2,1]
Output: [1,3,2,1,1,3,2,1]

Constraints​

  • n == nums.length
  • 1 <= n <= 1000

Solution for Concatenate Array Problem​

Approach 1: Simple Concatenation​

The approach is straightforward: iterate through the array nums and copy each element twice, once to the first half and once to the second half of the result array ans.

Written by @ImmidiSivani
class Solution {
public:
vector<int> getConcatenation(vector<int>& nums) {
int n = nums.size();
vector<int> ans(2 * n);
for (int i = 0; i < n; ++i) {
ans[i] = nums[i];
ans[i + n] = nums[i];
}
return ans;
}
};

Approach 2: Optimized Concatenation​

An optimized approach for concatenation in some languages can take advantage of built-in functions to concatenate the array directly.

Written by @ImmidiSivani
class Solution {
public:
vector<int> getConcatenation(vector<int>& nums) {
vector<int> ans = nums;
ans.insert(ans.end(), nums.begin(), nums.end());
return ans;
}
};

Complexity Analysis​

  • Time Complexity: O(n)O(n), where n is the length of the input array nums. We iterate through the array once.
  • Space Complexity: O(n)O(n), since we are using a new array ans of size 2n.

Authors:

Loading...