I recently ran into this programming problem that apparently couldn't make the complexity less (my current code works in O (n ^ 2)).
In fact, I have four different lists (I use python btw) of integers, both positive and negative, for example, lists A, B, C, D. Now each of these lists has 1000 integers, and these integers from -25000 to 25000 inclusive. Now suppose that from each of these lists we select an integer, say, a, b, c, d. I would like the fastest way to find these a, b, c, d such that a + b = - (c + d).
My method currently relies on iterating through every possible combination of a, b, and c, d, before trying to find if an element in the set (a + b) exists in the set - (c + d). This, of course, is not practical, since it works O (n ^ 2) times, especially considering the large size of the list (1000).
Therefore, I was wondering if anyone could think of a more efficient way (preferably O (n log n) or less), if possible, encoded in python.
Sorry if this is pretty confusing. If you have any questions, please let me know, I will try to give more clarification.
EDIT:
This problem is part of a larger problem. The big problem is that if we have 4 sequences of numbers with no more than 1000 integers, say A, B, C, D, find a, b, c, d such that a + b + c + d = 0.
I asked this question since a + b + c + d = 0 means that a + b = - (c + d), which I thought would lead to the quickest solution to the problem. If anyone can think of even faster ways, please share it with me.
Thanks in advance!:)