Problem statement
Here we have to create a new array , which length should be double then the given array nums. If the array length of nums is n, then new array length is 2*n ;
Examples
Steps
- Create a length variable and store the length of the given array
- run a for loop which stars at 0 and ends at length.
- now set arr[i] = nums[i], and arr[i+lenght] = arr[i];
Java Code
class Solution {
public int[] getConcatenation(int[] nums) {
int length = nums.length;
int ans[] = new int[2*length];
for(int i = 0; i< length; i++){
ans[i] = nums[i];
ans[i+length] = ans[i];
}
return ans;
}
}
Top comments (0)