-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMinimum_Partition.cpp
More file actions
179 lines (134 loc) · 4.89 KB
/
Copy pathMinimum_Partition.cpp
File metadata and controls
179 lines (134 loc) · 4.89 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
// Given an array arr[] of size n, the task is to divide it into two sets S1 and S2 such that the absolute difference between their sums is minimum.
// If there is a set S with n elements, then if we assume Subset1 has m elements, Subset2 must have n-m elements and the value of abs(sum(Subset1) – sum(Subset2)) should be minimum.
// Example:
// Input: arr = [1, 6, 11, 5]
// Output: 1
// Explanation: S1 = [1, 5, 6], sum = 12, S2 = [11], sum = 11, Absolute Difference (12 – 11) = 1
// Using Recursion – O(2^n) Time and O(n) Space
// C++ Code to partition a set into two
// subsets such that the difference
// of subset sums is minimum
#include <bits/stdc++.h>
using namespace std;
// Function to calculate the minimum absolute difference
int findMinDifference(vector<int> &arr, int n,
int sumCalculated, int sumTotal)
{
// Base case: if we've considered all elements
if (n == 0)
{
return abs((sumTotal - sumCalculated) - sumCalculated);
}
// Include the current element in the subset
int include = findMinDifference(arr, n - 1,
sumCalculated + arr[n - 1], sumTotal);
// Exclude the current element from the subset
int exclude = findMinDifference(arr,
n - 1, sumCalculated, sumTotal);
// Return the minimum of both choices
return min(include, exclude);
}
// Function to get the minimum difference
int minDifference(vector<int> &arr)
{
int sumTotal = 0;
// Calculate total sum of the array
for (int num : arr)
{
sumTotal += num;
}
// Call recursive function to find
// the minimum difference
return findMinDifference(arr,
arr.size(), 0, sumTotal);
}
int main()
{
vector<int> arr = {1, 6, 11, 5};
cout << minDifference(arr) << endl;
return 0;
}
// Using Top-Down DP (Memoization) – O(n*sumTotal) Time and O(n*sumTotal) Space
// C++ Code to partition a set into two
// subsets such that the difference
// of subset sums is minimum, using memoization
#include <bits/stdc++.h>
using namespace std;
// Function to calculate the minimum absolute
// difference with memoization
int findMinDifference(vector<int>& arr, int n,
int sumCalculated, int sumTotal,
vector<vector<int>>& memo) {
// Base case: if we've considered all elements
if (n == 0) {
return abs((sumTotal - sumCalculated)
- sumCalculated);
}
// Check if the result is already computed
if (memo[n][sumCalculated] != -1) {
return memo[n][sumCalculated];
}
// Include the current element in the subset
int include = findMinDifference(arr, n - 1,
sumCalculated + arr[n - 1], sumTotal, memo);
// Exclude the current element from the subset
int exclude = findMinDifference(arr, n - 1,
sumCalculated, sumTotal, memo);
// Store the result in memo and return
return memo[n][sumCalculated] = min(include, exclude);
}
// Function to get the minimum difference
int minDifference(vector<int>& arr) {
int sumTotal = 0;
// Calculate total sum of the array
for (int num : arr) {
sumTotal += num;
}
// Create a 2D memoization table, initialized to -1
vector<vector<int>> memo(arr.size() + 1,
vector<int>(sumTotal + 1, -1));
// Call the recursive function with memoization
return findMinDifference(arr, arr.size(),
0, sumTotal, memo);
}
int main() {
vector<int> arr = {1, 6, 11, 5};
cout << minDifference(arr) << endl;
return 0;
}
// Using Space Optimized DP – O(n*sumTotal) Time and O(sumTotal) Space
// C++ code to partition a set into two subsets
// with min diff with space optimization
#include <bits/stdc++.h>
using namespace std;
// Function to get the minimum difference using
// space optimization
int minDifference(vector<int>& arr) {
int sumTotal = 0;
// Calculate total sum of the array
for (int num : arr) {
sumTotal += num;
}
// Create a 1D DP array to track achievable subset sums
vector<bool> dp(sumTotal + 1, false);
dp[0] = true;
// Fill the DP array
for (int num : arr) {
for (int sum = sumTotal; sum >= num; sum--) {
dp[sum] = dp[sum] || dp[sum - num];
}
}
// Find the minimum difference
int minDiff = sumTotal;
for (int sum = 0; sum <= sumTotal / 2; sum++) {
if (dp[sum]) {
minDiff = min(minDiff, abs((sumTotal - sum) - sum));
}
}
return minDiff;
}
int main() {
vector<int> arr = {1, 6, 11, 5};
cout << minDifference(arr) << endl;
return 0;
}