Answer the question
In order to leave comments, you need to log in
How to develop an algorithm to find two elements whose sum gives x?
Please help me solve this problem, I can't think of anything
Answer the question
In order to leave comments, you need to log in
First, order the set (array) S in ascending order.
Then we take two extreme elements, we consider their sum. Let's say the sum is more than necessary - then we discard the right (largest) element; and if less, then the leftmost one (well, we don’t discard it - but shift the pointer to the center of the array). And we repeat - until the desired amount is obtained or until the elements run out.
Fantastic way:
Learn O-notation.
Learn different algorithms with their complexity estimation in O-notation.
Select the necessary algorithms and carry out their composition in your own algorithm that solves the problem.
Real way:
Run through all the forums asking for help, maybe someone will respond.
PS The task, by the way, is primitive, in two steps, the first O(n log n), the second O(n).
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question