
Reverse an Array-java
My Thought Process (Java DSA) When I first looked at this problem on GeeksforGeeks, it seemed very simple. But instead of jumping directly into code, I tried to understand what is actually happening when we "reverse" an array. Problem Statement We are given an array of integers arr[] . The task is to reverse the array in-place , meaning we should not use any extra space. Initial Thinking If I take an example: arr = [ 1 , 4 , 3 , 2 , 6 , 5 ] The reversed array should be: [ 5 , 6 , 2 , 3 , 4 , 1 ] At first, I thought about creating a new array and filling it from the end. But then I noticed the constraint: modify the array in-place . So using extra space is not a good approach here. Key Observation Reversing an array means: First element goes to last Second element goes to second last And so on... So instead of moving everything, I can just swap elements from both ends . Approach (Two Pointer Technique) I used two pointers: left starting from index 0 right starting from index n - 1 Then
Continue reading on Dev.to
Opens in a new tab


