1929. Concatenation of Array
Concatenation of Array (LeetCode 1929) is a Easy Data Structures and Algorithms interview problem you can solve, run and submit on Thita.ai. It belongs to the Array/Matrix Manipulation Patterns pattern, in the In-place Rotation subpattern.
How to solve Concatenation of Array
Concatenation of Array is a In-Place Array and Matrix Rotation problem, part of Array/Matrix Manipulation Patterns. Learn in-place rotation techniques for arrays and matrices. Rotate without using extra space.
Work towards the solution in these steps:
- The output has exactly twice as many elements as `nums`.
- The first half of the result should match `nums` element for element.
- After copying the first half, place another copy of `nums` immediately after it.
The reference solution runs in O(n) time and O(n) space.
The Concatenation of Array solution and editorial walks through the full approach with reference code.
Problem statement
Given an integer array nums, define its concatenation with itself as an array formed by placing all elements of nums first and then placing the same elements again in the same order.
Return the resulting integer array. The returned array must therefore contain two consecutive copies of nums, with the first copy unchanged and the second copy unchanged.
Example 1:
Input: nums = [7,2] Output: [7,2,7,2] Explanation: The original array [7, 2] is followed by an identical copy of itself.
Example 2:
Input: nums = [0] Output: [0,0] Explanation: A one-element array is repeated once, producing two occurrences of its only element.
Example 3:
Input: nums = [5,1,5] Output: [5,1,5,5,1,5] Explanation: The three elements appear in their original order, followed by the same three elements again.
Constraints:
- 1 <= nums.length <= 1000 - 1 <= nums[i] <= 1000 - nums contains only integers
Topics and companies
Topics: Array, Iteration.
How to practise Concatenation of Array on Thita.ai
Starter code is provided in C#, C++, Go, Java, JavaScript and Python. Your solution runs against 5 test cases for this problem, with the AI coach available for a hint when you are stuck rather than a finished answer. The reference solution runs in O(n) time and O(n) space.
Open Concatenation of Array in the code editor, or read the Concatenation of Array editorial for a worked solution with its approach and complexity analysis.
Where Concatenation of Array sits in the DSA pattern sheet
- Array/Matrix Manipulation Patterns pattern guide
- In-place Rotation subpattern
- The full DSA patterns sheet
Problems related to Concatenation of Array
Other problems that use the same In-place Rotation and Array/Matrix Manipulation Patterns techniques:
- Rotate Array — Medium, same subpattern
- Rotate Image — Medium, same subpattern
- Transpose Matrix — Easy, same subpattern
- Add Binary — Easy, same pattern
- Add to Array-Form of Integer — Easy, same pattern
- Contains Duplicate — Easy, same pattern
Concatenation of Array FAQ
What is the LeetCode number of Concatenation of Array?
Concatenation of Array is LeetCode problem 1929, rated Easy.
What pattern is used to solve Concatenation of Array?
Concatenation of Array is a In-Place Array and Matrix Rotation problem. It belongs to the Array/Matrix Manipulation Patterns pattern, in the In-place Rotation subpattern.
What is the time complexity of Concatenation of Array?
The reference solution runs in O(n) time and O(n) space.