Interleave Array Halves
Problem statement
You are given an array coupons of even length 2m. Interleave its first and second halves, taking one element from the first half followed by the matching element from the second half.
Return [coupons[0], coupons[m], coupons[1], coupons[m + 1], ...]. Keep the order within each half. Equal coupon numbers remain separate elements.
For this exercise, assume an empty input returns an empty array. Return a new array and leave coupons unchanged.
Function
interleaveHalves(coupons: int[]) → int[]Examples
Example 1
coupons = [4,4,4,4,8,3,5,9]return = [4,8,4,3,4,5,4,9]The first half is [4,4,4,4] and the second half is [8,3,5,9]. Pairing their elements in order gives [4,8,4,3,4,5,4,9]; all four copies of 4 are kept.
Example 2
coupons = [1,2,10,20]return = [1,10,2,20]The halves are [1,2] and [10,20]. Take 1, then 10, then 2, then 20.
Constraints
- For this exercise, assume 0 <= coupons.length <= 2000.
- The length of coupons is even.
- For this exercise, assume 0 <= coupons[i] <= 10^9.