BackeasyIntroduction to Arrays HCL

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.

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.
Live Compiler
Loading...
Test Cases & Output
🔒 Sign up to run your code

🚀 Practice this problem

Run code, get AI hints & track streak

Sign Up Free