Suppose we have a set S with n number of elements. How many different ways can you select k elements from the set S. So we solve the problem using recursion. Here is a video explaining the problem and below that is the recursive implementation of the subset problem.
Implementation:
int C(int n,int k) {
if (k==0 || k==n)
return 1;
return C(n-1,k-1) + C(n-1,k);
}
No comments:
Post a Comment