본문 바로가기
1 Day 1 Algorithms

[2018.12.19] Mini-Max Sum

by 곰돌찌 2018. 12. 19.

Problem


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.

For example, . Our minimum sum is  and our maximum sum is . We would print

16 24

Function Description

Complete the miniMaxSum function in the editor below. It should print two space-separated integers on one line: the minimum sum and the maximum sum of  of  elements.

miniMaxSum has the following parameter(s):

  • arr: an array of  integers

Input Format

A single line of five space-separated integers.

Constraints

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 a 32 bit integer.)

Sample Input

1 2 3 4 5

Sample Output

10 14

Explanation

Our initial numbers are , and . We can calculate the following sums using four of the five integers:

  1. If we sum everything except , our sum is .
  2. If we sum everything except , our sum is .
  3. If we sum everything except , our sum is .
  4. If we sum everything except , our sum is .
  5. If we sum everything except , our sum is .

Hints: Beware of integer overflow! Use 64-bit Integer.


Need help to get started? Try the Solve Me First problem


How I solved the problem


# 질문에서 보면 32 bit보다 넘어갈 수 있다는 말이 힌트..!

# long으로 타입이 선언되어야 함

# 반복문을 두 번 돌면서 5개의 숫자 중에서 하나씩 빼고 합을 구한 다음

# 기존의 minSum과 maxSum을 비교하여 해당 합을 minSum이나 maxSum에 넣을지 판단해야함.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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) {
        long minSum = 0L;
        long maxSum = 0L;
        
        for (int i = 0; i < arr.length; i++) {
            long compSum = 0L;
 
            for (int j = 0; j < arr.length; j++) {
                if (i != j ) {
                    compSum += arr[j];
                }
            }
            
            if (i == 0) {
                minSum = compSum;
                maxSum = compSum;
            } else {
                if (minSum > compSum) {
                    minSum = compSum;
                }
 
                if (maxSum < compSum) {
                    maxSum = compSum;
                }
            }
        }
 
        System.out.println(minSum + " " + maxSum);
 
    }
 
    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();
    }
}
 
cs


[출처 : https://www.hackerrank.com ]

'1 Day 1 Algorithms' 카테고리의 다른 글

[2018.12.21] Binary Numbers  (0) 2018.12.21
[2018.12.20] Recursion 3  (0) 2018.12.21
[2018.12.19] Dictionaries and Maps  (0) 2018.12.19
[2018.12.18] Staircase  (0) 2018.12.18
[2018.12.18] Arrays  (0) 2018.12.18

댓글