Determines whether there are two numbers in an ordered integer array, and the sum equals the given arbitrary integer

Source: Internet
Author: User

A few days ago in a C + + interview, the interviewer asked such a question, then the first answer is 1.

  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.
  2. 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
  3. 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.