-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path30May(II).java
More file actions
47 lines (38 loc) · 1.18 KB
/
Copy path30May(II).java
File metadata and controls
47 lines (38 loc) · 1.18 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
45
46
47
public class reverseArray {
// two pointer approach --------------
// with swapping logic...
// 1st pointer at array[0]. 2nd pointer at array[array.length]
// TC = O(n)
static void approach1(int[] arr){
int i = 0; // pointer 1
int j = arr.length-1; // pointer 2
int temp;
while (i<j){
temp = arr[i]; // swapping the last element with first
arr[i] = arr[j];
arr[j] = temp;
i++;
j--;
}
System.out.println("The reversed array is : ");
System.out.print("{");
for(int x : arr){
System.out.print(x+",");
}
System.out.println("}");
}
public static void main(String[] args) {
int[] arr = {1,2,3,4,5,6,7,8,9,10};
System.out.println("The initial array was: ");
System.out.print("{");
for(int i : arr){
System.out.print(i+",");
}System.out.println("}");
approach1(arr);
}
}
// ----------------------- Output -----------------------------
// The initial array was:
// {1,2,3,4,5,6,7,8,9,10,}
// The reversed array is :
// {10,9,8,7,6,5,4,3,2,1,}