Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions count_of_subset_with_given_diff.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// Count of Subsets with given Sum

#include <bits/stdc++.h>
using namespace std;

Expand Down Expand Up @@ -28,14 +26,19 @@ int CountSubsetsWithSum(int arr[], int n, int sum) {
}

int CountSubsetsWithDiff(int arr[], int n, int diff) {
int sumOfArray = 0;
for (int i = 0; i < n; i++)
int sumOfArray = 0, zerocount = 0;
for (int i = 0; i < n; i++){
sumOfArray += arr[i];
if(arr[i] == 0){
zerocount++;
}
}


if ((sumOfArray + diff) % 2 != 0)
return 0;
else
return CountSubsetsWithSum(arr, n, (sumOfArray + diff) / 2);
return pow(2,zerocount)*CountSubsetsWithSum(arr, n, (sumOfArray + diff) / 2);
}

signed main() {
Expand All @@ -47,4 +50,4 @@ signed main() {

cout << CountSubsetsWithDiff(arr, n, diff) << endl;
return 0;
}
}