I want to write a function bool possible(int sum, std::vector<int> &vec)
that checks for the possibility of summing the integers in vec to get sum without adding any number more than once.
e.g
vec = 140 80 90 180 70
sum = 300
Function returns true coz 140 + 90 + 70 == 300.
Can any one help with me how to go about this?
I wrote this but takes too long for large input.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
bool possible(int sum, vector<int> &vec)
{
do
{
int s = 0;
for (int i = 0; i < vec.size(); i++)
{
s += vec.at(i);
if (s == sum)
{
returntrue;
}
}
}while(next_permutation(vec.begin(), vec.end()));
returnfalse;
}
}