Last active
March 2, 2023 10:29
-
-
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
This file contains 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
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