FunctionRacerProblemsRace

Outside In

EasyArraysPython, JavaScript, TypeScript, Java, C++

Rearrange nums by taking items from the two ends in turns: first the leftmost item, then the rightmost, then the second from the left, then the second from the right, and so on until every item has been taken once. For [1, 2, 3, 4, 5] this gives [1, 5, 2, 4, 3].

Return the new list. An empty nums gives an empty list. Do not change nums.

Example 1

nums [1,2,3,4,5]
returns [1,5,2,4,3]

Left end 1, right end 5, then 2, then 4, and the middle item 3 comes last.

Example 2

nums [1,2,3,4]
returns [1,4,2,3]

With an even count the two middle items 2 and 3 are taken in that order.

Constraints

Race itor take your time

Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.

More Arrays problems