Shuffle the Array

Given the array nums consisting of 2n elements in the form [x1,x2,...,xn,y1,y2,...,yn], return the array in the form [x1,y1,x2,y2,...,xn,yn].

30:00

Shuffle the Array
easy
Free written solution
Topics
Companies

Given the array nums consisting of 2n elements in the form [x1,x2,...,xn,y1,y2,...,yn], return the array in the form [x1,y1,x2,y2,...,xn,yn].

Example 1:
Input: {"nums":[2,5,1,3,4,7],"n":3}
Output: [2,3,5,4,1,7]
Constraints:
  • 1≤n≤5001 \leq n \leq 500

  • nums.length==2n\text{nums.length} == 2n

  • 1≤nums[i]≤1031 \leq \text{nums}[i] \leq 10^3

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

Run code to see results.

Input
arr ={"nums":[2,5,1,3,4,7],"n":3}

x pile: nums[0..n-1]

2
x1
5
x2
1
x3

y pile: nums[n..2n-1]

3
y1
4
y2
7
y3

result: x1, y1, x2, y2, ...

0 / 6 slots filled

Cut the deck in half: the x pile is nums[0..2] and the y pile is nums[3..5]. result starts empty.

Variables
VariableValue
n3
DepthFunction Call
Stack empty
0/10