# | N | Answer --- | --------------------------- | ------- 0 | { 9, 2 } | 3: You'll add 9/2 = 4, but then you're done. 1 | { 8, 2 } | 3: Ditto. 2 | { 50 } | 1: There are no pairs of elements. 3 | { 1, 5, 8, 30, 15, 4 } | 11: You'll add 2, 3, 6, 7 and 10. 4 | { 1, 2, 4, 8, 16, 32, 64 } | 7: You're already done. 5 | { 6, 2, 18 } | 7: You'll add 1, 3, 4 and 9.
The constraints of this problem state that N only holds numbers between 1 and 1,000, and N.size() is ≤ 1,000. Since (a div b) is always going to be less than a, the numbers in N will always be between 1 and 1,000. That puts some constraints on input that we can leverage. In particular, if our solution is O(N.size()2), it will be fast enough.
Here's a solution that comes to mind -- keep two collections of numbers: those that you have processed (call it P -- it will be a vector), and the vector N. You can also maintain a vector, which we'll call T, that tells us in O(1) time whether a number is in v. It will contain 1,001 booleans, and T[i] is true if i is in N. It is false otherwise.
Now do the following:
Now, run through N with an integer i. When i = 0, n = 9. We want to run through P, but P is empty, so we're done with n -- we add it to P:
Next, i = 1 and n = 2. We run through P and there is one element: p = 9. Since p is bigger than n, we set k = (p div n) = 4. We check to see if T[4] = 0, which it does, so we set T[4] = 1 and add 4 to N:
At this point, we're done with P, so we add n to P:
Finally, we continue with i = 2 and n = 4. We run through P: When p = 9, k = 2, and since T[2] equals 1, we ignore k. Similarly, when p = 2, k also equals 2, and since T[2] equals 1, we ignore k again. At this point, we're done with P, so we add n to P:
Now, we're done with N, so we return the size of P, which is three. As you can see, when we're done, P and N will be identical, so we could simply return the size of N.
Now think about the choice of data structure for T. Because the values of v are limited to ≤ 1000, we can make T a vector of size 1,001, and checking to see whether a value is in N is O(1).
Suppose T were instead a set. Then checking would be O(log n), and our program would run in O(n2log n).
And finally, suppose that we didn't use T at all, but instead used N. Then checking would be O(n), and our program would be O(n3). As always, our choice of data structure is very important!