본문 바로가기
Programming/>> Algorithm

[Lucky Algorithm] Mini-Max Sum

by 니키ᕕ( ᐛ )ᕗ 2017. 10. 3.

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.

Input Format

A single line of five space-separated integers.

Constraints

  • Each integer is in the inclusive range .

Output Format

Print two space-separated long integers denoting the respective minimum and maximum values that can be calculated by summing exactly four of the five integers. (The output can be greater than 32 bit integer.)


import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int[] arr = new int[5];
        for(int arr_i=0; arr_i < 5; arr_i++){
            arr[arr_i] = in.nextInt();
        }
        
        
        long[] result = new long[arr.length];
        
        for(int i=0; i < arr.length; i++){
            long temp = 0;
            
            for(int j=0; j < arr.length; j++){
                if(i == j) continue;
                temp += arr[j];
            }
            result[i] = temp;
        }
        
        long min = result[0];
        long max = result[0];
        for(int i=0; i< result.length; i++){
            
            if(min > result[i]) {
                min = result[i];
            }
        
            if(max < result[i]) {
                max = result[i];
            }
        }
        
        System.out.println(min + " " + max);
    }
}


댓글