LeetCode 1929: Concatenation of Array Solution

Master LeetCode problem 1929 (Concatenation of Array), a easy challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

1929. Concatenation of Array

Problem Explanation

Explanation:

To solve this problem, we need to create a new array ans by concatenating the input array nums with itself. We can achieve this by iterating over the elements of nums and appending each element to ans twice.

  • Initialize an empty array ans.
  • Iterate over the elements of nums and append each element to ans twice.
  • Return the concatenated array ans.

Time Complexity:

The time complexity of this approach is O(n), where n is the length of the input array nums.

Space Complexity:

The space complexity of this approach is also O(n), where n is the length of the input array nums.

: :

Solution Code

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];
        }
        
        return ans;
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 1929 (Concatenation of Array)?

This page provides optimized solutions for LeetCode problem 1929 (Concatenation of Array) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 1929 (Concatenation of Array)?

The time complexity for LeetCode 1929 (Concatenation of Array) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 1929 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 1929 in Java, C++, or Python.

Back to LeetCode Solutions