Evaluate any two numbers in the array and given values

Source: Internet
Author: User

 

 

Question:

Enter an array and a number that have been sorted in ascending order and search for two numbers in the array so that their sum is exactly the number entered. The time complexity is O (n ). If there are multiple pairs of numbers and the sum is equal to the input number, output any one.

For example, input arrays 1, 2, 4, 7, 11, 15, and numbers 15. Because 4 + 11 = 15, 4 and 11 are output.

Ideas:

The most direct approach is the brute force law. There are two for loops, with the time complexity of O (n * n). However, this does not take full advantage of the ascending array. Assume that the array is A, the length is len, and the given sum is sum. The best way is to add the first number of arrays A [low] and the last number A [high] first, check whether it is equal to sum. If it is equal to sum, a group of numbers is found, and true is returned. If it is greater than sum, a large number is moved forward, that is, high --, at this time, it becomes the sum of the first and the last and second numbers. If it is smaller than sum, it will move a smaller number backward, that is, low ++, in this case, the second and last number are added, and so on. If a set of values for sum is not found when low = high, false is returned. The time complexity of this algorithm is O (n), and the space complexity is O (1 ).

Some proofs are required for this method:

For an ascending series of A1, A2 ,... ak k> = 3, if A1 + Ak is greater than sum, then the number of K-1 pairs and A1 + Ak, A2 + Ak ,... ak-1 + Ak has sum

This method is suitable for any integer array.

Complete code

 

/*************************************** **************************************** * ********************* Questions: enter an array and a number that have been sorted in ascending order and search for two numbers in the array so that their sum is exactly the number entered. The time complexity is O (n ). If there are multiple pairs of numbers and the sum is equal to the input number, output any one. For example, input arrays 1, 2, 4, 7, 11, 15, and numbers 15. Because 4 + 11 = 15, 4 and 11 are output. **************************************** **************************************** * ********************/# Include
 
  
/* Find any two elements of sum in the ascending array A and save them in a and B */bool FindTwoNumSum (int * A, int len, int sum, int *, int * B) {if (A = NULL | len <2) return false; int low = 0; int high = len-1; while (low
  
   Test ResultsLet's expand it now. Assume that the array is out of order, and that all elements in the array are non-negative integers. Similarly, given a number sum, we can find any two numbers in the array, make the sum of the two. Because the elements in the array are defined as non-negative integers, we can use a hash array to open up a bool array B [sum] with the length of sum, and initialize all the elements to false, array A is traversed once. If the current element A [I] is greater than sum, it is skipped directly. Otherwise, the following judgment is continued. If B [A [I] is false, set B [sum-A [I] to true, so that if B [A [I] is true, there are two numbers that meet the condition, A [I] and sum-A [I]. If no elements with B [A [I] being true are found after traversing A, it means that the elements meeting the condition cannot be found. The time complexity of this algorithm is O (n), but the space complexity is O (sum ). Or, if you know that the maximum value of the non-negative integer array A is MAX, you can also open up A bool array of MAX size. The idea is the same. The complete code is as follows:
   
/*************************************** **************************************** * ********************* Questions: enter an unordered non-negative integer array and a number, and search for two numbers in the array so that their sum is exactly the number entered. The time complexity is O (n ). If there are multiple pairs of numbers and the sum is equal to the input number, output any one. For example, input arrays 1, 7, 4, 11, 6, 15, and numbers 15. Because 4 + 11 = 15, 4 and 11 are output. **************************************** **************************************** * ********************/# Include
    
     
# Include
     
      
/* Find any two elements of sum in the unordered array A and store them in a and B */bool FindTwoNumSum (int * A, int len, int sum, int *, int * B) {if (A = NULL | len <2) return false; // bool array bool * B = (bool *) whose elements are initialized to false *) calloc (sum, sizeof (bool); if (B = NULL) exit (EXIT_FAILURE); int I; for (I = 0; I
      
       
Sum) break; if (B [A [I] = false) B [sum-A [I] = true; else {* a = A [I]; * B = sum-A [I]; free (B); B = NULL; return true ;}} free (B); B = NULL; return false ;} int main () {int A [] = {19,3, 9,7, 12,20, 17,18,}; int len = 10; int sum = 24; int a, B; if (FindTwoNumSum (A, len, sum, & a, & B) printf (Find two nums, they are: % d and % d, a, B ); elseprintf (Not find); return 0 ;}
      
     
    
Test results:

 

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.