Skip to content

Instantly share code, notes, and snippets.

@z0marlin
Created January 21, 2019 17:19
Show Gist options
  • Save z0marlin/4194448a4e40f71757114f9aee986a1a to your computer and use it in GitHub Desktop.
Save z0marlin/4194448a4e40f71757114f9aee986a1a to your computer and use it in GitHub Desktop.
WEC Codebuddy sem-4 week-2
class Solution {
public:
bool canPartition(vector<int>& nums) {
int sum = 0, n = nums.size();
for (auto a : nums){
sum += a;
}
if(sum&1)
return false;
sum >>= 1;
int A[n][sum + 1];
for(int i = 0; i < n; i++){
A[i][0] = 1;
}
for(int i = 1; i <= sum; i++)
if(i == nums[0])
A[0][i] = 1;
else
A[0][i] = 0;
for(int i = 1; i < n; i++){
for(int j = 1; j <= sum; j++){
A[i][j] = (j - nums[i]) >= 0 ? (A[i-1][j] || A[i-1][j - nums[i]]) : A[i-1][j];
}
}
if(A[n-1][sum])
return true;
return false;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment