Algorithm of 10487 - Closest Sums

Problem Description Link
Algorithm:
To solve this problem you can follow this technique
1. first you need input for n and n integer value
2. then generate all possible sum of n integers
3. then get input for m and m distinct integer query
4. for each query you need to search closest value from all possible sum
   N.B: if difference is minimum then this value is closest.

Example:

input:
5
3
12
17
33
34
3
1
51
30
where n=5
5 values are 3,12,17,33,34 so all possible sum are
(3+12), (3+17),(3+33),(3+34), (12+17),(12+33),(12+34),(17+33),(17+34),(33+34)
=15,20,36,37,29,45,46,50,51,67
and get m=3 they are 1, 51, 30
for 1 minimum difference(1-15) = 14 so closest = 15
for 51 minimum difference(51-51) = 0 so closest = 51
for 31 minimum difference(31-29) = 2 so closest = 29
image

No comments:

Post a Comment

Write your comment - Share Knowledge and Experience