forked from kodecocodes/swift-algorithm-club
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMinimumMaximumPairs.swift
32 lines (28 loc) · 923 Bytes
/
MinimumMaximumPairs.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
/*
Finds the maximum and minimum value in an array in O(n) time.
*/
func minimumMaximum<T: Comparable>(_ array: [T]) -> (minimum: T, maximum: T)? {
guard var minimum = array.first else { return nil }
var maximum = minimum
// if 'array' has an odd number of items, let 'minimum' or 'maximum' deal with the leftover
let start = array.count % 2 // 1 if odd, skipping the first element
for i in stride(from: start, to: array.count, by: 2) {
let pair = (array[i], array[i+1])
if pair.0 > pair.1 {
if pair.0 > maximum {
maximum = pair.0
}
if pair.1 < minimum {
minimum = pair.1
}
} else {
if pair.1 > maximum {
maximum = pair.1
}
if pair.0 < minimum {
minimum = pair.0
}
}
}
return (minimum, maximum)
}