-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathP27_ReverseArrayInPlace.java
More file actions
44 lines (35 loc) · 1.34 KB
/
Copy pathP27_ReverseArrayInPlace.java
File metadata and controls
44 lines (35 loc) · 1.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package programs;
import java.util.Arrays;
/**
* ============================================================
* PROGRAM 27: Reverse an Array In-Place
* ============================================================
* Problem: WAP to reverse an array in-place without allocating
* a second array (Two-Pointer Technique in O(n/2) steps).
* ============================================================
*/
public class P27_ReverseArrayInPlace {
public static void reverse(int[] arr) {
if (arr == null || arr.length <= 1) return;
int left = 0;
int right = arr.length - 1;
while (left < right) {
// Swap left and right elements
int temp = arr[left];
arr[left] = arr[right];
arr[right] = temp;
left++;
right--;
}
}
public static void main(String[] args) {
int[] oddArray = {1, 2, 3, 4, 5};
int[] evenArray = {10, 20, 30, 40, 50, 60};
System.out.println("Odd Array Before : " + Arrays.toString(oddArray));
reverse(oddArray);
System.out.println("Odd Array After : " + Arrays.toString(oddArray));
System.out.println("\nEven Array Before : " + Arrays.toString(evenArray));
reverse(evenArray);
System.out.println("Even Array After : " + Arrays.toString(evenArray));
}
}