Find the minimum and maximum values that can be calculated by summing exactly four of the five integers
Problem Description :
Given five positive integers, find the minimum and maximum values that can be calculated by summing exactly four of the five integers. Then print the respective minimum and maximum values as a single line of two space-separated long integers.
Example 1 :
arr = [1, 3, 5, 7, 9]
The minimum sum is 1 + 3 + 5 + 7 = 16 and the maximum sum is 3 + 5 + 7 + 9 = 24.
Answer is 16 24
Example 2 :
arr = [396285104, 573261094, 759641832, 819230764, 364801279]
The minimum sum is 364801279 + 396285104 + 573261094 + 759641832 = 2093989309 and the maximum sum is 396285104 + 573261094 + 759641832 + 819230764 = 2548418794.
Answer is 2093989309 2548418794
Lets see solution.
Solution 1
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class MiniMaxSum {
public static void main(String[] args) {
List<Integer> arr = new ArrayList<>() {{
add(5);
add(4);
add(2);
add(3);
add(1);
}};
long max = 0, min = 0;
Collections.sort(arr);
for(int i = 0, j = arr.size()-1; i < arr.size()-1; i++, j--){
min += arr.get(i);
max += arr.get(j);
}
System.out.print(min+" "+max);
}
}
Output :
10 14
Solution explanation :
- Define 2 long variable and initialize with 0.
- Sort given List using Collections.sort() method.
- Traverse through given list from 0 to list size-1. We are define two variable i and j in loop. i variable used for traverse from 0 to 3 (means minimum value to maximum value) and j variable used for traverse from 4 value to 1 (max value to min value).
- last, print both min and max value.
Output explanation :
after sorting list becomes :
list = [1, 2, 3, 4, 5]
- i = 0, j = 4, min = 0, max = 0
- min = 0 + arr.get(0) | 0 + 1 = 1
- max = 0 + arr.get(4) | 0 + 5 = 5
- i = 1, j = 3, min = 1, max =5
- min = 1 + arr.get(0) | 1 + 2 = 3
- max = 5 + arr.get(4) | 5 + 4 = 9
- i = 2, j = 3, min = 3, max = 9
- min = 3 + arr.get(2) | 3 + 3 = 6
- max = 9 + arr.get(3) | 9 + 3 = 12
- i = 3, j = 2, min = 6, max = 12
- min = 6 + arr.get(3) | 6 + 4 = 10
- max = 12 + arr.get(2) | 12 + 2 = 14
- i = 4 is not less than 4.
- min = 10, max = 14
Solution 2
import java.util.ArrayList;
import java.util.List;
public class MiniMaxSum {
public static void main(String[] args) {
List<Integer> arr = new ArrayList<>() {{
add(1);
add(2);
add(5);
add(7);
add(9);
}};
long min = arr.get(0);
long max = min;
long sum = min;
for (int i = 1; i < arr.size(); i++) {
sum += arr.get(i);
if (arr.get(i) < min) {
min = arr.get(i);
}
if (arr.get(i) > max) {
max = arr.get(i);
}
}
System.out.print((sum - max) + " " + (sum - min));
}
}
Output :
15 23
Solution explanation :
- Define three long variables and initialize with 0th index value of given list.
- Traverse through given list from 0 to list size.
- Add sum plus current list index value in sum variable.
- Check condition, if current list value is less than min variable then store that value to min variable.
- In another if, check if current list value is greater than max variable then store in max variable.
- At last, we will get sum of all list value in sum variable, minimum value in min and maximum value in max variable.
- So for get minimum 4 integer sum, minus the max value from total sum of all list value. and for get maximum 4 integer sum, minus the min value from total sum of all list value.
Other hackerrank problem and its solution in java :
Comments
Post a Comment