본문 바로가기

카테고리 없음

해커랭크 Mini-Max Sum

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 Foundation

// Complete the miniMaxSum function below.
func miniMaxSum(arr: [Int]) -> Void {

    var sortedArr = arr.sorted(by: < )
    var min: Int = 0
    var max: Int = 0 

    min = sortedArr.prefix(4).reduce(0,+)
    max = sortedArr.suffix(4).reduce(0,+)
    print(min, max)
}

guard let arrTemp = readLine() else { fatalError("Bad input") }
let arr: [Int] = arrTemp.split(separator: " ").map {
    if let arrItem = Int($0.trimmingCharacters(in: .whitespacesAndNewlines)) {
        return arrItem
    } else { fatalError("Bad input") }
}

guard arr.count == 5 else { fatalError("Bad input") }

miniMaxSum(arr: arr)

 

* prefix()

*suffix()

*reduce(0, +) -> 초기값 0, 원소들을 더해라