[Sword refers to offer learning] Two numbers whose sum is a fixed value

Source: Internet
Author: User
In the legend of the Condor Heroes, there are three swords used in the life of the 'swordsman dedicated to defeat: 'Yang once mentioned the first sword on the right, I saw two lines of small words engraved on the stone under the sword: 'fierce, invincible, the weak front of the championship with the heshuo group of forges'; the second is Xuan tie heavy sword: 'heavy sword without a front, big coincidence is not a work. When I was 40 years old, the world went viral. The third one was a wooden sword. The stone carvings under the sword: 'After forty years old, the grass, wood, bamboo, and stone can all be swords. Since then, I have refined my skills and gradually succeeded in winning the sword without a sword. '. The essence of the algorithm lies in this. Only by breaking through layers of realm can we make breakthroughs.


Description:


Enter an incremental sorting array and a number S, and search for two numbers in the array. Yes, their sum is exactly S. If there are multiple pairs of numbers and the sum is equal to S, the product of the two numbers is the smallest.
Input:
Each test case contains two rows: the first row contains an integer N and K, N indicates the number of elements in the array, and K indicates the sum of the two. Where 1 <= n <= 10 ^ 6, k is the second line of int contains N integers, each array is of the int type.
Output:
For each test case, two numbers are output first. If no value is found, "-1-1" is output"
Sample input:
6 151 2 4 7 11 15
Sample output:
4 11

Idea: This question is very simple. The most direct method is the brute-force cracking method, which uses two for loops with the time complexity of O (N * n ), however, this method does not take full advantage of the ascending array, and the efficiency is extremely low.

We can use a method similar to binary search. Assume that the array is A, the length is Len, and the given sum is sum, the best way is to first add the first number of arrays A [low] and the last number A [High] to check whether it is equal to sum. If it is equal to sum, a group of numbers is found, returns true. If the value is greater than sum, a large number is moved forward by one digit, that is, high --. At this time, the first and last two digits are added. If the value is less than sum, then, move a small number to the back, that is, low ++. Then 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 ).


The implementation code is as follows:

<Span style = "font-size: 18px;"> // offer01.cpp: defines the entry point of the console application. // # Include "stdio. H "// find any two elements of sum in the ascending array A and save them in bool findnumsum (int * a, int Len, int sum, int * a, int * B) {if (a = NULL | Len <2 | A [0]> sum) return false; int low = 0; int high = len-1; while (low 


References:
Some proofs are required for this method:
This method is suitable for any integer array. In addition, to output a group with the smallest product, it is unnecessary to save all the results.

When a + B = C, AB <= (a + B) ^ 2/4, and only when a = B, AB gets the maximum value, the difference between the two is farther, the smaller the product.






[Sword refers to offer learning] Two numbers whose sum is a fixed value

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.