<p>Given an integer array <code>nums</code>, return <code>true</code> <em>if you can partition the array into two subsets such that the sum of the elements in both subsets is equal or </em><code>false</code><em> otherwise</em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> nums = [1,5,11,5]
<strong>Output:</strong> true
<strong>Explanation:</strong> The array can be partitioned as [1, 5, 5] and [11].
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> nums = [1,2,3,5]
<strong>Output:</strong> false
<strong>Explanation:</strong> The array cannot be partitioned into equal sum subsets.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= nums.length &lt;= 200</code></li>
	<li><code>1 &lt;= nums[i] &lt;= 100</code></li>
</ul>

class Solution {
public:
bool solveUsingRecursion(vector<int>&arr,int index,int currSum,int target)
{
    if(index >= arr.size()){
        return false;
    }
    if(currSum > target){
        return false;
    }
    if(currSum==target)
    return true;
    bool include=solveUsingRecursion(arr,index+1,currSum+arr[index],target);
    bool exclude=solveUsingRecursion(arr,index+1,currSum,target);
    return include || exclude;
}
bool solveUsingMem(vector<int>&arr,int index,int currSum,int target,
    vector<vector<int> >&dp) {

    if(index >= arr.size())  return false;
    if(currSum > target) return false;
    if(currSum == target) return true;

    if(dp[index][currSum] != -1){
        return dp[index][currSum];
    }

    int include=solveUsingMem(arr,index+1,currSum+arr[index],target,dp);
    int exclude=solveUsingMem(arr,index+1,currSum,target,dp);

    dp[index][currSum]=(include || exclude);
    return dp[index][currSum];
    
}
bool solveUsingTab(vector<int>&arr,int target){
    int n = arr.size();
    vector<vector<int> >dp(n+1,vector<int>(target+1,0));

    for(int row=0; row<=n; row++) 
    dp[row][target] = 1;

    for(int index=n-1; index>=0; index--){
        for(int s=target; s>=0; s--){
            bool include=0;
            if(s+arr[index] <= target)
               include=dp[index+1][s+arr[index]];

               bool exclude=dp[index+1][s];
               dp[index][s]=( include || exclude );
        }
    }
    return dp[0][0];
}
bool solveUsingTabSO(vector<int>&arr,int target ){
    int n=arr.size();
    vector<int>curr(target+1,0);
    vector<int>next(target+1,0);
    curr[target]=1;
    next[target]=1;
    for(int index=n-1; index>=0; index--){
        for(int s=target; s>=0; s--){
            bool include=0;
            if(s+arr[index] <= target)
            include=next[s+arr[index]];

            bool exclude=next[s];
            curr[s]=(include||exclude);
        }
        // shifting 
        next=curr;
    }
    return next[0];

}




    bool canPartition(vector<int>& nums) {
        int index=0;
        int totalSum=0;
        for(int i=0; i<nums.size(); i++){
            totalSum+=nums[i]; 
        }
        if(totalSum & 1){
            // odd cannot be partitioned
            return false;
        }
        int target=totalSum/2;
        int currSum = 0;
        vector<vector<int> >dp(nums.size()+1,vector<int>(target+1,-1));
        bool ans = solveUsingTabSO(nums,target);
        return ans;
        
    }
};
102308