Subset sum equals to K | 4 Approaches | Paytm
Difficulty: EASYCategory: InterviewExperienceFrequency: 1Asked On 2022-09-16Added By Anonymous
Source: Link
Paytm | unknown | unknown | unknown | unknown
Question Link : https://www.codingninjas.com/codestudio/problems/subset-sum-equal-to-k_1550954?leftPanelTab=0
Approach 1 : Normal Recursion
``` bool solve(int i, int target, vector<int> &arr){ if(arr[i]==target){ return true; } if(i==0){ return arr[0]==target; } bool not_take = solve(i-1,target,arr); bool take = false; if(target>=arr[i]){ take = solve(i-1,target-arr[i],arr); } return take || not_take; } bool subsetSumToK(int n, int k, vector<int> &arr) { return solve(n-1,k,arr); } ``` Space : O(n) Time : O(2^n)
Approach 2 : Recursion -> Memoization * We can see that there are many subproblems that are computed already and now are being recomputed. * So, we are going to memoize it using a dp vector where we store the values which we calculate during recursion. If the value is stored in dp vector we will use it directly and won't recalculate the same thing again, otherwise we will solve that subproblem and store its value in dp vector. ``` bool solve(int i, int target, vector<int> &arr,vector<vector<int>>&dp){ if(arr[i]==target){ return true; } if(i==0){ return arr[0]==target; } if(dp[i][target]!=-1){ return dp[i][target]; } bool not_take = solve(i-1,target,arr,dp); bool take = false; if(target>=arr[i]){ take = solve(i-1,target-arr[i],arr,dp); } return dp[i][target] = take || not_take; } bool subsetSumToK(int n, int k, vector<int> &arr) { vector<vector<int>>dp(n,vector<int>(k+1,-1)); return solve(n-1,k,arr,dp); } ``` Space : O(n) for recursion + O(nk) for dp vector Time : O(nk)
Approach 3 : Tabulation * We can see that we are taking an extra space for recursion. Let's remove it by converting our top-down approach to an iterative bottom-up approach. ``` bool subsetSumToK(int n, int k, vector<int> &arr) { vector<vector<bool>>dp(n,vector<bool>(k+1,false)); for(int i = 0; i<n; i++){ dp[i][0] = true; } dp[0][arr[0]]=true; for(int i = 1; i<n ; i++){ for(int target = 1; target<=k ; target++){ bool not_take = dp[i-1][target]; bool take = false; if(target>=arr[i]){ take = dp[i-1][target-arr[i]]; } dp[i][target] = take || not_take; } } return dp[n-1][k]; } ``` Time : O(nk) Space : O(n^2)
Approach 4 : Space optimized to O(N) * We can reduce the 2d DP to O(N) solution by using 2 vectors - prev and curr. ``` bool subsetSumToK(int n, int k, vector<int> &arr) { vector<vector<bool>>dp(n,vector<bool>(k+1,false)); vector<bool>prev(k+1,false); vector<bool>curr(k+1,false); prev[0]=true; curr[0]=true; prev[arr[0]]=true; for(int i = 1; i<n ; i++){ for(int target = 1; target<=k ; target++){ bool not_take = prev[target]; bool take = false; if(target>=arr[i]){ take = prev[target-arr[i]]; } curr[target] = take || not_take; } prev = curr; } return prev[k]; } ``` Space : O(n) Time : O(nk)
P.S. 1. Upvote if helpful
Question Link : https://www.codingninjas.com/codestudio/problems/subset-sum-equal-to-k_1550954?leftPanelTab=0
Approach 1 : Normal Recursion
``` bool solve(int i, int target, vector<int> &arr){ if(arr[i]==target){ return true; } if(i==0){ return arr[0]==target; } bool not_take = solve(i-1,target,arr); bool take = false; if(target>=arr[i]){ take = solve(i-1,target-arr[i],arr); } return take || not_take; } bool subsetSumToK(int n, int k, vector<int> &arr) { return solve(n-1,k,arr); } ``` Space : O(n) Time : O(2^n)
Approach 2 : Recursion -> Memoization * We can see that there are many subproblems that are computed already and now are being recomputed. * So, we are going to memoize it using a dp vector where we store the values which we calculate during recursion. If the value is stored in dp vector we will use it directly and won't recalculate the same thing again, otherwise we will solve that subproblem and store its value in dp vector. ``` bool solve(int i, int target, vector<int> &arr,vector<vector<int>>&dp){ if(arr[i]==target){ return true; } if(i==0){ return arr[0]==target; } if(dp[i][target]!=-1){ return dp[i][target]; } bool not_take = solve(i-1,target,arr,dp); bool take = false; if(target>=arr[i]){ take = solve(i-1,target-arr[i],arr,dp); } return dp[i][target] = take || not_take; } bool subsetSumToK(int n, int k, vector<int> &arr) { vector<vector<int>>dp(n,vector<int>(k+1,-1)); return solve(n-1,k,arr,dp); } ``` Space : O(n) for recursion + O(nk) for dp vector Time : O(nk)
Approach 3 : Tabulation * We can see that we are taking an extra space for recursion. Let's remove it by converting our top-down approach to an iterative bottom-up approach. ``` bool subsetSumToK(int n, int k, vector<int> &arr) { vector<vector<bool>>dp(n,vector<bool>(k+1,false)); for(int i = 0; i<n; i++){ dp[i][0] = true; } dp[0][arr[0]]=true; for(int i = 1; i<n ; i++){ for(int target = 1; target<=k ; target++){ bool not_take = dp[i-1][target]; bool take = false; if(target>=arr[i]){ take = dp[i-1][target-arr[i]]; } dp[i][target] = take || not_take; } } return dp[n-1][k]; } ``` Time : O(nk) Space : O(n^2)
Approach 4 : Space optimized to O(N) * We can reduce the 2d DP to O(N) solution by using 2 vectors - prev and curr. ``` bool subsetSumToK(int n, int k, vector<int> &arr) { vector<vector<bool>>dp(n,vector<bool>(k+1,false)); vector<bool>prev(k+1,false); vector<bool>curr(k+1,false); prev[0]=true; curr[0]=true; prev[arr[0]]=true; for(int i = 1; i<n ; i++){ for(int target = 1; target<=k ; target++){ bool not_take = prev[target]; bool take = false; if(target>=arr[i]){ take = prev[target-arr[i]]; } curr[target] = take || not_take; } prev = curr; } return prev[k]; } ``` Space : O(n) Time : O(nk)
P.S. 1. Upvote if helpful