I came across one algorithm problem in the book Competitive programming handbook https://cses.fi/book/book.pdf: Integer partitions, Knapsack.
The problem is: Given an array of integers whose sum is n. Output all possible sums that can be formed by using a subset of integers.
The dynamic programming solution is understandable. Define dp(x,k) as a bool function that is true when we can use the first k numbers to sum up to x, and false otherwise. Then dp(x,k) = dp(x,k-1) | dp(x-A[k],k-1). The complexity is O(n^2).
However, the book described another solution that leverages one fact that the number of distinct numbers in the array is O(sqrt (n)), and then mentioned a solution by grouping similar numbers together. How does it work? The book seems hard to follow.