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.

Revisions

  1. Jon Bash created this gist Nov 13, 2019.
    14 changes: 14 additions & 0 deletions Sum.swift
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,14 @@
    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)