Concatenation of Array
Given an integer array numsnans2nans[i] == nums[i]ans[i + n] == nums[i]0 <= i < n
Specifically, ansnums
Return the array ans
Example 1
Output: [1, 2, 1, 1, 2, 1]
Explanation: After copying nums array into twice, it looks like this.
[nums[0],nums[1],nums[2],nums[0],nums[1],nums[2]]
[1, 2, 1, 1, 2, 1]
Example 1
Output: [1,3,2,1,1,3,2,1]
Solution - copy elements from source array to destination array with index locations
In this approach, we are going to create ans2nnumsans[i] = nums[i];
ans[i+nums.length] = nums[i];nums[i]ansithi + nums.lengthth
import java.util.Arrays;
public class ConcatenationOfArray {
static int[] getConcatenation(int[] nums) {
int[] ans = new int[nums.length *2];
for(int i=0;i<nums.length; i++) {
ans[i] = nums[i];
ans[i+nums.length] = nums[i];
}
return ans;
}
}
Complexity Analysis:
Time complexity: Above code runs in O(n) time where nnumsnums
Space complexity: O(n) since we have to return ans2n
Above implementations source code can be found at