Kraft Inequality Check

easy · compression, prefix-code, math

Kraft Check

Given a list of codeword lengths, determine whether a prefix-free code can exist.

Function

func KraftSatisfied(lengths []int) bool

Behavior

  • lengths[i] == 0 means the symbol is unused and should be ignored.
  • Return true iff sum 2^{-lengths[i]} <= 1.
  • If lengths is empty, return true.

Constraints

  • Each length is 0 <= l <= 30 (fits in 64-bit integer arithmetic).

Notes

  • Avoid floating-point error by using integer arithmetic.
Run tests to see results
No issues detected