A few days ago in a C + + interview, the interviewer asked such a question, then the first answer is 1.
- Iterates through the elements in the array, and the two-number addition is compared to the given integer, which is available when the number of arrays and the number of integers are small.
- The sum of two is equal to any integer, there must be a number less than or equal to any integer 1/2, another number greater than or equal to any integer 1/2, then only need to judge the integer half, because the array is sorted, can be found by binary method
- Directly traverse the array of elements less than or equal to any integer, by binary lookup, in the array of ordered elements in the discontinuous, compared to Method 2 save a lot of operations
Determines whether there are two numbers in an ordered integer array, and the sum equals the given arbitrary integer