[Topic] enter an array in ascending order, a given number, and search for two numbers in the array so that their sum is equal to the given number. The time complexity is O (n). If there are multiple matching numeric pairs, only one pair is given. For example, if the input array "1, 2, 4, 7, 11, 15" is given a number of 15, the output should be "4, 11 』.
[Idea 1] We do not consider the time complexity as O (n), but consider it according to the most intuitive idea. It is easy to think that we fix a number in an array at a time, then, traverse the numbers after this number to determine whether or not the given sum is used. The time complexity is O (n2 ). The following code is easy to write:
1 #include<iostream>
2 #include<string>
3 using namespace std;
4
5 bool FindTwoNumbers(int array[],int length,int sum,int &num1,int &num2)
6 {
7 if(array == NULL || length < 2)
8 return false;
9
10 for(int i = 0;i < length - 1;++i)
11 {
12 for(int j = i + 1;j < length;++j)
13 {
14 if(array[i] + array[j] == sum)
15 {
16 num1 = array[i];
17 num2 = array[j];
18 return true;
19 }
20 }
21 }
22
23 return false;
24 }
25
26 int main()
27 {
28 cout<<"Enter the length of your array:"<<endl;
29 int arraylength = 0;
30 cin>>arraylength;
31 int *p = new int[arraylength];
32
33 cout<<"Enter the numbers in your array:"<<endl;
34 for(int i = 0;i < arraylength;++i)
35 {
36 cin>>p[i];
37 }
38
39 cout<<"Enter a sum you want to find:"<<endl;
40 int result = 0;
41 cin>>result;
42
43 int a = 0;
44 int b = 0;
45 FindTwoNumbers(p,arraylength,result,a,b);
46
47 cout<<"the two numbers are:"
48 <<"\t"<<a<<"\t"<<b<<endl;
49
50 delete[] p;
51 return 0;
52 }
The running result is as follows:
[Idea 2] the time complexity of the above algorithm is O (n2), but this algorithm has the advantage that it can also be applied to unordered arrays, because we use traversal, we do not care about sorting. This also illustrates the reason for the high time complexity of the above algorithm from one side: we did not take full advantage of the fact that arrays are arranged in ascending order. What should we do if we consider this condition?
We consider a special case where an array is a strictly same number of numbers. Given a sum, if an integer can be found to meet the problem conditions, then according to the nature of the same number of numbers, if you want to move a large number to the left, that is, to move a small number to a number, you can find another matching number pair. In general, can we consider this: first, judge the sum of the first and last numbers. If the sum is greater than the given sum, then shift the last number to the left. Otherwise, if their sum is smaller than the given sum, it is logical to shift the first number to the right. However, this algorithm can easily give rise to a question. Will it be missed? This requires a strict mathematical proof. I cannot give proof for the time being. I hope that students who are very good at mathematics will give us some advice later. The code for this idea is as follows:
1 # include <iostream>
2 # include <string>
3 using namespace std;
4 bool FindTwoNumbers (int array [], int length, int sum, int & num1, int & num2)
5 {
6 // invalid input. false is returned.
7 if (array = NULL | length <2)
8 return false;
9
10 int * start = & array [0];
11 int * end = & array [length-1];
12
13 while (start <end)
14 {
15 if (* start + * end) = sum)
16 {
17 num1 = * start;
18 num2 = * end;
19 return true;
20}
21
22 // less than the given and smaller values move right
23 else if (* start + * end) <sum)
24 {
25 start ++;
26}
27
28 // greater than the given and greater numbers move left
29 else if (* start + * end)> sum)
30 {
31 end --;
32}
33}
34
35 return false;
36}
37
38
39 int main ()
40 {
41 cout <"Enter the length of your array:" <endl;
42 int arraylength = 0;
43 cin> arraylength;
44 int * p = new int [arraylength];
45
46 cout <"Enter the numbers in your array:" <endl;
47 for (int I = 0; I <arraylength; ++ I)
48 {
49 cin> p [I];
50}
51
52 cout <"Enter a sum you want to find:" <endl;
53 int result = 0;
54 cin> result;
55
56 int a = 0;
57 int B = 0;
58 FindTwoNumbers (p, arraylength, result, a, B );
59
60 cout <"the two numbers are :"
61 <"\ t" <a <"\ t" <B <endl;
62
63 delete [] p;
64 return 0;
65}
The running result is as follows:
References:
Programmer interview questions featured 100 questions: http://zhedahht.blog.163.com/blog/static/2541117420072143251809/
Note:
1) All the code environments of this blog are compiled in win7 + VC6. All code has been debugged by the blogger.
2) The blogger python27 has the copyright to this blog post. For the reposted website, please indicate the source at http://www.cnblogs.com/python27 /. You have any suggestions for solving the problem. Please feel free to comment on them.