Answer the question
In order to leave comments, you need to log in
Optimal algorithm for finding sum terms?
There are two similar tasks:
N = 10
[10, 5, 2, 3, 7, 5, -1]
^-----------^ 5 + 5 = 10
^--^ 3 + 7 = 10 <-- второй элемент встретился раньше,
чем в предыдущем случае,
поэтому берём эту пару
G = 6
M = 100 N = 110
[101, 103, 107, 109] <-- простые числа от M до N
^---------^ 107 - 101 = 6 <-- пара появилась раньше
^---------^ 109 - 103 = 6
1 и 2, 1 и 3, 1 и 4, ...
2 и 3, 2 и 4, 2 и 5, ...
и т.д.
Answer the question
In order to leave comments, you need to log in
Let's reformulate the problem a bit: find the first number i such that there is j from 0 to i-1, such that a[i] + a[j] = N.
In the second example, even the algorithm is not needed, since the numbers are in a row:
$g = 6;
$m = 100;
$n = 110;
$array = [100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110];
echo $array[0] . "-" . $array[$g - 1] . "=" . $g;
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question