c++ - Solving the array sum problem using iterators and testing for equality only -
Getting ready for the interview, I have the classic code "if there is an array that has two elements in Yoga It is decided to find out the number of questions ", using iterator logic so that containers other than the vector
can be normalized.
Here is my function here
// Find the container given for two elements with the given yoga. // If there are two such elements, then return to the truth and point to // Errators // Elements. Bool hasElementSum (integer sum, constant vector & lt; integer & gt; & amp; ve, vector & lt; integer & gt; :: iterator & amp; EL1, vector & lt; integer & gt; :: iterator & amp; El2) {bool retired = false; El1 = v.begin (); L2 = V. and () - 1; While (! EL1 = el2) {if (* EL1 + * el2 == yoga) return true; ++ EL1; - el2; } return false; }
This, of course, does not work, but I can not understand it while using the condition without using (EL1 & gt; = El2)
. Various sources I looked for advice against omnly similarity iterators, using the container that enable iterators to be able to normalize all types of support.
Thank you!
If the input is not sorted, then the reply to @ SBI is as good as it is.
With sorted, random-access input, you can start with the first element, and see binary search (or launch search etc.) whether you can find that value You should try with the desired amount then you can try the second element, but when you do a binary search (or whatever) then use the result as the upper limit from previous search since your first element is larger than the previous one Have , The value matching to produce the correct sum must be equal to or equal to the time that you've found in the last time.
Comments
Post a Comment