-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy path08 Mini-Max Sum.swift
42 lines (34 loc) · 1.13 KB
/
08 Mini-Max Sum.swift
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
//
// 08 Mini-Max Sum.swift
// HackerRank-Solutions/Problem Solving/Algorithms/01 Warmup
//
// Created by Aleksandar Dinic on 28/05/2020.
// Copyright © 2020 Aleksandar Dinic. All rights reserved.
//
import Foundation
/// Source: https://www.hackerrank.com/challenges/mini-max-sum/problem
struct Solution {
/// Finds the minimum and maximum values that can be calculated by summing exactly four
/// of the five integers.
///
/// - Parameter arr: An array of 5 integers.
/// - Returns: Minimum and maximum values.
///
/// - Complexity:
/// - time: O(n), where n is the length of arr.
/// - space: O(1), only constant space is used.
func miniMaxSum(_ arr: [Int]) -> [Int] {
var minEl = Int.max
var maxEl = Int.min
for a in arr {
minEl = min(minEl, a)
maxEl = max(maxEl, a)
}
let sum = arr.reduce(0, +)
return [sum - maxEl, sum - minEl]
}
}
let arr = readLine()!.split(separator: " ").compactMap { Int($0) }
let solution = Solution()
let ans = solution.miniMaxSum(arr)
print(ans.map { String($0) }.joined(separator: " "))