forked from ironhack-labs/lab-java-loops-and-version-control
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask_one.java
More file actions
31 lines (23 loc) · 699 Bytes
/
Copy pathtask_one.java
File metadata and controls
31 lines (23 loc) · 699 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
public class task_one {
public static int findDifference(int[] arr) {
if (arr == null || arr.length == 0) {
throw new IllegalArgumentException("Array must not be empty");
}
int min = arr[0];
int max = arr[0];
for (int i = 1; i < arr.length; i++) {
if (arr[i] < min) {
min = arr[i];
}
if (arr[i] > max) {
max = arr[i];
}
}
int difference = max - min;
return difference;
}
public static void main(String[] args) {
int[] arr = {5, 2, 9, 3, 7};
System.out.println("Difference: " + findDifference(arr));
}
}