From c5b6d774ff5ac31c3ebf96e62d680c6456042d99 Mon Sep 17 00:00:00 2001 From: paurush batish Date: Wed, 7 Apr 2021 14:52:35 +0530 Subject: [PATCH] Edge cases to count number of zeros. if the given array is -- [0,o,1,I,2,3] (o,I for visible change) the diff = 1 result = 1 - {0,1,2}, {o,I,3} 2 - {o,1,2}, {0,I,3} 3 - {0,o,1,2}, {I,3} 4 - {1,2} {0,o,I,3} 5- {0,I,2}, {o,1,3} 6- {o,I,2}, {0,1,3} 7 -{o,I,2}, {0,1,3} 8 -{0,o,I,2}, {1,3} 9 -{ I,2} , { 0,o,1,3} 10 -{1,I,2} ,{3,0,o} 11 -{1,I,2,o} ,{3,0} 12 - {1,I,2,0} ,{3,o} 13 - {1,I,2,0,o} ,{3} --- count_of_subset_with_given_diff.cpp | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) 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 +}