Array Reversal - Problem
Given an array of integers, reverse the array in-place without using any additional array or extra space.
The reversal should be done by modifying the original array directly, swapping elements from both ends moving towards the center.
For example, if the input array is [1, 2, 3, 4, 5], the output should be [5, 4, 3, 2, 1].
Constraints:
- You must reverse the array in-place (O(1) extra space)
- Do not create a new array
- Handle edge cases like empty arrays and single elements
Input & Output
Example 1 — Basic Case
$
Input:
nums = [1,2,3,4,5]
›
Output:
[5,4,3,2,1]
💡 Note:
Reverse the array in-place: first element becomes last, last becomes first, and so on
Example 2 — Even Length Array
$
Input:
nums = [1,2,3,4]
›
Output:
[4,3,2,1]
💡 Note:
With even length, pointers meet exactly in the middle after swapping all pairs
Example 3 — Single Element
$
Input:
nums = [42]
›
Output:
[42]
💡 Note:
Single element array remains unchanged - pointers start equal so no swaps needed
Constraints
- 1 ≤ nums.length ≤ 105
- -231 ≤ nums[i] ≤ 231 - 1
Visualization
Tap to expand
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code