#1929 Concatenation of Array
the problem
In plain words
Given an int array nums of length n, return an array of length 2n that is nums followed by nums again: ans[i] = nums[i] and ans[i + n] = nums[i].
in nums = [1,2,1]
out [1,2,1,1,2,1]
in nums = [1,3,2,1]
out [1,3,2,1,1,3,2,1]
- 1 ≤ n ≤ 1000
- 1 ≤ nums[i] ≤ 1000
your turn
Try it before you peek
Read the examples, say the brute force out loud, then write code: here or on LeetCode ↗. Stuck for 20 minutes? Open one hint at a time.
Solve it in the browser
LeetCode-style: complete the class. Compiled by the real javac (Java 17) and judged right here — nothing is sent anywhere.
Solutions are hidden so you can try first. Even 20 minutes of real effort makes the answer stick far better than reading it.
Marking the problem solved, or opening every hint, also shows it.
the aha
Key insight
patternSimulation
The output size is known up front (2n), so allocate new int[2 * n] once and write each value into two slots: i and i + n.
solutions
From brute force to optimal
Add everything twice to a list, then unbox it into an int[].
- Add each value to a
List<Integer>. - Add each value again.
- Copy the list into
new int[list.size()].
Three linear passes — but every add boxes and every get unboxes.
A boxed list (about 20 bytes per value) on top of the answer.
class Solution { public int[] getConcatenation(int[] nums) { List<Integer> list = new ArrayList<>(); // boxes every value for (int x : nums) list.add(x); for (int x : nums) list.add(x); // the same values again int[] ans = new int[list.size()]; for (int i = 0; i < ans.length; i++) ans[i] = list.get(i); // unbox back return ans; }}Allocate 2n slots; nums[i] goes to i and to i + n.
- ans = new int[2n].
- For each i: ans[i] = ans[i + n] = nums[i].
One pass, two writes per element.
Only the answer array, which the problem requires anyway.
class Solution { public int[] getConcatenation(int[] nums) { int n = nums.length; int[] ans = new int[2 * n]; // size is known up front for (int i = 0; i < n; i++) { ans[i] = nums[i]; // first copy ans[i + n] = nums[i]; // second copy, shifted by n } return ans; }}Let the JDK do both copies.
Arrays.copyOf(nums, 2n)gives the first copy plus n zeros.System.arraycopy(nums, 0, ans, n, n)fills the second half.
Two block copies of n values.
Only the answer array.
class Solution { public int[] getConcatenation(int[] nums) { int n = nums.length; int[] ans = Arrays.copyOf(nums, 2 * n); // first copy + n zeros System.arraycopy(nums, 0, ans, n, n); // (src, srcPos, dest, destPos, length) return ans; }}in the room
How to say it in the interview
talk track
- Say the output length first: 2n.
- Write the index-math loop, then mention
System.arraycopyas the library way.
traps
int[] ans = nums;copies nothing — it's the same array under a second name.- Mixing up
nand2 * nin the loop bound →ArrayIndexOutOfBoundsException.
what they ask next
Follow-ups
Concatenate k copies?
new int[k * n] and write nums[i] to j * n + i for each copy j — O(k·n).
before you submit
Edge cases to test
- n = 1: [7] → [7, 7].
- n = 1000 (the maximum): any O(n) way is fine — just don't grow a list when the size is known.
practise next
Similar problems
- #1470Shuffle the ArrayEasyin Java Arrays & Strings
- #189Rotate ArrayMediumin Arrays & Strings