Skip to content

Instantly share code, notes, and snippets.

@junebash
Created November 13, 2019 16:18
Show Gist options
  • Save junebash/10b8506ab18cf5aedfd21fb95435fb79 to your computer and use it in GitHub Desktop.
Save junebash/10b8506ab18cf5aedfd21fb95435fb79 to your computer and use it in GitHub Desktop.
u2s3-codingChallenge - Sum
func indicesToSum(from array: [Int], to target: Int) -> [Int] {
for i in 1 ..< array.count {
for j in 0 ..< i {
if array[i] + array[j] == target { return [j,i] }
}
}
print("ERROR: No sum possible!")
return []
}
indicesToSum(from: [2,7,11,15], to: 9)
indicesToSum(from: [3,2,4], to: 6)
indicesToSum(from: [3,3], to: 6)
indicesToSum(from: [9,3,6,23,78,2,45,123,6,17], to: 140)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment