Subset Sum Problem Geeks For Geeks

 class Solution{



    static boolean isSubsetSum(int n, int arr[], int sum){

        boolean [][] t=new boolean[n+1][sum+1];

        for(int i=0;i<n+1;i++){

            for(int j=0;j<sum+1;j++){

                if(i==0){

                    t[i][j]=false;

                }

                if(j==0){

                    t[i][j]=true;

                }

            }

        }

        

        for(int i=1;i<n+1;i++){

            for(int j=1;j<sum+1;j++){

                if(arr[i-1]>j){

                    t[i][j]=t[i-1][j];

                }

                else{

                    t[i][j]=(t[i-1][j-arr[i-1]]) || (t[i-1][j]);

                }

            }

        }

        return t[n][sum];

    }

}


APPROACH: Dynamic Programming

Here we will first initialize boolean matrix t of size n+1 and sum+1 with true or false.

If the sum is 0 then the size of the array does not matter and hence true will be stored.

If the sum is non zero but the size of an array is non zero then it is not possible. Hence false will be stored.

If an element has more value than the required sum then it will be neglected.

If the element has less value or equal value then the required sum it will be taken under consideration. 

Now it depends further either to take that element or not which will be decided by or operation between yes or no.  

In such a manner we will traverse the whole t array filling values in each cell and return the last cell result.


TIME: O(N*SUM) [Geeks For Geeks Time: 0.2/1.2]

SPACE: O(N*SUM)


Thanks for Reading.😇




Recursive Solution ***************************************************************


class Solution{

static boolean isSubsetSum(int n, int arr[], int sum){

//Base Condition

if(n==0){

return false;

}

else if(sum==0 && n!=0){

return true;

}

else if(sum==0 && n==0){

return true;

}

else{

if(arr[n-1]>sum){

return isSubsetSum(n-1,arr,sum);

}

else{

return isSubsetSum(n-1,arr,sum) || isSubsetSum(n-1,arr,sum-arr[n-1]) ;

}

}

}

}



TIME:O(N*Sum)

SPACE:O(N*Sum)

Here you will get a time limit exceeded error due to several recursive calls.


******************************************************************************


BY MEMOIZATION


class Solution{

//Base Condition
static boolean isSubsetSum(int n, int arr[], int sum){
    int t[][]=new int[n+1][sum+1];
    for(int i=0;i<n+1;i++){
for(int j=0;j<sum+1;j++){
t[i][j]=-1;
}
}
int b=isSubsetSum1(n,arr,sum,t);
if(b==1){
     return true;
}
else{
     return false;
}
}
public static int isSubsetSum1(int n, int arr[], int sum, int[][] t){
//Base Conditions
 
if(n==0 && sum!=0){
return 0;
//return false;
}
else if(n==0 && sum==0){
return 1;
//return true;
}
else if(sum==0){
return 1;
    // return true;
}

    //Main Code
else if(t[n][sum]!=-1){
return t[n][sum];
}
else{
if(arr[n-1]>sum){
t[n][sum]=isSubsetSum1(n-1,arr,sum,t);
}
else{
t[n][sum]=Integer.max(isSubsetSum1(n-1,arr,sum,t), isSubsetSum1(n-1,arr,sum-arr[n-1],t));
}
return t[n][sum];
}
}
}


TIME:O(N*SUM)[GFG TIME:0.2/1.2]

SPACE:O(N*SUM)



***********************************************************************************






Thanks for Reading.

"Share further to increase knowledge treasure."

Comments

Popular posts from this blog

Solutions Of Practice Questions Dated 01-06-2022

CODEFORCES SPY DETECTED ROUND 713

Array Formation HackerEarth