Skip to content

Instantly share code, notes, and snippets.

@mahasak
Forked from nuboat/TwoSum.scala
Last active August 29, 2015 14:21
Show Gist options
  • Save mahasak/53829e99b7f03c48fbe5 to your computer and use it in GitHub Desktop.
Save mahasak/53829e99b7f03c48fbe5 to your computer and use it in GitHub Desktop.
true
Execution n = 1007377 in 1521 ms
true
Tuning Execution n = 1007377 in 1394 ms
object TwoSum {
def sum(n: Int, sorted: ArrayBuffer[Int]): Boolean = sorted.par.find(x => isMatch(x, n, sorted)).isDefined
def isMatch(x: Int, n: Int, sorted: ArrayBuffer[Int]): Boolean = sorted.find( y => (x + y) == n).isDefined
}
object TwoSumTuning {
def sum(n: Int, sorted: ArrayBuffer[Int]): Boolean = sorted.par.find(x => isMatch(x, n, sorted)).isDefined
def isMatch(x: Int, n: Int, sorted: ArrayBuffer[Int]): Boolean = {
var result = false
breakable {
var a = 0
for (a <- x until sorted.size) {
if ((x + sorted(a)) == n) {
result = true
break
}
}
}
return result
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment