1929. Concatenation of Array LeetCode Solution

In this guide, you will get 1929. Concatenation of Array LeetCode Solution with the best time and space complexity. The solution to Concatenation of Array problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Concatenation of Array solution in C++
  4. Concatenation of Array solution in Java
  5. Concatenation of Array solution in Python
  6. Additional Resources
1929. Concatenation of Array LeetCode Solution image

Problem Statement of Concatenation of Array

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.

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]]
– ans = [1,2,1,1,2,1]
Example 2:

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]

Constraints:

n == nums.length
1 <= n <= 1000
1 <= nums[i] <= 1000

Complexity Analysis

  • Time Complexity: O(n)
  • Space Complexity: O(n)

1929. Concatenation of Array LeetCode Solution in C++

class Solution {
 public:
  vector<int> getConcatenation(vector<int>& nums) {
    const int n = nums.size();

    for (int i = 0; i < n; ++i)
      nums.push_back(nums[i]);

    return nums;
  }
};
/* code provided by PROGIEZ */

1929. Concatenation of Array LeetCode Solution in Java

class Solution {
  public int[] getConcatenation(int[] nums) {
    final int n = nums.length;

    int[] ans = new int[n * 2];

    for (int i = 0; i < n; ++i)
      ans[i] = ans[i + n] = nums[i];

    return ans;
  }
}
// code provided by PROGIEZ

1929. Concatenation of Array LeetCode Solution in Python

class Solution:
  def getConcatenation(self, nums: list[int]) -> list[int]:
    return nums * 2
# code by PROGIEZ

Additional Resources

See also  997. Find the Town Judge LeetCode Solution

Happy Coding! Keep following PROGIEZ for more updates and solutions.