-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayFunctions.java
More file actions
46 lines (39 loc) · 1.32 KB
/
ArrayFunctions.java
File metadata and controls
46 lines (39 loc) · 1.32 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
// ArrayFunctions.java
public class ArrayFunctions {
public static void separateEvenOdd(int[] numbers) {
int[] even = new int[numbers.length];
int[] odd = new int[numbers.length];
int evenIndex = 0, oddIndex = 0;
for (int num : numbers) {
if (num % 2 == 0) {
even[evenIndex++] = num;
} else {
odd[oddIndex++] = num;
}
}
// Print the even and odd arrays
System.out.print("Even Numbers: ");
for (int i = 0; i < evenIndex; i++) {
System.out.print(even[i] + " ");
}
System.out.println();
System.out.print("Odd Numbers: ");
for (int i = 0; i < oddIndex; i++) {
System.out.print(odd[i] + " ");
}
System.out.println();
}
public static int findSmallestDistance(int[] numbers) {
int minDistance = Integer.MAX_VALUE;
int index = -1;
// Loop to find the pair with the smallest distance
for (int i = 0; i < numbers.length - 1; i++) {
int distance = Math.abs(numbers[i] - numbers[i + 1]);
if (distance < minDistance) {
minDistance = distance;
index = i;
}
}
return index; // Return the index of the first number
}
}