id
int64
1
3.58k
problem_description
stringlengths
516
21.8k
instruction
int64
0
3
solution_c
dict
416
<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:<...
1
{ "code": "class Solution {\n \npublic:\n \n \n bool canPartition(vector<int>& nums) { \n int sum=0;\n for(int n:nums)\n sum+=n;\n if(sum%2!=0)\n return 0;\n int target = sum/2;\n vector<vector<bool>>possible_sums(target+1,vector<bool>(2,0));\n int...
416
<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:<...
1
{ "code": "class Solution {\npublic:\n bool canPartition(vector<int>& nums) {\n int sum = accumulate(nums.begin(), nums.end(), 0);\n if(sum%2)\n return false;\n int n = nums.size();\n int req = sum / 2;\n vector<vector<bool>> dp(req + 1, vector<bool>(n+1));\n fo...
416
<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:<...
1
{ "code": "class Solution {\npublic:\n\n int dp[200][20005];\n\n bool solve(int i,int sum,int target,vector<int>&nums){\n if(sum==target)return true;\n if(i==nums.size())return false;\n if(sum>target)return false;\n if(dp[i][sum]!=-1)return dp[i][sum];\n int take=solve(i+1,sum...
416
<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:<...
1
{ "code": "class Solution {\npublic:\n\n int dp[201][20001];\n\n bool compute(int n, int t, vector<int> &nums) {\n if(n == 0) return false;\n if(t == 0) return true;\n\n if(dp[n][t] != -1) return dp[n][t];\n\n if(t-nums[n-1] >= 0)\n return dp[n][t] = compute(n-1, t-nums[n-...
416
<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:<...
1
{ "code": "class Solution {\npublic:\n bool canPartition(vector<int>& nums) {\n long long sum=0;\n int n=nums.size();\n for(int i=0;i<nums.size();i++)\n {\n sum+=nums[i];\n }\n \n if(sum%2!=0)\n return 0;\n \n sum=sum/2;\n \n ...
416
<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:<...
1
{ "code": "class Solution {\npublic:\n bool canPartition(vector<int>& nums) {\n\n int n = nums.size();\n \n int tsum = accumulate(nums.begin(), nums.end(), 0);\n\n if(tsum %2) {\n return false;\n }\n\n vector<bool> dp(20000);\n dp[0] = dp[nums[0]] = true;...
416
<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:<...
1
{ "code": "class Solution {\npublic:\n bool canPartition(vector<int>& nums) {\n int sum = accumulate(nums.begin(), nums.end(), 0);\n if (sum % 2 == 1)\n return false;\n int halfSum = sum/2;\n vector<vector<char>> memos(nums.size(), vector<char>(halfSum+1, -1));\n retur...
416
<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:<...
1
{ "code": "class Solution {\npublic:\n bool canPartition(vector<int>& nums) {\n int sum = accumulate(nums.begin(), nums.end(), 0);\n if (sum % 2 == 1) return false;\n vector<vector<char>> memos(nums.size(), vector<char>(sum/2+1, -1));\n return dfs(nums, sum/2, 0, memos);\n }\n\n b...
416
<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:<...
1
{ "code": "class Solution {\npublic:\n bool canPartition(vector<int>& nums) {\n int t = 0;\n for (auto &n : nums) {\n t += n;\n }\n if (t % 2 == 1) return false;\n t = t / 2;\n unordered_set<int> us;\n queue<int> q;\n int tmp;\n for (auto &n...
416
<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:<...
1
{ "code": "class Solution {\npublic:\n bool canPartition(vector<int>& a) {\n int n=a.size();\n int s=0;\n for(auto i:a)\n s+=i;\n if(s%2)\n return false;\n int x=0,sz;\n queue<int> q;\n unordered_set<int> visited;\n q.push(0);\n f...
416
<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:<...
1
{ "code": "class Solution {\n void solve(vector<int>&nums,int i,int val,int sum,vector<vector<bool>>&dp){\n if(i>=nums.size()){\n return;\n }\n if(dp[i][sum]==true){\n return;\n }\n solve(nums,i+1,val,sum+nums[i],dp);\n solve(nums,i+1,val,sum,dp);\...
416
<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:<...
1
{ "code": "class Solution {\n void solve(vector<int>&nums,int i,int val,int sum,vector<vector<bool>>&dp){\n if(i>=nums.size()){\n return;\n }\n if(dp[i][sum]==true){\n return;\n }\n solve(nums,i+1,val,sum+nums[i],dp);\n solve(nums,i+1,val,sum,dp);\...
416
<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:<...
1
{ "code": "#include<bits/stdc++.h>\nusing namespace std;\n#define ll long long int\n#define fr(i,n) for(ll i=0;i<(n);++i)\n#define fr1(i,n) for(ll i=1;i<=(n);++i)\n\n\nll n;\nll dp[205][20005];\nll a[205];\nll pref[205];\nll rec(ll i,ll s1,ll s2){\n // pr(i,s1,s2,a[i],pref[i]);\n if(i==n){\n // pr(s1,s2);\n ...
416
<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:<...
1
{ "code": "class Solution {\npublic:\n bool canPartition(vector<int>& nums) {\n int total_sum = 0;\n for (auto elem : nums) {\n total_sum += elem;\n }\n\n if ((total_sum % 2) != 0) {\n return false;\n }\n\n std::unordered_set<int> prev, current;\n ...
416
<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:<...
1
{ "code": "class Solution {\npublic:\n int dp[200][40001];\n bool isPossible(vector<int> &nums, int i,int sum, int target){\n if(target==sum){\n return true;\n }\n if(i >= nums.size()){\n return false;\n }\n\n if(dp[i][sum]!=-1) return dp[i][sum];\n\n ...
416
<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:<...
1
{ "code": "int fun(int n,int temp_sum, vector< int > &nums, int dp[][40001]){\n \n if(temp_sum==0 && n==-1){\n return 1;\n }\n if(n==-1) return 0;\n \n int key;\n if(temp_sum<0) key=20000+abs(temp_sum);\n else key=...
416
<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:<...
1
{ "code": "class Solution {\npublic:\n bool canPartition(std::vector<int>& nums) {\n auto total = std::accumulate(nums.begin(), nums.end(), 0);\n if (total & 1) return false;\n auto target = total / 2 + 1;\n auto n = nums.size() + 1;\n std::vector<std::vector<uint8_t>> dp;\n ...
416
<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:<...
1
{ "code": "class Solution {\npublic:\n bool canPartition(std::vector<int>& nums) {\n auto total = std::accumulate(nums.begin(), nums.end(), 0);\n if (total & 1) return false;\n total = total / 2 + 1;\n auto n = nums.size() + 1;\n std::vector<std::vector<uint8_t>> dp;\n dp....
416
<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:<...
1
{ "code": "class Solution {\npublic:\n bool canPartition(vector<int>& nums) {\n int sum = 0;\n for (auto & num : nums) {\n sum += num;\n }\n\n if (sum % 2 == 1) {\n return false;\n }\n\n sum /= 2;\n vector<bool> dp(sum+1, false);\n dp[0]...
416
<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:<...
1
{ "code": "class Solution {\npublic:\n bool canPartition(vector<int>& nums) {\n const auto sum = std::accumulate(nums.begin(), nums.end(), 0);\n if (sum & 1U) return false;\n const auto target = sum / 2;\n std::ranges::sort(nums);\n vector<int> curr_sums{nums.front()};\n v...
416
<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:<...
1
{ "code": "class Solution {\npublic:\n int dp[1001][10010];\n bool check(int level,int sum,vector<int>& nums){\n if(level >= nums.size()){\n if(sum == 0){\n return true;\n }\n return false;\n }\n if(dp[level][sum] != -1){\n return d...
416
<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:<...
1
{ "code": "class Solution {\npublic:\n bool canPartition(vector<int>& nums) {\n sort(nums.begin(), nums.end());\n N = nums.size();\n ng = std::move(nums);\n int total = accumulate(ng.begin(),ng.end(), 0);\n if((total % 2) == 1)return false;\n return helper(0, total/2);\n ...
416
<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:<...
1
{ "code": "class Solution {\npublic:\n int target;\n vector<int> nums;\n int n;\n unordered_map<int, bool> myMap[205]; \n bool canPartition(vector<int>& nums) \n {\n \n int sum = accumulate(nums.begin(), nums.end(), 0);\n if(sum % 2 == 1) return false;\n sort(nums.rbeg...
416
<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:<...
1
{ "code": "class Solution {\npublic:\n bool canPartition(vector<int>& nums) {\n int sum = accumulate(nums.begin(), nums.end(), 0);\n if(sum % 2 == 1) return false;\n unordered_set<int> sums;\n sums.insert(0);\n int target = sum/2;\n for(size_t i = nums.size(); i --> 0;) {\...
416
<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:<...
1
{ "code": "#include <bits/stdc++.h>\n#include <ios>\nusing namespace std;\n#pragma GCC optimize (\"Ofast\")\n#pragma GCC target(\"sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx2,tune=native\")\n\ninline int gcd(int a,int b) { if (b==0) return a; return gcd(b, a%b); }\ninline int lcm(int a,int b) { return a/gcd(a,b)*b; }...
416
<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:<...
1
{ "code": "class Solution {\npublic:\n bool canPartition(vector<int>& nums) {\n int sum{};\n for(auto n : nums){\n sum += n;\n }\n\n if(sum % 2) return false;\n sum /= 2;\n\n unordered_set<int> uSet{};\n uSet.insert(nums[0]);\n\n for(int i = 1 ; i ...
416
<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:<...
1
{ "code": "class Solution {\npublic:\n bool canPartition(vector<int>& nums) {\n unordered_set<int> sums = {0};\n int sum = accumulate(nums.begin(), nums.end(), 0);\n if (sum % 2)\n return false;\n \n int target = sum / 2;\n for (int n : nums)\n {\n ...
416
<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:<...
1
{ "code": "class Solution {\npublic:\n vector<int> nums;\n map<pair<int, int>, bool> memo;\n\n bool canPartition(vector<int>& nums) {\n\n // sort this array to take advantage of early stopping\n sort(nums.begin(), nums.end());\n\n this->nums = nums;\n\n int sum = 0;\n for (...
416
<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:<...
1
{ "code": "class Solution {\npublic:\n vector<int> nums;\n map<pair<int, int>, bool> memo;\n\n bool canPartition(vector<int>& nums) {\n\n sort(nums.begin(), nums.end());\n\n this->nums = nums;\n\n int sum = 0;\n for (int num : nums) {\n sum += num;\n }\n\n ...
416
<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:<...
1
{ "code": "class Solution {\npublic:\n bool canPartition(vector<int>& nums) {\n int g= 0;\n for(int i :nums){\n g += i;\n }\n sort(nums.begin(),nums.end());\n if(g%2==0){\n int val =g/2;\n if(finder(val,nums,0)){\n return true;\n ...
416
<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:<...
1
{ "code": "map<pair<int,int>, bool> mp;\nclass Solution {\npublic:\n bool isPossiblePartition(vector<int>& nums) {\n int sum = 0;\n for(int i=0; i<nums.size(); i++) {\n sum += nums[i];\n }\n if(sum%2==0) {\n return true;\n }\n return false;\n }\n ...
416
<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:<...
1
{ "code": "class Solution {\n struct pair_hash {\n inline std::size_t operator()(const std::pair<int,int> & v) const {\n return v.first*31+v.second;\n }\n };\n using cache = unordered_set<pair<int, int>, pair_hash>;\n\n bool dfs(int idx, int curr, int target, vector<int>& nums, cache& dp) {\n...
416
<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:<...
1
{ "code": "class Solution {\npublic:\n bool canPartition(vector<int>& nums) {\n int sum = accumulate(nums.begin(), nums.end(),0);\n if(sum%2!=0){\n return false;\n }\n else{\n int n= nums.size();\n vector<vector<bool>> dp(201,vector<bool>(20001,false));\...
416
<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:<...
1
{ "code": "class Solution {\npublic:\n // bool f(int idx, int k,vector<int> &arr,vector<vector<int>> &dp){\n // if(k == 0) return true;\n // if(idx==0) return arr[0]==k;\n // if(dp[idx][k]!=-1) return dp[idx][k];\n \n // bool not_take = f(idx-1,k,arr,dp);\n // bool take= ...
416
<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:<...
1
{ "code": "class Solution {\npublic:\n\n struct Cache{\n std::vector<int> values;\n int width;\n };\n\n int getId(int lastElement, int maxValue, const Cache &cache){\n //return lastElement + cache.width * maxValue;\n return cache.width * lastElement + maxValue;\n }\n\n bool ...
416
<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:<...
1
{ "code": "class Solution {\npublic:\n\n struct Cache{\n std::vector<int> values;\n int width;\n };\n\n int getId(int lastElement, int maxValue, const Cache &cache){\n return lastElement + cache.width * maxValue;\n }\n\n bool canPartition(vector<int>& nums) {\n \n aut...
416
<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:<...
1
{ "code": "int dp[200][100000];\nclass Solution {\npublic:\n bool util(vector<int>& nums, int index, int n, int sum, int curr) {\n if (curr == sum - curr) return 1;\n if (index == n) return 0;\n if (dp[index][curr] != -1) return dp[index][curr];\n bool include = util(nums, index + 1, n,...
416
<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:<...
1
{ "code": "int t[201][100005];\nclass Solution {\npublic:\n bool isSubsetPos(vector<int>& nums,int n,int sum){\n if(n==0){\n return false;\n }\n if(sum==0){\n return true;\n }\n if(t[n][sum]!=-1){\n return t[n][sum];\n }\n if(nums[n-...
416
<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:<...
1
{ "code": "class Solution {\npublic:\n bool canPartition(vector<int>& nums) {\n int sum = 0;\n int n = nums.size();\n\n for(int i=0; i<n; i++)\n {\n sum+=nums[i];\n }\n\n if(sum %2 != 0) return false;\n int target = sum/2;\n\n vector<int> prev(targ...
416
<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:<...
1
{ "code": "class Solution {\npublic:\n bool canPartition(vector<int>& nums) {\n int n = nums.size();\n int sum = accumulate(nums.begin(), nums.end(), 0);\n if(sum%2) return 0;\n int target = sum / 2;\n vector<int> prev(target+1, 0);\n if(nums[0] <= target) prev[nums[0]] = ...
416
<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:<...
1
{ "code": "class Solution {\npublic:\n int solve(int idx, int tar, vector<int>& arr, int n, vector<vector<int>>& dp){\n if(idx==n-1){\n if(arr[idx]==tar)\n return true;\n return false;\n }\n if(tar==0)\n return true;\n if(dp[idx][tar]!=-1)...
416
<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:<...
1
{ "code": "class Solution {\npublic:\n bool isEqual(int i,vector<int>& nums,int k,vector<vector<int>>& dp){\n if(k==0)\n return 1;\n else if(k<0)\n return 0;\n if(i==nums.size()){\n return false;\n }\n if(dp[i][k]!=-1)\n return dp[i][k];\n r...
416
<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:<...
1
{ "code": "class Solution {\npublic:\n bool recTab(vector<int>& v, int n, int k, vector<int>& prev)\n {\n prev[0] = 1;\n if(v[0]<=k) prev[v[0]]=1;\n\n for(int idx=1; idx<n; idx++)\n {\n vector<int> curr(k+1, 0);\n curr[0]=1;\n for(int tar=1; tar<=k; t...
416
<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:<...
3
{ "code": "class Solution {\npublic:\n\n bool isSubsetSum(vector<int>arr, int sum){\n int n = arr.size();\n vector<vector<int>> dp(n+1 , vector<int> (sum+1 , 0)); \n vector<int> curr (sum+1 , 0);\n vector<int>prev (sum+1 , 0);\n \n if (arr[0] <= sum){\n prev[arr...
416
<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:<...
3
{ "code": "class Solution {\npublic:\nbool solveUsingRecursion(vector<int>&arr,int index,int currSum,int target)\n{\n if(index >= arr.size()){\n return false;\n }\n if(currSum > target){\n return false;\n }\n if(currSum==target)\n return true;\n bool include=solveUsingRecursion(arr,...
416
<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:<...
3
{ "code": "class Solution {\npublic:\n bool isSubsetSum(vector<int>& arr, int sum){\n int n = arr.size();\n vector<vector<bool>> dp(n, vector<bool>(1e5 + 1, 0));\n for(int i = 0;i < n;i++) dp[i][0] = true;\n dp[0][arr[0]] = true;\n for(int ind = 1;ind < n;ind++) {\n fo...
416
<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:<...
3
{ "code": "class Solution {\npublic:\n bool isSubsetSum(vector<int>& arr, int sum){\n int n = arr.size();\n vector<vector<bool>> dp(n, vector<bool>(1e5 + 1, 0));\n for(int i = 0;i < n;i++) dp[i][0] = true;\n dp[0][arr[0]] = true;\n for(int ind = 1;ind < n;ind++) {\n fo...
416
<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:<...
3
{ "code": "class Solution {\n bool func(int ind, vector<int>& nums, int sum, vector<vector<int>>&dp) {\n if (sum == 0)\n return true;\n if (ind == 0)\n return nums[0] == sum;\n\n if(dp[ind][sum] != -1) return dp[ind][sum];\n\n bool n = func(ind - 1, nums, sum, dp);...
416
<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:<...
3
{ "code": "class Solution {\npublic:\n // Analysis\n // method1: greedy (DP..)\n // step1: sort the array, then pick the last one, ...\n // -- 其实不用排序也行的,无非就是随机挑选(the last...)\n // -- 复杂度(N * N * sum);\n\n // Optimization: 优化\n // 从尾部开始判断,是否要(1-不取;0-取)\n // 那么复杂度就是2 (每个问题判断两次),也就是(N*sum);\n ...
416
<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:<...
3
{ "code": "class Solution {\npublic:\n int solve(vector <int> &nums, int index, int amount, vector <vector<int>> &dp){\n if(amount < 0) return 0;\n if(amount == 0) return 1;\n if(index<0) return 0;\n if(dp[index][amount]!=-1) return dp[index][amount];\n int a1,a2;\n a1 = s...
416
<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:<...
3
{ "code": "class Solution {\npublic:\n bool canPartition(vector<int>& nums) {\n // Calculate the total sum of the array\n int total = accumulate(nums.begin(), nums.end(), 0);\n\n // If the total sum is odd, it's not possible to split the array into two equal subsets\n if (total % 2 != 0...
416
<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:<...
3
{ "code": "class Solution {\npublic:\n bool f(vector<int>& nums, int k, int ind, vector<vector<int>> dp) {\n if (k == 0)\n return true;\n if (ind == 0)\n return (nums[0] == k);\n\n if (dp[ind][k] != -1)\n return dp[ind][k];\n bool nottake = f(nums, k, in...
416
<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:<...
3
{ "code": "class Solution {\npublic:\n bool canPartition(vector<int>& nums) {\n if (nums.size() == 1) return false;\n\n int sum = 0;\n for (int i = 0; i < nums.size(); i++) {\n sum += nums[i];\n }\n\n if (sum % 2 != 0) return false;\n\n int target = sum / 2;\n\n...
416
<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:<...
3
{ "code": "class Solution {\npublic:\n bool canPartition(vector<int>& nums) {\n int sum = 0;\n for(int i:nums){\n sum+=i;\n }\n sort(nums.begin(),nums.end());\n int n = nums.size();\n if(sum%2==1)return false;\n sum = sum/2;\n vector<vector<int> > ...
416
<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:<...
3
{ "code": "class Solution {\npublic:\nint solve(vector<int>& nums,int n,int sum1,int i, vector<vector<int>>&v)\n{\n if(i>=n)return 0;\n if(sum1>0 && i>=n)return 0;\n if(sum1==0)return 1;\n if(v[sum1][i]!=-1)return v[sum1][i];\n int take=0;int nottake=0;\n if(nums[i]<=sum1)\n take=solve(nums,n,sum1-...
416
<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:<...
3
{ "code": "class Solution {\npublic:\n bool canPartition(vector<int>& nums) {\n unordered_set<int> dp;\n dp.insert(0);\n int sum = 0;\n\n for (int num : nums) {\n sum += num;\n vector<int> current(dp.begin(), dp.end()); \n for (int partialSum : current) ...
416
<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:<...
3
{ "code": "class Solution {\nunordered_map<int, unordered_set<int>> dp;\n\n int sumArr(vector<int>& nums) {\n int s = 0;\n for (int n : nums) s += n;\n return s;\n }\n\n bool seenBefore(int index, int target) {\n return dp.find(index) != dp.end() &&\n dp[index].find(tar...
416
<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:<...
3
{ "code": "class Solution {\npublic:\n bool canPartition(vector<int>& nums) {\n int sum = 0;\n\n for (auto e: nums) {\n sum += e;\n }\n\n set<int> candidates;\n\n candidates.insert(0);\n\n for (auto e: nums) {\n vector<int> newCandidates;\n ...
416
<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:<...
3
{ "code": "class Solution {\npublic:\n bool checkSum(vector<int>&nums,int ind,int target,vector<vector<int>>&dp){\n if(target == 0) return true;\n if(ind==0) return (nums[ind]==target);\n if(dp[ind][target]!=-1) return dp[ind][target];\n bool notTake = checkSum(nums,ind-1,target,dp);\n ...
416
<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:<...
3
{ "code": "class Solution {\npublic:\n\n int helper(int index,vector<int>& nums,int sum,int target,vector<vector<int>> &dp)\n { \n if(sum>target || index==nums.size())\n {\n return 0;\n }\n if(sum==target)\n {\n return 1;\n }\n if(dp[index...
416
<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:<...
3
{ "code": "class Solution {\npublic:\n bool checkSum(vector<int>&nums,int ind,int target,vector<vector<int>>&dp){\n if(target == 0) return true;\n if(ind==0) return (nums[ind]==target);\n if(dp[ind][target]!=-1) return dp[ind][target];\n bool notTake = checkSum(nums,ind-1,target,dp);\n ...
416
<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:<...
3
{ "code": "class Solution {\npublic:\n bool helper(vector<int>&nums, int sum,int si, map<pair<int,int>,int>&mp){\n if(sum<0){return false;}\n if(sum == 0){return true;}\n if(si >= nums.size()){return false;}\n if(mp[{sum,si}]){\n if(mp[{sum,si}] == 1){return true;}\n return f...
416
<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:<...
3
{ "code": "class Solution {\npublic:\n bool findTheAns(vector<int>&nums,int sum,int index, map<pair<int,int>,int>& dp){\n if(index==nums.size()){\n if(sum==0){\n return true;\n }\n return false;\n }\n if(dp.find({index,sum})!=dp.end()){\n ...
416
<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:<...
3
{ "code": "class Solution {\npublic:\n\n bool result;\n int target;\n unordered_map<int,unordered_map<int,bool>> dp;\n\n bool helper(vector<int> & nums, int sum, int idx){\n if(idx>=nums.size() or sum>target){\n dp[sum][idx]=false;\n return false;\n }\n \n ...
416
<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:<...
3
{ "code": "class Solution {\npublic:\n bool solveByTabu(vector<int>& nums, int target){\n int n = nums.size();\n vector<vector<int>>dp(n+50, vector<int>(target+100,0));\n\n for(int row=0; row<= nums.size(); row++){\n dp[row][target]=1;\n }\n \n for(int index= n-1...
416
<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:<...
3
{ "code": "class Solution {\npublic:\n bool canPartition(vector<int>& nums) {\n unordered_set<int> dp0;\n \n int array_sum = accumulate(nums.begin(), nums.end(), 0);\n if (array_sum % 2 != 0) {\n return false;\n }\n\n dp0.insert(nums[0]);\n int sum = nums...
416
<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:<...
3
{ "code": "class Solution {\npublic:\n bool canPartition(vector<int>& nums) {\n unordered_set<int> dp0;\n \n int array_sum = accumulate(nums.begin(), nums.end(), 0);\n if (array_sum % 2 != 0) {\n return false;\n }\n\n dp0.insert(nums[0]);\n\n for (int i =...
416
<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:<...
3
{ "code": "class Solution {\npublic:\n bool canPartition(vector<int>& nums) {\n unordered_set<int> dp0;\n \n int array_sum = accumulate(nums.begin(), nums.end(), 0);\n if (array_sum % 2 != 0) {\n return false;\n }\n\n dp0.insert(nums[0]);\n int sum = nums...
416
<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:<...
3
{ "code": "class Solution {\npublic:\n \n bool canPartition(vector<int>& nums) {\n \n int n = nums.size();\n \n int ac = accumulate(nums.begin(), nums.end(), 0);\n if(ac % 2) return false;\n int hf = ac/2;\n \n unordered_set<int> s, s1;\n for(int i=...
416
<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:<...
3
{ "code": "class Solution {\npublic:\n bool solve(vector<int>&nums,int x,int index,vector<vector<int>>&dp){\n if(x==0){\n return true;\n }\n if(x<0){\n return 0;\n }\n if(index>=nums.size()){\n return 0;\n }\n if(dp[x][index]!=-1){\n return dp[x][index];\n }\n ...
416
<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:<...
3
{ "code": "class Solution {\npublic:\n bool canPartition(vector<int>& nums) {\n int target = 0;\n for(auto &x:nums) target += x;\n if (target%2 != 0) return false;\n target /= 2;\n int n = nums.size();\n\n unordered_set<int> dp,temp;\n dp.insert(0);\n for(int...
416
<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:<...
3
{ "code": "class Solution {\npublic:\n bool canPartition(vector<int>& nums) {\n int target = 0;\n for(auto &x:nums) target += x;\n if (target%2 != 0) return false;\n target /= 2;\n int n = nums.size();\n\n unordered_set<int> dp,temp;\n dp.insert(0);\n for(int...
416
<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:<...
3
{ "code": "class Solution {\npublic:\n bool canPartition(vector<int>& nums) {\n //sort from the biggest to the lowest O(nlogn)\n std::sort(nums.begin(), nums.end(), std::greater<int>());\n int sum = 0;\n sum = std::accumulate(nums.begin(), nums.end(), sum);\n if (sum % 2 ...
416
<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:<...
3
{ "code": "class Solution {\npublic:\n bool canPartition(vector<int>& nums) {\n //sort from the biggest to the lowest O(nlogn)\n std::sort(nums.begin(), nums.end(), std::greater<int>());\n int sum = 0;\n sum = std::accumulate(nums.begin(), nums.end(), sum);\n if (sum % 2 == 1) {\...
416
<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:<...
3
{ "code": "class Solution {\npublic:\n bool helper(int ind,vector<vector<int>>& dp,vector<int>& nums,int target){\n if(target==0) return true;\n if(ind==0) return (nums[0]==target);\n if(dp[ind][target]!=-1) return dp[ind][target];\n bool notTake=helper(ind-1,dp,nums,target);\n b...
416
<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:<...
3
{ "code": "class Solution {\npublic:\n bool canPartition(vector<int>& nums) {\n unordered_set<int> set;\n set.insert(0);\n\n unordered_set<int> nextSet;\n\n int target = 0;\n\n for (int n : nums) {\n target += n;\n }\n\n if (target % 2 != 0) return false;...
416
<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:<...
3
{ "code": "class Solution {\npublic:\nvector<vector<int>>dp;\nbool f(vector<int>&v,int t,int i ){\n if(t<0)return false;\n if(t==0)return true;\n if(dp[i][t]!=-1)return dp[i][t];\n if(i>=v.size()&&t!=0)return false;\n return dp[i][t]= f(v,t-v[i],i+1)||f(v,t,i+1);\n}\n bool canPartition(vector<int>& ...
416
<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:<...
3
{ "code": "class Solution {\npublic:\n vector<vector<int>> dp;\n bool f(vector<int>& v,int i,int s){\n if(s==0) return dp[i][s] = 1;\n if(i>=v.size()) return dp[i][s] = 0;\n if(dp[i][s]!=-1) return dp[i][s];\n if(v[i]<=s) return dp[i][s] = (f(v,i+1,s) || f(v,i+1,s-v[i]));\n e...
416
<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:<...
3
{ "code": "class Solution {\npublic:\n\n bool helper(int index ,vector<int>&arr,int sum,int propagatedsum,vector<vector<int>>&dp)\n {\n if(propagatedsum==sum)\n {\n return true;\n }\n if(index==arr.size() || propagatedsum > sum)\n {\n // if(sum+arr[index]==propagatedsum)...
416
<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:<...
3
{ "code": "class Solution {\npublic:\n\n bool helper(int index ,vector<int>&arr,int sum,int propagatedsum,vector<vector<int>>&dp)\n {\n if(propagatedsum==sum)\n {\n return true;\n }\n if(index==arr.size() || propagatedsum > sum)\n {\n // if(sum+arr[index]==propagatedsum)...
416
<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:<...
3
{ "code": "class Solution {\npublic:\n\n struct Cache{\n std::vector<std::optional<int>> values;\n int width;\n };\n\n int getId(int lastElement, int maxValue, const Cache &cache){\n return maxValue + cache.width * lastElement;\n }\n\n int getMaxImpl(int lastElement, int maxValue, ...
416
<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:<...
3
{ "code": "class Solution {\npublic:\n\n struct Cache{\n std::vector<std::optional<int>> values;\n int width;\n };\n\n int getId(int lastElement, int maxValue, const Cache &cache){\n return lastElement + cache.width * maxValue;\n }\n\n int getMaxImpl(int lastElement, int maxValue, ...
416
<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:<...
3
{ "code": "class Solution {\npublic:\n bool canPartition(vector<int>& nums) {\n int target = accumulate(nums.begin(), nums.end(), 0);\n\n if (target % 2) {\n return false;\n }\n\n vector<vector<char>> checkedSums(nums.size(), vector<char>(20000));\n\n return checkNums(...
416
<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:<...
3
{ "code": "class Solution {\npublic:\n bool canPartition(vector<int>& nums) {\n int target = 0, n = nums.size();\n target = accumulate(nums.begin(), nums.end(), target);\n if(target % 2 != 0){ return false; }\n target /= 2;\n unordered_set <int> dp, temp;\n dp.insert(nums[...
416
<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:<...
3
{ "code": "class Solution {\npublic:\n bool canPartition(vector<int>& nums) {\n int target = 0, n = nums.size();\n target = accumulate(nums.begin(), nums.end(), target);\n if(target % 2 != 0){ return false; }\n target /= 2;\n unordered_set <int> dp, temp;\n dp.insert(nums[...
416
<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:<...
3
{ "code": "class Solution {\npublic:\n bool canPartition(vector<int>& nums) {\n // dp[i, k] -> when the elements from ith to n-1th index can be picked and current diff of sum is k\n // dp[i, k] = (dp[i+1, k-2*nums[i]] || dp[i+1, k])\n // dp[i, 0] = 1 and dp[i, k!=0] = 0\n // dp[0, total...
416
<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:<...
3
{ "code": "class Solution {\npublic:\n bool canPartition(vector<int>& nums) {\n int n=nums.size(), sum = 0;\n for (int i=0; i<n; i++) sum += nums[i];\n if (sum %2) return false;\n vector<int> mp(sum+1, 0);\n mp[0] = 1;\n mp[nums[0]] = 1;\n for (int i=1; i<n; i++) {\...
416
<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:<...
3
{ "code": "class Solution {\npublic:\n bool canPartition(vector<int>& nums) {\n int s=0;\n for(int i=0; i<nums.size(); i++)\n s += nums[i];\n if(s%2) return false;\n\n vector<vector<int>>dp(nums.size() + 1, vector<int>(s + 1, 0));\n return helper(nums, 0, s/2, dp);\n ...
416
<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:<...
3
{ "code": "class Solution {\npublic:\n vector<vector<int>> dp;\n bool solve(int idx, int sum, vector<int>&A)\n {\n if(idx==A.size())\n {\n if(sum==0) return true;\n return false;\n }\n if(dp[idx][sum]!=-1) return dp[idx][sum];\n if(A[idx]<=sum)\n ...
416
<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:<...
3
{ "code": "class Solution\n{\npublic:\n bool canPartition(vector<int>& nums)\n {\n int sum = 0;\n for (int i = 0; i < nums.size(); i++)\n sum += nums[i];\n if (sum % 2 == 1)\n return false;\n sum /= 2;\n unordered_map<int, bool> mp;\n mp[0] = true;...
416
<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:<...
3
{ "code": "class Solution {\npublic:\n bool canPartition(vector<int>& nums) {\n int target = reduce(nums.begin(), nums.end());\n\n if (target & 0x1)\n return false;\n \n target /= 2;\n\n // bitset<20000> b;\n\n // for (const auto &el: nums) {\n // b |...
416
<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:<...
3
{ "code": "class Solution {\npublic:\n\nbool helper(int i , int sum1 , int n , vector<int>arr , vector<vector<int>> &dp){\n if(sum1 == 0){\n return true;\n }\n\n if(sum1 < 0 || i>=n){\n return false;\n }\n\n if(dp[i][sum1] != -1){\n return dp[i][sum1];\n }\n\n int inc = help...
416
<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:<...
3
{ "code": "class Solution {\npublic:\n bool canPartition(vector<int>& nums) {\n int target = 0;\n for(auto& i : nums) target += i;\n if(target % 2 == 1) return false;\n target /= 2;\n unordered_set<int> main_set;\n main_set.insert(0);\n main_set.insert(nums[0]);\n\n...
416
<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:<...
3
{ "code": "class Solution {\npublic:\n bool canPartition(vector<int>& nums) {\n int total = accumulate(nums.begin(), nums.end(), 0);\n if (total & 1) return false;\n int target = total / 2;\n unordered_set<int> powerset({0});\n for (auto& n : nums) {\n // Create a copy...
416
<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:<...
3
{ "code": "class Solution {\npublic:\n bool canPartition(vector<int>& nums) {\n int sum = accumulate(nums.begin(), nums.end(), 0);\n\n if (sum & 1) return false;\n int target = sum / 2;\n\n unordered_set<int> s;\n s.insert(0);\n\n for (int num : nums) {\n if (nu...
416
<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:<...
3
{ "code": "class Solution {\npublic:\n bool canPartition(vector<int>& nums) {\n // brute force:\n // generate all partitions\n // compute sums\n\n int target = 0;\n for(const int& n : nums) target += n;\n if(target % 2) return false; // if odd, impossible\n ...
416
<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:<...
3
{ "code": "class Solution {\npublic:\n bool canPartition(vector<int>& nums) {\n // brute force:\n // generate all partitions\n // compute sums\n\n int target = 0;\n for(const int& n : nums) target += n;\n // if odd, partition impossible\n if(target % 2) retu...
416
<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:<...
3
{ "code": "class Solution {\npublic:\n bool canPartition(vector<int>& nums) {\n int totalSum = 0;\n \n // Calculate total sum of the array\n for (int num : nums) {\n totalSum += num;\n }\n \n // If the total sum is odd, we cannot partition into two equal subsets\n if (totalSum % ...
416
<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:<...
3
{ "code": "class Solution {\npublic:\n bool canPartition(vector<int>& nums) {\n int totalSum = 0;\n\n for(auto it: nums) {\n totalSum += it;\n }\n\n if(totalSum % 2 != 0) {\n return false;\n }\n\n int target = totalSum / 2;\n int n = nums.size(...
416
<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:<...
3
{ "code": "class Solution {\npublic:\n bool canPartition(vector<int>& nums) {\n int totalSum = 0;\n for(int num : nums) {\n totalSum += num;\n }\n if(totalSum % 2 == 1) {\n return false;\n }\n int target = totalSum / 2;\n\n unordered_set<int> s...