Skip to content

Commit 52b19bb

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 2dc978c commit 52b19bb

File tree

1 file changed

+3
-4
lines changed

1 file changed

+3
-4
lines changed

Section9ArrayAB/Array 2.0/src/RotateTheArrayTwoPointer.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
public class RotateTheArrayTwoPointer {
2-
public static void ReverseTheArray(int[] arr)
3-
{
2+
public static void ReverseTheArray(int[] arr) {
43
int p1 = 0;
54
int p2 = arr.length - 1;
6-
while(p1 < p2)
7-
{
5+
6+
while(p1 < p2) {
87
int temp = arr[p1];
98
arr[p1] = arr[p2];
109
arr[p2] = temp;

0 commit comments

Comments
 (0)