std::string scale_Balancing(std::vector<int>& integerWeights, std::vector<int>& availableWeights) { int difference = std::abs(integerWeights[1] - integerWeights[0]); sort(availableWeights.begin(), availableWeights.end()); if (std::binary_search(availableWeights.begin(), availableWeights.end(), difference)) { return std::to_string(difference); } else if ( auto sum_pair = find_pair_with_sum(availableWeights, difference); if (sum_pair) { return std::to_string(sum_pair->first) + "," + std::to_string(sum_pair->second); } else if ( auto diff_pair = find_pair_with_difference(availableWeights, difference); if (diff_pair) { return std::to_string(sum_pair->first) + "," + std::to_string(sum_pair->second); } else { return "NOT POSSIBLE"; } } std::string scale_Balancing(std::vector<int>& integerWeights, std::vector<int>& availableWeights) { int difference = std::abs(integerWeights[1] - integerWeights[0]); sort(availableWeights.begin(), availableWeights.end()); if (std::binary_search(availableWeights.begin(), availableWeights.end(), difference)) { return std::to_string(difference); } else if (auto sum_pair = find_pair_with_sum(availableWeights, difference)) { return std::to_string(sum_pair->first) + "," + std::to_string(sum_pair->second); } else if (auto diff_pair = find_pair_with_difference(availableWeights, difference)) { return std::to_string(sum_pair->first) + "," + std::to_string(sum_pair->second); } else { return "NOT POSSIBLE"; } } std::string scale_Balancing(std::vector<int>& integerWeights, std::vector<int>& availableWeights) { int difference = std::abs(integerWeights[1] - integerWeights[0]); sort(availableWeights.begin(), availableWeights.end()); if (std::binary_search(availableWeights.begin(), availableWeights.end(), difference)) { return std::to_string(difference); } auto sum_pair = find_pair_with_sum(availableWeights, difference); if (sum_pair) { return std::to_string(sum_pair->first) + "," + std::to_string(sum_pair->second); } auto diff_pair = find_pair_with_difference(availableWeights, difference); if (diff_pair) { return std::to_string(sum_pair->first) + "," + std::to_string(sum_pair->second); } return "NOT POSSIBLE"; } take the vectors as values, you probably want to pass the vectorsthem by as referencesreference to avoid making a copycopies.
for that purpose. If C++17 is not available then returning a std::pair<int, int> (with { -1, -1 } indicating “not found”) would be an alternative to a returningreturning a vector.
Finally noteNote that both functions can be implemented with \$ O(n) \$ complexity if the array is sorted, see for example
With those changes, the main function simplifies to
std::string scale_Balancing(std::vector<int>& integerWeights, std::vector<int>& availableWeights) { int difference = std::abs(integerWeights[1] - integerWeights[0]); sort(availableWeights.begin(), availableWeights.end()); if (std::binary_search(availableWeights.begin(), availableWeights.end(), difference)) { return std::to_string(difference); } else if (auto sum_pair = find_pair_with_sum(availableWeights, difference)) { return std::to_string(sum_pair->first) + "," + std::to_string(sum_pair->second); } else if (auto diff_pair = find_pair_with_difference(availableWeights, difference)) { return std::to_string(sum_pair->first) + "," + std::to_string(sum_pair->second); } else { return "NOT POSSIBLE"; } } take the vectors as values, you probably want to pass the vectors as references to avoid making a copy.
for that purpose. If C++17 is not available then returning a std::pair<int, int> (with { -1, -1 } indicating “not found”) would be an alternative to a returning a vector.
Finally note that both functions can be implemented with \$ O(n) \$ complexity if the array is sorted, see for example
take the vectors as values, you probably want to pass them by reference to avoid making copies.
for that purpose. If C++17 is not available then returning a std::pair<int, int> (with { -1, -1 } indicating “not found”) would be an alternative to returning a vector.
Note that both functions can be implemented with \$ O(n) \$ complexity if the array is sorted, see for example
With those changes, the main function simplifies to
std::string scale_Balancing(std::vector<int>& integerWeights, std::vector<int>& availableWeights) { int difference = std::abs(integerWeights[1] - integerWeights[0]); sort(availableWeights.begin(), availableWeights.end()); if (std::binary_search(availableWeights.begin(), availableWeights.end(), difference)) { return std::to_string(difference); } else if (auto sum_pair = find_pair_with_sum(availableWeights, difference)) { return std::to_string(sum_pair->first) + "," + std::to_string(sum_pair->second); } else if (auto diff_pair = find_pair_with_difference(availableWeights, difference)) { return std::to_string(sum_pair->first) + "," + std::to_string(sum_pair->second); } else { return "NOT POSSIBLE"; } } Unnecessary includes: These
#include <algorithm> #include <functional> #include <numeric> are apparently not needed.
In your function
template <typename T> bool all_Positive(const T start, const T end) { T it; for (it = start; it != end; it++) { if (*it < 0) return false; } return true; } the test should be
if (*it <= 0) return false; otherwise zero would qualify as positive number. As @tinstaafl already said, verifying the input is usually not necessary for programming challenges.
The functions
std::string find_2_Nums_That_Minus_To_Difference(std::vector<int> availableWeights, int difference) std::string scale_Balancing(std::vector<int> integerWeights, std::vector<int> availableWeights) take the vectors as values, you probably want to pass the vectors as references to avoid making a copy.
In scale_Balancing() a possible pair of weights adding to the difference is determined (and possibly discarded) before searching for a single weight.
It is also confusing that
std::vector<int> find_2_Nums_That_Add_To_Difference(...) returns a vector, but
std::string find_2_Nums_That_Minus_To_Difference(...) returns a string.
Both functions actually return a pair of integers or “nothing.” In C++17 you can use a
std::optional<std::pair<int, int>> for that purpose. If C++17 is not available then returning a std::pair<int, int> (with { -1, -1 } indicating “not found”) would be an alternative to a returning a vector.
Better (and slightly shorter) function names would be
find_pair_with_sum(std::vector<int>&nums, int sum) find_pair_with_difference(std::vector<int>&nums, int difference) Finally note that both functions can be implemented with \$ O(n) \$ complexity if the array is sorted, see for example
Unnecessary includes: These
#include <algorithm> #include <functional> #include <numeric> are apparently not needed.
In your function
template <typename T> bool all_Positive(const T start, const T end) { T it; for (it = start; it != end; it++) { if (*it < 0) return false; } return true; } the test should be
if (*it <= 0) return false; otherwise zero would qualify as positive number. As @tinstaafl already said, verifying the input is usually not necessary for programming challenges.
The functions
std::string find_2_Nums_That_Minus_To_Difference(std::vector<int> availableWeights, int difference) std::string scale_Balancing(std::vector<int> integerWeights, std::vector<int> availableWeights) take the vectors as values, you probably want to pass the vectors as references to avoid making a copy.
In scale_Balancing() a possible pair of weights adding to the difference is determined (and possibly discarded) before searching for a single weight.
It is also confusing that
std::vector<int> find_2_Nums_That_Add_To_Difference(...) returns a vector, but
std::string find_2_Nums_That_Minus_To_Difference(...) returns a string.
Both functions actually return a pair of integers or “nothing.” In C++17 you can use a
std::optional<std::pair<int, int>> for that purpose. If C++17 is not available then returning a std::pair<int, int> (with { -1, -1 } indicating “not found”) would be an alternative to a returning a vector.
Better (and slightly shorter) function names would be
find_pair_with_sum(std::vector<int>&nums, int sum) find_pair_with_difference(std::vector<int>&nums, int difference) Finally note that both functions can be implemented with \$ O(n) \$ complexity, see for example
Unnecessary includes: These
#include <algorithm> #include <functional> #include <numeric> are apparently not needed.
In your function
template <typename T> bool all_Positive(const T start, const T end) { T it; for (it = start; it != end; it++) { if (*it < 0) return false; } return true; } the test should be
if (*it <= 0) return false; otherwise zero would qualify as positive number. As @tinstaafl already said, verifying the input is usually not necessary for programming challenges.
The functions
std::string find_2_Nums_That_Minus_To_Difference(std::vector<int> availableWeights, int difference) std::string scale_Balancing(std::vector<int> integerWeights, std::vector<int> availableWeights) take the vectors as values, you probably want to pass the vectors as references to avoid making a copy.
In scale_Balancing() a possible pair of weights adding to the difference is determined (and possibly discarded) before searching for a single weight.
It is also confusing that
std::vector<int> find_2_Nums_That_Add_To_Difference(...) returns a vector, but
std::string find_2_Nums_That_Minus_To_Difference(...) returns a string.
Both functions actually return a pair of integers or “nothing.” In C++17 you can use a
std::optional<std::pair<int, int>> for that purpose. If C++17 is not available then returning a std::pair<int, int> (with { -1, -1 } indicating “not found”) would be an alternative to a returning a vector.
Better (and slightly shorter) function names would be
find_pair_with_sum(std::vector<int>&nums, int sum) find_pair_with_difference(std::vector<int>&nums, int difference) Finally note that both functions can be implemented with \$ O(n) \$ complexity if the array is sorted, see for example