Given a list of integers, how can all pairs (ie. a x b = c x d; such that a != b != c != e) be found under minimal time complexity?
I've tried using a hashmap data type, which basically does the product calculation, checks whether it's already found within a hashmap, and if it is it attributes the value of the increment counter of the nested for loops with the Pair object type found within the hashmap.
Pair is an object storing the index of the first and second numbers in a pair. The hashmap stores the product as the key and the pair as the value.
The problem with my code is that when it comes to the following scenario...
a x b = c x d = e x f
...it doesn't work due to the fact it only makes the following links...
a x b = c x d and a x b = e x f
...and is unable to reach:
c x d = e x f
For example the following array produces incorrect results:
int[] A = {1,2,3,4,6,12};
I expect the sole problem is because a hashmap only takes one value for a given key. I've tried to maybe change the hashmap declaration to an array of pairs but quickly realised I would be needing to add another for loop, and thus increasing the time complexity.
Any ideas what I can do to maintain the O(n²) and provide correct results?