easyIntroduction to ArraysPattern: Array Operations

Alternate Element Rearrangement Solution

Problem Statement

Given a sequence of integers `arr` of length `n`, rearrange the elements such that the first element is placed at the beginning, the last element is placed next, the second element is placed after that, and the second last element is placed next, and so on. The rearranged sequence should be returned as the result.

Examples

Example 1:
Input:[1, 2, 3, 4, 5]
Output:[1, 3, 5, 2, 4]
Explanation: Alternating elements and placing the second half at the end
Example 2:
Input:[10, 20, 30, 40]
Output:[10, 30, 20, 40]
Explanation: Alternating elements and maintaining the order

Constraints

  • The input array contains only non-negative integers.
  • The length of the input array is at least 1.
Time: O(n) Space: O(n)
Analyze constraints and compute optimal solutions step-by-step.

Run, Test & Submit Code

Ready to practice this challenge? Launch our interactive compilation environment with compiler validation.

Solve on Interactive Workspace

Tested Solutions

No solution code is currently loaded.
Complete this code in the workspace editor.