Reverse an Array without using Another Array

In this tutorial, I am going to discuss how to reverse an array without using another array.

Given an array of integers, write a code to reverse an array in-place.

For Example –

Input : {1, 7, 8, 9}

Output: { 9, 8, 7, 1}

Reverse an Array without using Another Array

We have to reverse an array without using extra space. How do you approach this problem?

Programming Video Tutorials

Segregate 0s and 1s in an array

Reverse an Array without using Another Array – Java Code

We can solve this problem in-place by using two pointers.

We declare two pointers start and end. The initial value of Start variable is zero and the initial value of end variable is with array length minus one. Then we run a loop until the value of start is less than end. We swap the values present at start and end index. Then we increment the value of start variable and decrement the value of end variable.

The time complexity of this approach is O(n) and it’s space complexity is O(1).

Find sum of array elements using recursion

Tagged . Bookmark the permalink.

About WebRewrite

I am technology lover who loves to keep updated with latest technology. My interest field is Web Development.