DEV Community

Ganga Sri V
Ganga Sri V

Posted on

Reverse the Array

Problem Statement:
Reverse an array . Reversing an array means the elements such that the element becomes the , the element becomes and so on.

arr[] = [1, 4, 3, 2, 6, 5]
[5, 6, 2, 3, 4, 1]
The first element moves to last position, the second element moves to second-last and so on.

arr[] = [4, 5, 1, 2]
[2, 1, 5, 4]
The first element moves to last position, the second element moves to second last and so on.

My Approach:
1.First I start with two pointers that are left at the beginning and right at the end.
2.Then I swap arr[left] with arr[right].
3.Then I move left forward and right backward.

  1. I repeat this until left is no longer less than right.
  2. Finally that I return the reversed array.

Top comments (0)