dimanche 1 février 2015

Smallest maximum difference between adjacent members of an array


How can I calculate the minimum possible difference between adjacent integer elements of an array.


For example:


We have the array:



11 14 14 9 16


Adjacent elements are 11 and 14, 14 and 14 and etc. But the catch is that 11 and 16, are also considered as adjacent. Otherwise, I could just sort the array.


The solution for this example is 5. We can't re-arrange the elements in a way that the smallest maximum difference between adjacent elements is less than 5, or:



16 11 14 9 14




Aucun commentaire:

Enregistrer un commentaire