DEV Community

Cover image for 1929. Concatenation of Array(Leetcode easy)
Tahzib Mahmud Rifat
Tahzib Mahmud Rifat

Posted on

1929. Concatenation of Array(Leetcode easy)

Problem statement

Image description

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

Image description

Image description

Steps

  1. Create a length variable and store the length of the given array
  2. run a for loop which stars at 0 and ends at length.
  3. 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;
    }
}
Enter fullscreen mode Exit fullscreen mode

Output

Image description

Top comments (0)