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); } }
'Programming > >> Algorithm' 카테고리의 다른 글
[Lucky Algorithm] Compare the Triplets (0) | 2017.10.03 |
---|---|
[Lucky Algorithm] Simple Array Sum (0) | 2017.10.03 |
[Lucky Algorithm] Grading Students (0) | 2017.09.27 |
[Lucky Algorithm] 시작 (0) | 2017.09.27 |
[Algorithm] 혼자 결론 내린 정렬 알고리즘 (0) | 2017.08.30 |
댓글