My approach is quite simple. Just to take the input then sort the array and then display difference between first to elements of the array.

But I am not getting what’s wrong in this code.

Question : https://www.codechef.com/problems/HORSES

My approach is quite simple. Just to take the input then sort the array and then display difference between first to elements of the array.

But I am not getting what’s wrong in this code.

Question : https://www.codechef.com/problems/HORSES

1 Like

Taking the difference of first two elements in the sorted array is not going to work.

Take a test case where sorted array looks like

2 4 5 10

Now, your algorithm will give its output as 2 but the actual output is 1

i.e the difference between 4 and 5.

I think now you have got what to do…

Just take the minimum of all the differences of adjacent elements in the sorted array.

Thank you. I think the question was not clear to me.

1 Like