-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlec10_44.cpp
More file actions
44 lines (34 loc) · 1.57 KB
/
Copy pathlec10_44.cpp
File metadata and controls
44 lines (34 loc) · 1.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
class Solution {
public:
// Helper function: Checks if a subset with the given target sum exists
bool isSubsetSum(vector<int>& arr, int target) {
int n = arr.size();
// Optimized DP arrays (previous & current row)
vector<bool> prev(target + 1, false), cur(target + 1, false);
// Base case: Sum 0 is always possible
prev[0] = cur[0] = true;
// If first element is within range, mark it
if (arr[0] <= target) prev[arr[0]] = true;
// Bottom-Up DP
for (int ind = 1; ind < n; ind++) {
for (int i = 1; i <= target; i++) {
bool notTake = prev[i]; // Exclude current element
bool take = false;
if (arr[ind] <= i)
take = prev[i - arr[ind]]; // Include current element
cur[i] = take || notTake; // Store result
}
prev = cur; // Move current row to previous
}
return prev[target]; // Final answer
}
// Main function: Checks if the array can be partitioned into two equal subsets
bool canPartition(vector<int>& arr) {
int n = arr.size();
int totSum = accumulate(arr.begin(), arr.end(), 0);
// If total sum is odd, partitioning is not possible
if (totSum % 2 != 0) return false;
int target = totSum / 2;
return isSubsetSum(arr, target);
}
};