diff --git a/count_of_subset_with_given_diff.cpp b/count_of_subset_with_given_diff.cpp index 00f1655..d56cab2 100644 --- a/count_of_subset_with_given_diff.cpp +++ b/count_of_subset_with_given_diff.cpp @@ -1,5 +1,3 @@ -// Count of Subsets with given Sum - #include using namespace std; @@ -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() { @@ -47,4 +50,4 @@ signed main() { cout << CountSubsetsWithDiff(arr, n, diff) << endl; return 0; -} \ No newline at end of file +}