Advertisement
Guest User

Renka

a guest
Jun 22nd, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.50 KB | None | 0 0
  1. /*
  2.     Given five positive integers, find the minimum and maximum values that can be calculated by summing exactly four of the five             
  3.     integers. Then print the respective minimum and maximum values as a single line of two space-separated long integers.
  4.     Sample Input   
  5.     1 2 3 4 5
  6.    
  7.     Sample Output
  8.     10 14
  9. */
  10.  
  11. import java.io.*;
  12. import java.math.*;
  13. import java.security.*;
  14. import java.text.*;
  15. import java.util.*;
  16. import java.util.concurrent.*;
  17. import java.util.regex.*;
  18.  
  19. public class Solution {
  20.  
  21.     // Complete the miniMaxSum function below.
  22.     static void miniMaxSum(int[] arr) {
  23.         SortedSet<Long> sums = new TreeSet<>();
  24.        
  25.         for(int i = 0; i < arr.length; i++) {
  26.             for(int j = i+1; j < arr.length; j++) {
  27.                 for (int j2 = j+1; j2 < arr.length; j2++) {
  28.                     for (int k = j2+1; k < arr.length; k++) {
  29.                         sums.add((long)arr[i] + arr[j] + arr[j2] + arr[k]);
  30.                     }
  31.                 }
  32.             }
  33.         }
  34.        
  35.         System.out.println(sums.first() + " " + sums.last());
  36.        
  37.     }
  38.  
  39.     private static final Scanner scanner = new Scanner(System.in);
  40.  
  41.     public static void main(String[] args) {
  42.         int[] arr = new int[5];
  43.  
  44.         String[] arrItems = scanner.nextLine().split(" ");
  45.         scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
  46.  
  47.         for (int i = 0; i < 5; i++) {
  48.             int arrItem = Integer.parseInt(arrItems[i]);
  49.             arr[i] = arrItem;
  50.         }
  51.  
  52.         miniMaxSum(arr);
  53.  
  54.         scanner.close();
  55.     }
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement