This commit is contained in:
Lajat5
2021-11-09 15:54:32 +00:00
parent a7550dedd9
commit 7267e8cc34

View File

@@ -53,11 +53,12 @@ namespace dp {
uint64_t sum = std::accumulate(arr.begin() , arr.end() , 0); // Calculate sum of all elements
if (sum % 2 != 0) return false; //if sum is odd, it cannot be divided into two equal sum
bool part[sum / 2 + 1];
std::vector<bool> part;
//bool part[sum / 2 + 1];
// Initialize the part array as 0
for (uint64_t it = 0; it <= sum / 2; ++it) {
part[it] = false;
part.push_back(false);
}
// Fill the partition table in bottom up manner
@@ -65,14 +66,15 @@ namespace dp {
// The element to be included in the sum cannot be greater than the sum
for (uint64_t it2 = sum / 2; it2 >= arr[it]; --it2) { // Check if sum - arr[i]
// ould be formed from a subset using elements before index i
if (part[it2 - arr[it]] == 1 || it2 == arr[it])
if (part[it2 - arr[it]] == 1 || it2 == arr[it]) {
part[it2] = true;
}
}
}
return part[sum / 2];
}
}
}
} // namespace partitionProblem
} // namespace dp
/*******************************************************************************
* @brief Self-test implementations
@@ -83,7 +85,6 @@ void test() {
uint64_t n = arr.size();
bool expected_result = true;
bool derived_result = dp::partitionProblem::findPartiion(arr, n);
std::cout << derived_result << std::endl;
std::cout << "1st test: ";
if(expected_result == derived_result) {
std::cout << "Passed!" << std::endl;
@@ -102,4 +103,4 @@ int main()
test(); // run self-test implementations
return 0;
}