|
U
Pro25. Reverse Array In-Place
Easy
Problem Description
Reverse an array of N integers in-place using two pointers.
Input Format
- •Line 1: An integer N (1≤N≤105).
- •Line 2: N space-separated integers A1,A2,…,AN.
Output Format
Print the N reversed integers separated by a single space.
Example 1:
Input:
5 1 2 3 4 5
Output:
5 4 3 2 1
Explanation: Reversing 1 2 3 4 5 yields 5 4 3 2 1.
Example 2:
Input:
1 99
Output:
99
Explanation: Single element remains unchanged.
Example 3:
Input:
4 10 20 30 40
Output:
40 30 20 10
Explanation: Even length array reversal.
Constraints:
Standard integer bounds: 32-bit signed integer or input range [1, 10^5].Time complexity target: O(N) or O(N log N) where applicable.Auxiliary space target: O(1) or minimal auxiliary memory.
Follow up:Can this algorithm be adapted to an online streaming setting where the entire input cannot be held in memory simultaneously?
1,180 Online
Code
|Loading Code Editor...
SavedLn 1, Col 1
param_1 =
param_2 =
Sandboxed Engine
Ctrl+Enter = Run | Ctrl+Shift+Enter = Submit