this is the question im trying to solve :Write findTwoSumPair
, which takes in a vector of integers and a target sum, and returns a pair that represents two distinct indices of elements that sum up to the target value (with indexes sorted). There are no explicit time complexity constraints here (i.e. algorithm just needs to work as intended). Also make sure to handle empty input. This is my main
std::cout << "Q3" << std::endl;
std::vector<int> q3Input1{0, 2, 3, 4, 5};
std::pair<int, int> q3Out1 = findTwoSumPair(q3Input1, 6);
std::pair<int, int> q3Out2 = findTwoSumPair(q3Input1, 10);
std::cout << q3Out1.first << " " << q3Out1.second
<< std::endl; // should output 1 3
std::cout << q3Out2.first << " " << q3Out2.second
<< std::endl; // should output -1 -1
And this is the function causing me problems:
std::pair<int, int> findTwoSumPair(const std::vector<int>& vec, int targetSum) {
for (unsigned int i = 0; i < vec.size(); i++){
for(unsigned int j = i; i < vec.size();j++){
/*
if(targetSum == vec[i]+ vec[j]){
std::cout << vec[i] << vec[j];
}*/
}
}
return{vec[i],vec[j];
// throw std::logic_error("not implemented");
}
I was given the main.cpp so I'd like to not change it and there is libraries, the vector and the important parts to make it run it just only shows "Q3" for some reason. I commented out the if statement bc that was giving me the signal: aborted (core dumped) error. Also it's due tonight so any guidance done quickly is super appreciated
Aucun commentaire:
Enregistrer un commentaire