Marks :20
: 15 | : 0
Barry works as a chemist in a science lab , Barry is working on new experiment. In the experiment he prepares a list of chemical elements, each element having its own chemical reaction power .He needs to select two elements and reacting the two elements together result of this reaction is subtraction of the chemical power of the second element from the first element, Barry wants the result to be as close as possible to a value Barry had decided before the experiment(in absolute difference).
The problem here is that Barry need to repeat it many times for different pairs of elements to get the closest pair to the desired result, each reaction costs time and materials, so Barry needs to select the right elements before the reaction. If there are many options, he selects the one that has summation of chemical power maximum.
Given decided result or desired result before the experiment and list of chemical reaction power, Help Barry to determine summation of chemical reaction power of the two elements Barry should select.
The first line contains an integer , $$$0\le$$$desired_result$$$\le 10^{5}$$$ denoting the desired result or decided result.
The second line contains an integer $$$ 2\le n \le 10^{5}$$$ denoting number of elements in the $$$powers$$$ list.
Each line $$$l_i$$$ of $$$n$$$ subsequent lines $$$where,0<=i<n$$$ contains an integer describing $$$0\le powers_i \le 10^{5}$$$
Print the summation of the chemical reaction power of two elements Barry should select.
4 5 2 1 0 7 2
9
5 8 2 1 4 2 3 2 2 6
7
In the first sample $$$n=5$$$ and desired_result=4 and list of $$$power$$$ of elements is $$$power[2, 1, 0, 7, 2]$$$. the two selected powers are $$$(7,2)$$$ result=$$$7-2=5$$$
and difference between subtraction and desired result is $$$1$$$ . It can be shown that this difference can be at most 1 and maximum sum corresponding to the given minimum difference cannot exceed $$$7+2=9$$$.Hence answer is 9.
You need to login to view your submissions.
You need to login to view all submissions.
Result : Executed
Feel something is wrong with the test cases?
Result : Accepted
Test Cases :
But to Run or Submit the Problem, you need to Log In.
Continue to Log InYour challenge has been submitted successfully.
You will get a response soon via WhatsApp or Email.
Do let us know your issue.