-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathChallenge1.java
More file actions
37 lines (32 loc) · 1.18 KB
/
Challenge1.java
File metadata and controls
37 lines (32 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
import java.util.Arrays;
import java.util.Scanner;
public class Challenge1 {
// 1) Difference between largest and smallest values in an int array (length >= 1)
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number of elements: ");
int n = sc.nextInt();
int[] arr = new int[n];
System.out.println("Enter " + n + " integers in different lines:");
for (int i = 0; i < n; i++) {
arr[i] = sc.nextInt();
}
// Call the getdifference method
int diff = getdifference(arr);
System.out.println("Difference between max and min: " + diff);
sc.close();
}
public static int getdifference(int[] arr) {
if (arr == null || arr.length == 0) {
System.out.println("invalid array");
}
int min = Integer.MAX_VALUE;
int max = Integer.MIN_VALUE;
// Loop + conditionals
for (int i = 0; i < arr.length; i++) {
if (arr[i] < min) min = arr[i];
if (arr[i] > max) max = arr[i];
}
return max - min;
}
}