<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:
    int solve(int idx, int tar, vector<int>& arr, int n, vector<vector<int>>& dp){
        if(idx==n-1){
            if(arr[idx]==tar)
                return true;
            return false;
        }
        if(tar==0)
            return true;
        if(dp[idx][tar]!=-1)
            return dp[idx][tar];
        //base case

        bool ntake = solve(idx+1, tar, arr, n ,dp);
        bool take = false;
        if(arr[idx]<=tar)
            take = solve(idx+1, tar-arr[idx], arr, n, dp);
        return dp[idx][tar]=take||ntake;
    }

    bool canPartition(vector<int>& nums) {
        int sum=0;
        int size=nums.size();
        for(int i=0;i<size;i++){
            sum+=nums[i];
        }
        if(sum%2!=0)
            return false;
        sum/=2;
        //vector<vector<int>> dp(size, vector<int>(sum+1, 0)); 
        vector<int> dp(sum+1, 0);
        dp[0] = 1;
        // for(int tar=0;tar<=sum;tar++){
        //     if(tar==sum)
        //         dp[tar]=1;
        // }
        for(int idx=1;idx<size;idx++){
            vector<int> curr(sum+1, 0);
            for(int tar=0;tar<=sum;tar++){
                int ntake = dp[tar];
                int take = 0;
                if(nums[idx]<=tar)
                    take = dp[tar-nums[idx]];
                curr[tar]=take|ntake;
            }
            dp=curr;
        }
        return dp[sum];
    }
};
96580