Skip to content

Instantly share code, notes, and snippets.

@renanreismartins
Last active March 2, 2023 10:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save renanreismartins/2b330144a9eb4b74464df77d9fea5474 to your computer and use it in GitHub Desktop.
Save renanreismartins/2b330144a9eb4b74464df77d9fea5474 to your computer and use it in GitHub Desktop.
null pointers because of the null return when there is no combination, but fine
object HowSum extends App {
def howSum(n: Int, coins: Array[Int]): Array[Int] = {
if (n == 0) return Array[Int]()
if (n < 0) return null
for (coin <- coins) {
val rest = n - coin
val ints = howSum(rest, coins)
if (ints != null)
return Array.concat(Array[Int](coin), ints)
}
null
}
println(howSum(0, Array[Int](7)).mkString(","))
println(howSum(7, Array[Int](7)).mkString(","))
println(howSum(7, Array[Int](1)).mkString(","))
println(howSum(7, Array[Int](1, 7)).mkString(","))
println(howSum(7, Array[Int]( 2, 3, 8, 7)).mkString(","))
println()
println(howSum(300, Array[Int](7, 14)).mkString(","))
println(howSum(7, Array[Int](8)).mkString(","))
println(howSum(7, Array[Int](8, 7)).mkString(","))
println(howSum(7, Array[Int](5, 3, 4, 7)).mkString(","))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment