Digking's cave

HackerRank 해커랭크 ) Mini-Max Sum 본문

코딩테스트/알고리즘

HackerRank 해커랭크 ) Mini-Max Sum

디깅 2023. 5. 25. 22:06
728x90

https://www.hackerrank.com/challenges/mini-max-sum/problem?isFullScreen=true 

 

Mini-Max Sum | HackerRank

Find the maximum and minimum values obtained by summing four of five integers.

www.hackerrank.com

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.

Example

The minimum sum is  and the maximum sum is . The function prints

16 24

Function Description

Complete the miniMaxSum function in the editor below.

miniMaxSum has the following parameter(s):

  • arr: an array of  integers

Print

Print two space-separated integers on one line: the minimum sum and the maximum sum of  of  elements.

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

The numbers are , , , , and . Calculate the following sums using four of the five integers:

  1. Sum everything except , the sum is .
  2. Sum everything except , the sum is .
  3. Sum everything except , the sum is .
  4. Sum everything except , the sum is .
  5. Sum everything except , the sum is .

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

 

풀이

import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.function.*;
import java.util.regex.*;
import java.util.stream.*;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;

class Result {

    /*
     * Complete the 'miniMaxSum' function below.
     *
     * The function accepts INTEGER_ARRAY arr as parameter.
     */

    public static void miniMaxSum(List<Integer> arr) {
    // Write your code here
        arr.sort(Comparator.naturalOrder());
        long result = 0;
        for(int i = 0; i<arr.size() - 1; i++) {
            result += arr.get(i);
        }
        System.out.print(result);
        System.out.print(" ");
        result = 0;
        for(int i = arr.size() -1; i>0; i--) {
            result += arr.get(i);
        }
        System.out.print(result);
    }

}

public class Solution {
    public static void main(String[] args) throws IOException {
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));

        List<Integer> arr = Stream.of(bufferedReader.readLine().replaceAll("\\s+$", "").split(" "))
            .map(Integer::parseInt)
            .collect(toList());

        Result.miniMaxSum(arr);

        bufferedReader.close();
    }
}

처음에는 해결했는데 test case 5개만 성공하고, 10개는 실패했다

이유는 총합 값의 범위가 int의 범위보다 넘을 수도 있어서였다!!

반응형