data-structures · intermediate · ~15 min
Decide whether a subset hits an exact target sum.
Implement:
int subset_sum(const int *a, int n, int target);
Return 1 if some subset of the non-negative a[0..n-1] sums exactly to target, else 0.
Non-negative a, count n, non-negative target.
1 if reachable, else 0.
Downward target sweep (0/1 selection).
#include <stddef.h>
/* Return 1 if some subset of a[0..n-1] (non-negative) sums exactly to target, else 0. target>=0. */
int subset_sum(const int *a,int n,int target){ (void)a;(void)n;(void)target; return 0; }
Upward sweep allows reusing an element; use a downward loop.
target 0 is always reachable (empty subset).
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.