[Sword refers to offer learning] the sum is the two numbers of a specified value, and the sword refers to offer.

Source: Internet
Author: User

[Sword refers to offer learning] the sum is the two numbers of a specified value, and the sword refers to offer.

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.







Excel programming solution, how to write the variable and the fixed value in the constraints, such as Σ Xi = 2

For example, if A1: A10 is the number to be changed, enter = 2-sum (A1: A10) in B1 and enter B1 = 0 as the constraint. This is generally the case.

In urgent EXCEL, how can I find a number in a group and a value equal to the expected number? Between 1 and 4 numbers

Added: I asked the person upstairs, but I still don't understand, Khan.

"Is the sum of several numbers equal to the expected value ?" :
Where is the expected value? Is it calculated by the landlord or by Excel? According to the question, it should be calculated in Excel. What are the calculation rules?

"Sum between 1-4 numbers"
For example, the sum of the first four digits is 947.09.
The sum of the last four digits is 413.61.
How do you deal with these numbers? How did we calculate 699.24 in the question?

No internet access.

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.