๐ WAYPOINT LESSON
Catalan Numbers
โญโญโญ advancedโณ 30 min read๐ Lesson 173 of 281
When counting structures whose every prefix is 'balanced', the answer is a Catalan number โ C(2n,n) - C(2n,n+1).
Counting balanced structures
Balanced brackets, binary trees, triangulations, non-crossing pairings: one sequence, C_n = C(2n,n) - C(2n,n+1). The subtraction removes the structures whose running balance dips negative โ a bijection to unrestricted paths ending one step off. Precompute factorials once; each query is O(1).