Skip to content

Instantly share code, notes, and snippets.

@putraxor
Forked from esafirm/DenomSuggestion.kt
Created July 30, 2017 03:35
Show Gist options
  • Save putraxor/655c673bf48400407220c4d384aec0e6 to your computer and use it in GitHub Desktop.
Save putraxor/655c673bf48400407220c4d384aec0e6 to your computer and use it in GitHub Desktop.
Given some amount, return possible values that constructed by particular denomination
val denom = arrayOf(1, 2, 5, 10, 20, 50, 100)
val amount = arrayOf(5, 11, 53, 122, 155, 157, 210, 200)
fun main(args: Array<String>) = amount.forEach { currentAmount ->
val suggestions = mutableListOf<Int>()
denom.forEach {
if (it >= currentAmount) {
suggestions += it
} else {
val anotherFactor = Math.ceil(currentAmount.toDouble() / it)
suggestions += it * anotherFactor.toInt()
}
}
println("$currentAmount : ${suggestions.distinct()}")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment