Problem Statement: Given a set of non-negative integers, and a value sum, determine if there is a subset of the given set with sum equal to given sum.
1. For every element In the array, we have a choice of whether to select this item or not.
2. If the value of the element is strictly greater than the Sum, the element cannot be taken. But, if the value of the element is not greater than Sum, then we have two choices. we either choose the element or we don't choose the element.
3. If we choose the element the Sum should be decrease by the value of the element. If we dont choose the element the Sum left unchanged.
4. After this the nth array element is now processed. we move to next array element which (n-1) the element with new Sum value. so now we have the new sub-porblem.
5. we do this recursively for every array element solving the subproblems eventually the original problem is solved.
/*
Let our arr[n] be the given array and DP[n][S], a boolean value, represent sif there exists a subset with sum S and n elements;
then,
if( arr[n] <= S) --> DP[n][S] = DP[n-1][S-arr[n]] or DP[n-1][S];
else --> DP[n][S] = dp[n-1][S];
*/
In recursive (TOP-DOWN) approach our task is to divide the given problem in the further subproblems.
// Recursive Method.
//Iterative Method
1. For every element In the array, we have a choice of whether to select this item or not.
2. If the value of the element is strictly greater than the Sum, the element cannot be taken. But, if the value of the element is not greater than Sum, then we have two choices. we either choose the element or we don't choose the element.
3. If we choose the element the Sum should be decrease by the value of the element. If we dont choose the element the Sum left unchanged.
4. After this the nth array element is now processed. we move to next array element which (n-1) the element with new Sum value. so now we have the new sub-porblem.
5. we do this recursively for every array element solving the subproblems eventually the original problem is solved.
/*
Let our arr[n] be the given array and DP[n][S], a boolean value, represent sif there exists a subset with sum S and n elements;
then,
if( arr[n] <= S) --> DP[n][S] = DP[n-1][S-arr[n]] or DP[n-1][S];
else --> DP[n][S] = dp[n-1][S];
*/
In recursive (TOP-DOWN) approach our task is to divide the given problem in the further subproblems.
// Recursive Method.
//Iterative Method