-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReversingArray.java
More file actions
40 lines (29 loc) · 883 Bytes
/
ReversingArray.java
File metadata and controls
40 lines (29 loc) · 883 Bytes
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
import java.util.Scanner;
public class ReversingArray {
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
//Reversing an array using Recursive function
int[] arr = {1, 5, 23, 12, 57, 2};
int l = 0;
int r = arr.length - 1;
System.out.print("Original array : ");
for(int num : arr){
System.out.print(num + " ");
}
reverse(arr, l, r);
System.out.print("\nReversed array : ");
for (int num : arr) {
System.out.print(num + " ");
}
scanner.close();
}
static void reverse(int[] arr, int l, int r){
if (l >= r) {
return;
}
int temp = arr[l];
arr[l] = arr[r];
arr[r] = temp;
reverse(arr, l + 1, r - 1);
}
}