Subset Sum Problem

PHOTO EMBED

Mon May 29 2023 06:08:32 GMT+0000 (Coordinated Universal Time)

Saved by @Ranjan_kumar #c++

class Solution{   
public:

    bool solve(vector<int>&arr, int sum, int curr, int idx, vector<vector<int>>& dp)
    {
        if(curr==sum) return true;
        if(curr>sum||idx==arr.size()) return false;
        if(dp[curr][idx]!=-1) return dp[curr][idx];
        bool ck=false;
        ck=ck|solve(arr, sum, curr+arr[idx], idx+1, dp);
        ck=ck|solve(arr, sum, curr, idx+1, dp);
        return dp[curr][idx]=ck;
        
    }
    
    bool isSubsetSum(vector<int>arr, int sum){
        // code here 
        vector<vector<int>> dp(100000, vector<int> (arr.size(), -1));
       return solve(arr, sum, 0, 0, dp);
    }
};
content_copyCOPY

https://practice.geeksforgeeks.org/problems/subset-sum-problem-1611555638/1