call(back)
Algorithms & Data StructureseasySign in to star problems

Interleave Front and Back

Build a new array from numbers by taking elements alternately from the front and the back: the first, then the last, then the second, then the second-to-last, and so on until every element has been placed.

[0, 4, 3, 2, 1]  ->  [0, 1, 4, 2, 3]
[1, 2, 3, 4]     ->  [1, 4, 2, 3]
[7]              ->  [7]

1 <= numbers.length <= 100000.

Hints

Hint 1

Two indices, left and right, and a flag for whose turn it is; stop when they cross. The middle element of an odd-length array is taken by whichever pointer reaches it.

Topics

Asked at

Similar Problems6