Skip to content

Instantly share code, notes, and snippets.

@allurco
Last active September 17, 2017 14:35
Show Gist options
  • Save allurco/fff8bfba170d2fa5aa74f4055e166db4 to your computer and use it in GitHub Desktop.
Save allurco/fff8bfba170d2fa5aa74f4055e166db4 to your computer and use it in GitHub Desktop.
Max and Min sum of 4 values in array
import Foundation
let string = "1 2 3 4 5"
let numbers = string.split(separator: " ").map{Int($0)!}
var asc = numbers.sorted()
var desc = Array(asc.reversed())
print (asc)
asc.removeFirst()
desc.removeFirst()
print (asc)
var min = 0
var max = 0
for i in 0..<asc.count {
max = max + asc[i];
}
for i in 0..<desc.count {
min = min + desc[i];
}
print("\(min) \(max)")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment