I was looking for some tips regarding vector iteration. I am trying to fulfill a challenge, in which you have to implement a function (SumOfTwo) that takes two vectors and a sum as arguments and has to check if there is any pair of numbers in the two vectors that if added can give the sum that is passed as an argument.
My function works fine, except that it doesn't cover the time limitation which is 500ms. i feel that there is a better way to iterate through two vectors than two for loops, but I feel very stuck...
bool sumOfTwo(std::vector<int> a, std::vector<int> b, int v) {
if((a.empty()) || b.empty()){
return false;
}
for (std::vector<int>::iterator it1 = a.begin(); it1<=a.end(); ++it1) {
for (std::vector<int>::iterator it2 = b.begin(); it2<=b.end(); ++it2) {
if ((*it1 + *it2) == v){
return true;
}
}
}
return false;
}