DEV Community

VARUN
VARUN

Posted on

CA 05 - Reverse the array

Reverse an Array — The Simplest Problem You’re Still Overthinking

Problem:
Given an array, reverse it.

That means:

  • First → Last
  • Second → Second Last
  • and so on ([GeeksforGeeks][1])

Example:
Input → [1, 4, 3, 2, 6, 5]
Output → [5, 6, 2, 3, 4, 1] ([GeeksforGeeks][2])

Code (Clean & Correct)

class Solution:
def reverseArray(self, arr):
left = 0
right = len(arr) - 1

    while left < right:
        arr[left], arr[right] = arr[right], arr[left]
        left += 1
        right -= 1
Enter fullscreen mode Exit fullscreen mode

Top comments (0)