-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMini-MaxSum.swift
44 lines (37 loc) · 998 Bytes
/
Mini-MaxSum.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
43
44
// https://www.hackerrank.com/challenges/mini-max-sum/problem
import Foundation
// Complete the miniMaxSum function below.
func miniMaxSum(arr: [Int]) -> Void {
let min: Int = getMin(arr)
let max: Int = getMax(arr)
var minSum: Int = 0
var maxSum: Int = 0
let isSameNum = arr.map { $0 }.reduce(0, +)
if (isSameNum % min == 0) && (isSameNum % max == 0) {
minSum = isSameNum - min
maxSum = isSameNum - max
}else{
for i in arr {
if i != min { minSum += i }
if i != max { maxSum += i }
}
}
print("\(maxSum) \(minSum)")
}
func getMin(_ arr: [Int]) -> Int {
var min: Int = 10000000000
for i in arr {
if i <= min { min = i }
}
return min
}
func getMax(_ arr: [Int]) -> Int {
var max: Int = 1
for i in arr {
if i >= max { max = i }
}
return max
}
miniMaxSum(arr: [5, 5, 5, 5, 5])
miniMaxSum(arr: [1, 2, 3, 4, 5])
miniMaxSum(arr: [7, 69, 2, 221, 8974])