How One Operator Change Transforms Subset Sum Into a Counting Problem

A tutorial on dynamic programming explains how the 'Count of Subsets' problem differs from the classic Subset Sum by just one change: replacing the boolean 'or' operator with integer addition '+'. While Subset Sum asks whether a target sum is achievable, Count of Subsets asks in how many ways it can be achieved, shifting the DP table from storing booleans to storing counts. The base case requires careful handling — only T[0][0] should be set to 1, representing the empty subset, rather than setting the entire first column to 1, which breaks correctness when the array contains zeros. Zero-valued elements are a special trap because including or excluding them both leave the sum unchanged, meaning each zero doubles the valid subset count. The inner loop must also start from index 0 instead of 1 so that zero-valued elements are processed correctly through the transition logic.
This is an AI-generated summary. ShortSingh links to the original source for the complete article.

Discussion (0)
Log in to join the discussion and vote.
Log in