Data Structures

Reverse Array

Given an array arr, reverse the array.

You should solve this problem in-place, without creating a new Array.

First Few Test Cases:

You need to solve this problem in-place, without creating a new Array. The idea for reversing the array is to swap all the elements in pairs, like this:

When you you're done, you'll end up with the reversed array, like this:

To do this, you can use 2 pointers, one starting at the right side of the array n-1, and one starting at the left side 0.

You keep moving the pointers inwards and swapping elements, until each element has been swapped with the element on the opposite side. You stop when the pointers cross each other. Here's the code for this:

Time Complexity O(n)O(n). We loop through the entire array.

Space Complexity O(1)O(1). We don't have to store any variables. The size of the input never counts towards the space complexity, which is why the space complexity is O(1) and not O(n).

Mark as Completed:
Submits:
reverseArray
Test your code to get an output here!
reverseArray(
)
Test your code to get an output here!