samedi 3 janvier 2015

Algorithm: check whether the summation of n random numbers is equal to integer K?

We have n random numbers which is less than 1.0 and greater than 0 i.e. 0.0 < a_i < 1.0 .


How to check whether there exist a set S which contains elements of property a_i such that summation of all a_i is equal to a integer constant K where i=1,2,...,n ?


Note: set should contain n elements.


e.g.



  1. N = 4 and K = 2 then we can choose {0.5 ,0.5 ,0.5 ,0.5} or {0.25 ,0.75 ,0.5 ,0.5} since their sum is 2 so answer is yes .

  2. N = 4 and K = 4 then answer is no since we can't choose a_i such that their sum K = 4 .





Aucun commentaire:

Enregistrer un commentaire