Have the function ScaleBalancing(int vector, int vector) read vectors which will contain two elements, the first being the two positive integer weights on a balance scale (left and right sides) and the second element being a list of available weights as positive integers. Your goal is to determine if you can balance the scale by using the least amount of weights from the list, but using at most only 2 weights.
For example: if vector is ["[5, 9]", "[1, 2, 6, 7]"] then this means there is a balance scale with a weight of 5 on the left side and 9 on the right side. It is in fact possible to balance this scale by adding a 6 to the left side from the list of weights and adding a 2 to the right side. Both scales will now equal 11 and they are perfectly balanced. Your program should return a comma separated string of the weights that were used from the list in ascending order, so for this example your program should return the string 2,6
There will only ever be one unique solution and the list of available weights will not be empty. It is also possible to add two weights to only one side of the scale to balance it. If it is not possible to balance the scale then your program should return the string not possible.
How can I make improvements to my code? I also want to know if my approach was a good way to solve this problem.
#include <algorithm>
#include <functional>
#include <iostream>
#include <numeric>
#include <vector>
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;
}
bool every_Num_Positive(std::vector<int> &integerWeights, std::vector<int> &availableWeights)
{
if (all_Positive(integerWeights.begin(),integerWeights.end()) && all_Positive(availableWeights.begin(), availableWeights.end()))
{
return true;
}
return false;
}
bool check_Length(std::vector<int> &integerWeights, std::vector<int> &availableWeights)
{
if (integerWeights.size() == 2 && !availableWeights.empty())
{
return true;
}
return false;
}
std::vector<int> find_2_Nums_That_Add_To_Difference(std::vector<int> &availableWeights, int difference)
{
std::vector<int> result;
for (std::size_t i = 0; i < availableWeights.size(); ++i)
{
for (std::size_t j = i + 1; j < availableWeights.size(); ++j)
{
if (availableWeights.at(i) + availableWeights.at(j) == difference)
{
result.push_back(availableWeights.at(i));
result.push_back(availableWeights.at(j));
}
}
}
return result;
}
std::string find_2_Nums_That_Minus_To_Difference(std::vector<int> availableWeights, int difference)
{
std::vector<int> possible;
for (std::size_t i = 0; i < availableWeights.size(); ++i)
{
for (std::size_t j = i + 1; j < availableWeights.size(); ++j)
{
if (abs(availableWeights.at(i) - availableWeights.at(j)) == difference)
{
return std::to_string(availableWeights.at(i)) + "," + std::to_string(availableWeights.at(j));
}
}
}
return "NOT POSSIBLE";
}
std::string scale_Balancing(std::vector<int> integerWeights, std::vector<int> availableWeights)
{
if (check_Length(integerWeights, availableWeights) && every_Num_Positive(integerWeights,availableWeights))
{
int difference = std::abs(integerWeights.at(1) - integerWeights.at(0));
std::vector<int> possibleResults = find_2_Nums_That_Add_To_Difference(availableWeights, difference);
if (std::find(availableWeights.begin(),availableWeights.end(),difference) != availableWeights.end())
{
return std::to_string(difference);
}
else if (!possibleResults.empty())
{
return std::to_string(possibleResults.at(0)) + "," + std::to_string(possibleResults.at(1));
}
else
{
return find_2_Nums_That_Minus_To_Difference(availableWeights, difference);
}
}
return "NOT POSSIBLE";
}