Created
December 6, 2020 19:51
-
-
Save Roshankumar350/ce32efdc57e4acfa6cb257bd6444a3fe to your computer and use it in GitHub Desktop.
Day 1 of Advent Code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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