Topic:
The array a consists of 10 million random positive integers (int), the design algorithm, given the integer n, finds A and b in a, so that it conforms to the following equation:
n = a + b
Problem Solving Ideas:
1.1000w of random positive integer occupy space is probably 38-40mb, not very large, but still need to consider if the order of magnitude continues to increase the situation. It is best to find a way to not load the array into memory.
2. If n is given, then the number of the array greater than n is useless, but only those in the number between 0 and N, so 1000w numbers can actually be reduced to an array of length n, but n may be larger than 1000w. This is not a problem-solving idea.
3. Consider using a bit to record numeric values to reduce memory requirements: Each array is stored in bits, assuming that the array length (including n) is within the range of int (32-bit), then up to 2^31-1 = 2,147,483,647 bits, or memory 200M. (The 200M here only depends on the range of array elements, and the length of the array does not have any relationship, this is one of the shortcomings of the bit set, if the data is sparse, and each element of the value range is relatively large, the space occupied by the bit set will be a serious waste).
4. Create a bit set of size n, traverse array A, and if element a appears, set the bit a to 1.
5. Starting from the No. 0 bit, if both the first and the n-i bits are 1, this shows that both have appeared, and the sum is N, meet the conditions, the algorithm ends!
Algorithm title: Shaping arrays to find A and b make a+b=n