avatar
Brocolling's Blog
[코딩테스트 일지] 해커랭크, Mini-Max Sum
해커랭크 - 1 Month Preparation Kit 2번 문제.
May 27
·
4 min read

코딩테스트 준비 일지

문제

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
arr = [1,3,5,7,9]
The minimum sum is 1+3+5+7 = 16 and the maximum sum is 3+5+7+9 = 24 . 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 5 integers

Print

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

Input Format

A single line of five space-separated integers.

Constraints

1 <= arr[i] <= 10^9

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 1, 2, 3, 4 and 5. Calculate the following sums using four of the five integers:

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

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

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

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

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

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

문제 풀이 접근

이 문제도 사실상 Basic 문제라, 어려울 건 없다.
딱 문제 보자마자 보이는 것이 있다. Minimum, Maximum 값 구해주세요.
5가지의 정수를 던져주고 Minimum은 당연히 제일 큰 수 빼고 더하는 것일 거고,
Maximum은 제일 작은 수 빼고 더하면 나올 수 있는 조합 중 가장 큰 수가 될 것 이다.
더군다나 친절하게도 Hint. 오버플로우를 조심해! 64 비트 정수형을 써라고 알려주니 당연히 long 으로 결과 값을 담아주었다.
원리는 아래에 설명되어있다.

  1. 최소, 최대 변수를 빼기 쉽게 정렬한다.

  2. 더하기 전, Minimum 은 Count -1 일 때, 더하지 않는다.

  3. Maximum은 0 일때 더하지 않는다.

작성 코드 ( C# )

using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.Collections;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.Serialization;
using System.Text.RegularExpressions;
using System.Text;
using System;
class Result
{/*
 * Complete the 'miniMaxSum' function below.
 *
 * The function accepts INTEGER_ARRAY arr as parameter.
 */
public static void miniMaxSum(List&lt;int&gt; arr)
{
long minimum = 0;
long maximum = 0;arr.Sort((a, b) =&gt;
{
return a.CompareTo(b);
});for(int i = 0; i &lt; arr.Count; ++i)
{
if (i == 0) continue;maximum += arr[i];}for (int i = 0; i &lt; arr.Count; ++i)
{
if (i == (arr.Count -1)) continue;minimum += arr[i];}Console.Write($"{minimum} {maximum}");}}class Solution
{
public static void Main(string[] args)
{    List&lt;int&gt; arr = Console.ReadLine().TrimEnd().Split(' ').ToList().Select(arrTemp =&gt; Convert.ToInt32(arrTemp)).ToList();Result.miniMaxSum(arr);}}







Dotorings,