fun climbingStaircase(n: Int, k: Int): MutableList> { val results: MutableList> = mutableListOf>() climbingStaircase(n, k, results, mutableListOf()) return results } fun climbingStaircase(n: Int, k: Int, results: MutableList>, path: MutableList) { if (n == 0) { results.add(path) } for (step in 1 .. k) { val stepsLeft = n - step if (stepsLeft >= 0) { path.add(step) climbingStaircase(stepsLeft, k, results, path) path.removeAt(path.size-1) } } } fun main(args: Array) { val n = 5 val k = 2 println("The ways to count ab $n-steps staircase while being able to take k steps where k in 1..$k steps is: ${climbingStaircase(n, k)}") // The ways to count ab 5-steps staircase while being able to take k steps where k in 1..2 steps is: [[], [], [], [], [], [], [], []] }