22. Shuffle the Array

EasyArrayArray

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

Input: An integer array nums of length 2n, and integer n.

Output: Integer array interleaving first half and second half.

Examples

Example 1
Input: [2,5,1,3,4,7],3
Output: [2,3,5,4,1,7]
Explanation: x=[2,5,1], y=[3,4,7]. Interleave: [2,3,5,4,1,7].
Example 2
Input: [1,2,3,4,4,3,2,1],4
Output: [1,4,2,3,3,2,4,1]
Explanation: x=[1,2,3,4], y=[4,3,2,1]. → [1,4,2,3,3,2,4,1].
Example 3
Input: [1,1,2,2],2
Output: [1,2,1,2]
Explanation: x=[1,1], y=[2,2]. → [1,2,1,2].

Constraints

Asked by

MicrosoftGoogleAmazonBloombergMeta
Solve this problem in the editor →