You are given a queue Q containing N integers, where N is an even number. Rearrange the elements of the queue by interleaving the first half of the queue with the second half.
Note: Return the rearranged queue as a vector or ArrayList.
Example 1
Input: N = 6, Q = {1, 2, 3, 4, 5, 6}
Output: {1, 4, 2, 5, 3, 6}
Explanation: The first half of the queue is {1, 2, 3}, and the second half is {4, 5, 6}. After interleaving the first half with the second half, the final queue becomes {1, 4, 2, 5, 3, 6}.
Example 2
Input: N = 8, Q = {10, 20, 30, 40, 50, 60, 70, 80}
Output: {10, 50, 20, 60, 30, 70, 40, 80}
Explanation: The first half of the queue is {10, 20, 30, 40}, and the second half is {50, 60, 70, 80}. After interleaving the first half with the second half, the final queue becomes {10, 50, 20, 60, 30, 70, 40, 80}.
Approach, complexity and clean code in every language. Sign in to unlock the editorial.
Your submissions, results and runtime history live here. Sign in to start your streak.
6 1 2 3 4 5 6
1 4 2 5 3 6
8 10 20 30 40 50 60 70 80
10 50 20 60 30 70 40 80