Chef is very fond of horses. He enjoys watching them race. As expected, he has a stable full of horses. He, along with his friends, goes to his stable during the weekends to watch a few of these horses race. Chef wants his friends to enjoy the race and so he wants the race to be close.
This can happen only if the horses are comparable on their skill i.e. the difference in their skills is less.
There are N horses in the stable. The skill of the horse i is represented by an integer S[i].
The Chef needs to pick 2 horses for the race such that the difference in their skills is minimum. This way, he would be able to host a very interesting race. Your task is to help him do this and report the minimum difference that is possible between 2 horses in the race.
TEST CASE 1
INPUT
2
5
4 9 1 32 13
5
4 9 1 3 1
OUTPUT
3
0
TEST CASE 2
INPUT
5
6
5 3 7 66 44 23
3
33 98 25
4
34 27 89 33
5
4 0 2 120 456
7
9 4 3 19 25 36 35
OUTPUT
2
8
1
2
1
import java.io.*; import java.util.Scanner; public class temp{ public static void main(String[] args){ Scanner inp=new Scanner(System.in); System.out.print("Enter N number: "); int T=inp.nextInt(); for(int k=0;k<T;k++) { System.out.print("\n\nEnter no: of Horses: "); int N=inp.nextInt(); int S[]=new int[101]; System.out.print("Enter skills: "); for(int i=0;i<N;i++) { S[i]=inp.nextInt(); } for(int i=0;i<N;i++) { for(int j=0;j<N;j++) { if(S[i]<S[j]) { int t=S[i]; S[i]=S[j]; S[j]=t; } } } int diff=999; for(int i=0; i<N;i++) { for(int j=i+1;j<N;j++) { int diff1=S[j]-S[i]; if(diff1<diff) diff=diff1; } } System.out.println("Difference in Skills "+diff); } } }
OUTPUT:
Enter N number: 5
Enter no: of Horses: 6
Enter skills: 5 3 7 66 44 23
Difference in Skills 2
Enter no: of Horses: 3
Enter skills: 33 98 25
Difference in Skills 8
Enter no: of Horses: 4
Enter skills: 34 27 89 33
Difference in Skills 1
Enter no: of Horses: 5
Enter skills: 4 0 2 120 456
Difference in Skills 2
Enter no: of Horses: 7
Enter skills: 9 4 3 19 25 36 35
Difference in Skills 1