data-structures · intermediate · ~15 min
Count subsets summing to an exact target.
Implement:
long long count_subsets_target(const int *a, int n, int target);
Return the number of subsets of the non-negative a[0..n-1] that sum to exactly target.
Non-negative a, count n, non-negative target.
Number of subsets.
Downward target sweep, adding counts.
#include <stddef.h>
/* Number of subsets of a[0..n-1] (non-negative) whose elements sum to exactly target. target>=0. */
long long count_subsets_target(const int *a,int n,int target){ (void)a;(void)n;(void)target; return 0; }
Using OR (reachability) instead of += (counting).
target 0 -> 1 (the empty subset); a value of 0 doubles the count.
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.