Back to problems Sign in to unlock
Leetcode
Easy
Arrays
Shuffle the Array
Rearrange an array of length so that the values alternate between the first half and the second half.
Acceptance 100%
Problem Statement
You are given an integer array nums containing 2n elements in the form:
- the first
nelements:x1, x2, ..., xn - the next
nelements:y1, y2, ..., yn
Return the array arranged as:
[x1, y1, x2, y2, ..., xn, yn]
The goal is to interleave the two halves of the array in order.
Input Format
Input
- An integer array
numsof even length2n.
Interpretation
- The first half of the array contains
x1..xn. - The second half contains
y1..yn.
Output Format
Output
- Return a new array of length
2narranged as[x1, y1, x2, y2, ..., xn, yn].
Constraints
nums.length = 2n1 <= n- The elements may be any integers allowed by the platform version of the problem.
- Preserve the relative order within each half.
Examples
Sample cases returned by the problem API.
Example 1
Input
nums = [2,5,1,3,4,7], n = 3
Output
[2,3,5,4,1,7]
Explanation
The first half is [2,5,1] and the second half is [3,4,7]. Interleaving them gives [2,3,5,4,1,7].
Premium problem context
Unlock deeper context for this problem
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.
Guided hints
Editorial and discussion links
Concept map and variants
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.