Skip to content
DeepForgeby svx
All problems
EasyData Structuresds-349

D-ary Heap Height

Problem

A complete d-ary heap with n nodes has height h, the smallest integer such that 1 + d + ... + d^h >= n. Return the height in edges for n >= 0 and d >= 2; the height is 0 when n <= 1.

Hint

Level k of a complete d-ary tree holds d^k nodes, so add powers of d until the capacity reaches n.

Test cases

case 1
in: [10,2]
expected: 3
case 2
in: [9,3]
expected: 2
case 3
in: [1,2]
expected: 0
case 4
in: [0,5]
expected: 0
case 5
in: [8,2]
expected: 3
Starter codePython
def dary_heap_height(n, d):
    # Your code here
    pass

5 test cases · run your code against them in the editor