I've taken [this][1] sample algorithm question. It is quite a simple problem to solve.. But it is the execution time of my code algorithm that bugs me. Here is my working code. #include <iostream> #include <vector> using namespace std; class TwoSum { public: static std::pair<int, int> findTwoSum(const std::vector<int>& list, int TargetSum) { for (int increment = 0; increment < list.size(); increment++) { for (int i = 0; i < list.size(); i++) { if (list.at(increment) + list.at(i) == TargetSum) { return make_pair(increment, i); } } } return make_pair(-1, -1); } }; int main(int argc, const char* argv[]) { vector<int> list = {1, 3, 5, 7, 9 }; int targetSum = 12; pair<int, int> indices = TwoSum::findTwoSum(list, targetSum); indices.first == -1 && indices.second == -1 ? cout << "No vector elements can be added to achieve the target sum" << endl : cout << "The target sum can be achieved by adding the vector element " << indices.first << " and " << indices.second; cout << endl; return 0; } It is guaranteed correct as I tested it with the website's compiler (or whatever you call it), they can test your code just by pasting and running it on their code editor. I tested this on my IDE as well, and worked fine. The problem is, my code failed the performance test when dealing with large number of vector elements as it took longer than the website's expectation. For me, the function named `findTwoSum()`is already straightforward. And I don't have any idea how to optimize this function.. Note that the test's focus here is the function itself and how fast it executes. Any modifications in function's return type and parameter is not allowed. As the website stated that you cannot modify the main function's contents implying that you cannot also change how the function is called. But I slightly modified the main function for you guys to know what the program is trying to achieve. I am looking forward for recommendation and possible optimization for my code. Many Thanks. [1]: http://www.testdome.com/Questions/Cpp/TwoSum/2910