Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Roshankumar350/ce32efdc57e4acfa6cb257bd6444a3fe to your computer and use it in GitHub Desktop.
Save Roshankumar350/ce32efdc57e4acfa6cb257bd6444a3fe to your computer and use it in GitHub Desktop.
Day 1 of Advent Code
func findIndexOf(targetSum sum: Int, in inputArray: [Int]) -> (Int,Int) {
// Output Indices
var indices: (Int,Int) = (-1, -1)
// hastable
var hashTable = [Int:Int]()
// enumerate it
for (index, value) in inputArray.enumerated() {
// check if hastable has complementry value (i.e index)
if let rightIndex = hashTable[value] {
indices.0 = index
indices.1 = rightIndex
break
} else {
// set the key as 'compelementry value' and value as 'index'
hashTable[sum-value] = index
}
}
return indices
}
let indicess = findIndexOf(targetSum: 2020, in: inputArray)
print(inputArray[indicess.0] * inputArray[indicess.1])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment