Skip to content

Commit 2dc978c

Browse files
committed
feat: Add two-pointer method to reverse an array in-place
🧠 Logic: - Introduced `ReverseTheArray(int[] arr)` method using two pointers (`p1` at start, `p2` at end). - Swap elements at `p1` and `p2` and move both pointers towards the center. - This reverses the array efficiently in O(n) time and O(1) space. Signed-off-by: Somesh diwan <[email protected]>
1 parent 6c014d6 commit 2dc978c

File tree

2 files changed

+4
-43
lines changed

2 files changed

+4
-43
lines changed

Section9ArrayAB/Array 2.0/src/RotateTheArray.java

Lines changed: 0 additions & 36 deletions
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,15 @@
1-
/*
2-
public class RotateTheArrayTwopointer
3-
{
4-
public static ReverseTheArray(int[] arr)
1+
public class RotateTheArrayTwoPointer {
2+
public static void ReverseTheArray(int[] arr)
53
{
64
int p1 = 0;
75
int p2 = arr.length - 1;
8-
96
while(p1 < p2)
107
{
118
int temp = arr[p1];
129
arr[p1] = arr[p2];
1310
arr[p2] = temp;
14-
11+
p1++;
12+
p2--;
1513
}
1614
}
1715
}
18-
*/

0 commit comments

Comments
 (0)