-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArraySeprationRec.java
63 lines (42 loc) · 1.5 KB
/
ArraySeprationRec.java
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import java.util.Scanner;
public class ArraySeprationRec {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the size of the array: ");
int size = sc.nextInt();
int[] arr = new int[size];
System.out.println("Enter the elements of the array: ");
for (int i = 0; i < size; i++) {
System.out.print("Element " + (i + 1) + ": ");
arr[i] = sc.nextInt();
}
sc.close();
int[] oddArr = new int[size];
int[] evenArr = new int[size];
separateOddEvenRec(arr, oddArr, evenArr, 0, 0, 0);
System.out.println("The odd array is: ");
printArray(oddArr);
System.out.println("The even array is: ");
printArray(evenArr);
}
public static void separateOddEvenRec(int[] arr, int[] oddArr, int[] evenArr, int index, int oddIndex, int evenIndex) {
if (index == arr.length) {
return;
}
if (arr[index] % 2 == 0) {
evenArr[evenIndex] = arr[index];
evenIndex++;
}
else {
oddArr[oddIndex] = arr[index];
oddIndex++;
}
separateOddEvenRec(arr, oddArr, evenArr, index + 1, oddIndex, evenIndex);
}
public static void printArray(int[] arr) {
for (int num : arr) {
System.out.print(num + " ");
}
System.out.println();
}
}