Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- 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.
- Sample Input
- 1 2 3 4 5
- Sample Output
- 10 14
- */
- import java.io.*;
- import java.math.*;
- import java.security.*;
- import java.text.*;
- import java.util.*;
- import java.util.concurrent.*;
- import java.util.regex.*;
- public class Solution {
- // Complete the miniMaxSum function below.
- static void miniMaxSum(int[] arr) {
- SortedSet<Long> sums = new TreeSet<>();
- for(int i = 0; i < arr.length; i++) {
- for(int j = i+1; j < arr.length; j++) {
- for (int j2 = j+1; j2 < arr.length; j2++) {
- for (int k = j2+1; k < arr.length; k++) {
- sums.add((long)arr[i] + arr[j] + arr[j2] + arr[k]);
- }
- }
- }
- }
- System.out.println(sums.first() + " " + sums.last());
- }
- private static final Scanner scanner = new Scanner(System.in);
- public static void main(String[] args) {
- int[] arr = new int[5];
- String[] arrItems = scanner.nextLine().split(" ");
- scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
- for (int i = 0; i < 5; i++) {
- int arrItem = Integer.parseInt(arrItems[i]);
- arr[i] = arrItem;
- }
- miniMaxSum(arr);
- scanner.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement