Skip to content

Commit e93a132

Browse files
authored
O(n*sum)
1 parent d8536ec commit e93a132

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

Diff for: src/Algorithms/Dynamic Programming/07 Minimum subset sum difference.cpp

+5-5
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ vector<int> isSubsetPoss(int arr[], int n, int sum) {
77
for (int i = 0; i <= n; i++) {
88
for (int j = 0; j <= sum; j++) {
99
if (i == 0)
10-
t[i][j] = false;
10+
t[i][j] = false;
1111
if (j == 0)
1212
t[i][j] = true;
1313
}
@@ -23,9 +23,9 @@ vector<int> isSubsetPoss(int arr[], int n, int sum) {
2323
}
2424

2525
vector<int> v; // contains all subset sums possible with n elements // creating a vector varible to store all the element of the last row
26-
for (int j = 0; j <= sum; j++)
27-
if (t[n][j] == true)
28-
v.push_back(j);
26+
for (int j = 0; j <= sum; j++) // from the range we need to exclude the element which is not there in the existing problem
27+
if (t[n][j] == true) // keep true to only those place whose subset sum exist
28+
v.push_back(j); // store in the vector
2929

3030
return v;
3131
}
@@ -37,7 +37,7 @@ int MinSubsetSumDiff(int arr[], int n) {
3737

3838
vector<int> v = isSubsetPoss(arr, n, range);
3939
int mn = INT_MAX;
40-
for (int i = 0; i < v.size(); i++)
40+
for (int i = 0; i < v.size(); i++) // iterating through the last row of the matrix
4141
mn = min(mn, abs(range - 2 * v[i])); // taking minimum from the last row
4242

4343
return mn;

0 commit comments

Comments
 (0)