Here is my problem:
I have a large integer (somewhere between 0 and 2 ^ 32-1). Let me call this number X. I also have a list of integers that are currently unsorted. All of them are unique numbers, greater than 0 and less than X. Suppose that in this list there are a large number of items, say more than 100,000 items.
I need to find up to 3 numbers in this list (let them be called A, B and C) that make up to X. A, B and C must be inside the list, and they can be repeated (for example, if X is 4, I I can have A = 1, B = 1 and C = 2, although 1 will only be displayed once in the list).
There may be several solutions for A, B and C, but I just need to find one possible solution for each of the fastest ways.
I tried to create a for loop structure like this:
For A in itemlist:
For B in itemlist:
For C in itemlist:
if A + B + C == X:
exit("Done")
But since my list of integers contains more than 100,000 elements, this uses too much memory and takes too much time.
Is there any way to find a solution for A, B, and C without using crazy amount of memory or taking crazy time? Thanks in advance.
source
share