Hi I am going over Gayle Laakmann's CTIC book and I came across her question
print all possible integer solutions for a^3 + b^3 = c^3 + d^3 where a,b,c,d can be integers 1 - 1000
She gave some pseudo code which looks like this:
First solution
n = 1000
for c from 1 to n
for d from 1 to n
result = c^3 + d^3
append(c,d) to list at value map[result]
for a from 1 to n
for b from 1 to n
result = a^3 + b^3
list = map.get(result)
for each pair in list
print a, b, pair
Second Solution
n = 1000
for c from 1 to n
for d from 1 to n
result = c^3 + d^3
append (c,d) to list at value map[result]
for each result, list in map
for each pair1 in list
for each pair2 in list
print pair1, pair2
How do any of these solutions get an O(n^2) running time? Even in the first solution the second set of for loops you also iterate over the pairs in your map so wouldn't that be O(n^3)?