Concatenation of Array
Build an array that holds the input array twice in a row, by writing each value to two positions.
You are given an integer array numsansnumsans[i]ans[i + n]nums[i]i
Example 1
Output: [1, 2, 1, 1, 2, 1]
Example 2
Output: [1, 3, 2, 1, 1, 3, 2, 1]
The Idea
Think of a playlist of three songs set to play through twice. The order you hear is the list, then the same list again. Joining two lists end to end like this is called concatenation, and here an array is joined to itself.
Take Example 1, nums = [1, 2, 1]numsans
Look at the picture below. The 2 sits at index 1 of numsans
Why 4? The second copy starts after the whole first copy, which is n = 3 places long. So every value moves exactly 3 places further along: index 1 becomes 1 + 3 = 4. The two 1s follow the same rule, from index 0 to indexes 0 and 3, and from index 2 to indexes 2 and 5.
So each value of numsansii + nnums
Solution Approach
Turning that idea into code means settling two things: which array the loop walks over, and whether to write the copy by hand or let Java's library do it.
Walking over nums, not over ans
Which loop is simplest? One that runs over the n indexes of numsans[i]ans[i + n]nums[i]
A loop over all 2n places of ansnums[i][1, 2, 1]nums[3]ArrayIndexOutOfBoundsExceptionnums[i % n]%
Copying with System.arraycopy
Is there a ready-made way? System.arraycopy(src, srcPos, dest, destPos, length)lengthdestPos
Java Solution
The method getConcatenation
- First, store the length of
innums , and create the result arrayn withans . Java fills a newnew int[2 * n] array with zeros, and every one of them will be overwritten.int - Then a loop moves
from 0 to n - 1. Each time, it writesi tonums[i] for the first copy, and toans[i] for the second copy.ans[i + n] - When the loop is done, return
.ans
The main
import java.util.Arrays;
public class ConcatenationOfArray {
public static 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]; // first copy
ans[i + n] = nums[i]; // second copy, n places later
}
return ans;
}
public static void main(String[] args) {
System.out.println(Arrays.toString(getConcatenation(new int[] {1, 2, 1})));
System.out.println(Arrays.toString(getConcatenation(new int[] {1, 3, 2, 1})));
System.out.println(Arrays.toString(getConcatenation(new int[] {5})));
System.out.println(Arrays.toString(getConcatenation(new int[] {})));
int[] nums = {7, 8, 9};
int[] ans = getConcatenation(nums);
ans[0] = 100;
System.out.println(Arrays.toString(nums) + " " + Arrays.toString(ans));
}
}
Running it prints:
[1, 2, 1, 1, 2, 1]
[1, 3, 2, 1, 1, 3, 2, 1]
[5, 5]
[]
[7, 8, 9] [100, 8, 9, 7, 8, 9]
The answers [1, 2, 1, 1, 2, 1][1, 3, 2, 1, 1, 3, 2, 1][5][5, 5]ans[]
Look at the last line. The program set ans[0]nums[7, 8, 9]
The original implementation,
Complexity
The work is creating an array of 2n places and one pass over nums
- Time: O(n), where n is the length of
. The loop runs n times with two writes each, and creating the array of 2n zeros also takes time in proportion to n.nums - Space: O(n) for the result.
holds 2n values. Apart from it, the method keeps onlyans and the loop index.n
Treating index i + ni