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

Tahzib Mahmud Rifat @rifat87

About: Open source || Backend-Development || IoT || Robotics || Computer Vision

Joined:
Feb 13, 2024

1929. Concatenation of Array(Leetcode easy)

Publish Date: Feb 19 '24
0 0

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

Comments 0 total

    Add comment