580. Interleave First Half with Second Half

EasyStackQueue

Given a queue represented as an array (index 0 is the front), interleave its first half with its second half: output the 1st element of the first half, then the 1st of the second half, then the 2nd of each, and so on. If the length is odd, the extra middle element goes to the second half. Return the resulting queue. The input is JSON {queue}.

Input: JSON {queue}.

Output: Array — the interleaved queue.

Examples

Example 1
Input: {"queue":[1,2,3,4,5,6]}
Output: [1,4,2,5,3,6]
Explanation: Halves [1,2,3] and [4,5,6] interleaved.
Example 2
Input: {"queue":[1,2]}
Output: [1,2]
Explanation: Single element per half.
Example 3
Input: {"queue":[]}
Output: []
Explanation: Empty queue.

Constraints

Asked by

TCSInfosysWiproCognizantCapgemini
Solve this problem in the editor →