Concatenation of Array

Given an integer array nums of length n, you want to create an array ans of length 2n where ans[i] == nums[i] and ans[i + n] == nums[i] for 0 <= i < n (0-indexed). Specifically, ans is the concatenation of two nums arrays. Return the array ans.

30:00

Concatenation of Array
easy
Free written solution
Topics
Companies

Given an integer array nums of length n, you want to create an array ans of length 2n where ans[i] == nums[i] and ans[i + n] == nums[i] for 0 <= i < n (0-indexed). Specifically, ans is the concatenation of two nums arrays. Return the array ans.

Example 1:
Input: {"nums":[1,2,1]}
Output: [1,2,1,1,2,1]
Constraints:
  • n==nums.lengthn == \text{nums.length}

  • 1≤n≤10001 \leq n \leq 1000

  • 1≤nums[i]≤10001 \leq \text{nums}[i] \leq 1000

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"nums":[1,2,1]}

ans has 2n = 6 slots: the first half (0..2) and the second half (3..5). Every nums[i] lands once in each half, at i and at i + n.

ans[i] = ans[i + n] = nums[i]
filled 0 / 6

nums

0

1

1

2

2

1

0

1

2

3

4

5

first half

second half

Variables
VariableValue
n3
len(ans)6
DepthFunction Call
Stack empty
0/7