-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfindMinMax.java
More file actions
49 lines (35 loc) · 1.19 KB
/
Copy pathfindMinMax.java
File metadata and controls
49 lines (35 loc) · 1.19 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
47
48
49
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
//Given an array of five elements find the maximum value and the minimum
//value that you could get if you were to remove only one element
public class Solution {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
//add all the numbers first
long [] nums = new long[5];
long sum = 0;
long max = 0;
long min = 0;
for(int i = 0; i < 5; i++) {
nums[i] = scan.nextInt();
sum += nums[i];
}
max = sum - nums[0];
min = max;
for(int i = 1; i < 5; i++) {
if(sum - nums[i] < min) {
min = sum - nums[i];
}
if(sum - nums[i] > max) {
max = sum - nums[i];
}
}
System.out.println(min + " " + max);
}
}
//a better way to deal with this was to have tried to find the
//biggest and smallest number from the array that you were given
// and use those to find the minimum and the maximum value