Skip to content

Instantly share code, notes, and snippets.

@esafirm
Last active July 30, 2017 03:35
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save esafirm/f77529919e426024d9ac1e53c6819f13 to your computer and use it in GitHub Desktop.
Save esafirm/f77529919e426024d9ac1e53c6819f13 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()}")
}
@esafirm
Copy link
Author

esafirm commented Jul 28, 2017

5 : [5, 6, 10, 20, 50, 100]
11 : [11, 12, 15, 20, 50, 100]
53 : [53, 54, 55, 60, 100]
122 : [122, 125, 130, 140, 150, 200]
155 : [155, 156, 160, 200]
157 : [157, 158, 160, 200]
210 : [210, 220, 250, 300]
200 : [200]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment