Post

Leetcode 1929. Concatenation of Array

Explanation for Leetcode 1929 - Concatenation of Array, and its solution in Python.

Problem

Leetcode 1929 - Concatenation of Array

Example:

1
2
3
4
5
6
7
8
9
10
11
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]]
- ans = [1,2,1,1,2,1]

Input: nums = [1,3,2,1]
Output: [1,3,2,1,1,3,2,1]
Explanation: The array ans is formed as follows:
- ans = [nums[0],nums[1],nums[2],nums[3],nums[0],nums[1],nums[2],nums[3]]
- ans = [1,3,2,1,1,3,2,1]

Approach

This is a very simple problem and we can solve this using Python even easier as we can just append every element in nums array into copy of nums array

Here is the Python code for the solution:

1
2
3
4
5
6
7
8
class Solution:
    def getConcatentation(self, nums: List[int]) -> List[int]:
        res = nums.copy()

        for num in nums:
            res.append(num)
        
        return res

Time Complexity and Space Complexity

Time Complexity: $O(n)$ since we are iterating through the list 2*n times.

Space Complexity: $O(n)$ since we are maintaining a list of size 2*n.

This post is licensed under CC BY 4.0 by the author.