imagine you're in a coding interview where you are asked to solve in an example array [1,2,3,4,5] the sum of 9 with only two digits of the best possible algorithmic complexity, the following code is the one that was done in the interview: (and it is in which an error must be found, it must be put in the line of code that is the error or if there are several write (123) and if it is none error write (0): (in order)
CODE: 1. bool PairNums(const vector<int> & data, int sum) { 2. set<int>comp; 3. for (int value : data) { 4. if (comp.find(value) == comp.end) 5. return true; 6. comp.add(sum+ (value-sum)); 7. } 8. return false; }
This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try
refreshing the page, (b) enabling javascript if it is disabled on your browser and,
finally, (c)
loading the
non-javascript version of this page
. We're sorry about the hassle.
Assuming, that the code was supposed to be written in C++:
(line 4) end is a method not a field,
(line 6) the set has no method add , it has insert instead.
Besides:
I would add the namespace std into your code,
the variable value could be const .
Concerning the algorithm itself: