Reverse Array
tags: swap two-pointers
Reverse the order of the given input array.
Solution Outline
- Initialize the index i to 0
- Swap the first element arrays[i] with last element arrays[length - i - 1]
- Repeat until index of middle i == length / 2.
|
|
One of the key concept in this code is that we can find numbers located at the end by using the size of the array and the loop index. The index is computed by : n-i-1 (one is needed to deal with the 0 based array access).
Two pointer approach:
|
|