I have a New question !!!!! try to solve it !!!!
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.
Input:
First line of the input file contains a single integer T, the number of test cases.Every test case starts with a line containing the integer N.
The next line contains N space separated integers where the i-th integer is S[i].
Output:
For each test case, output a single line containing the minimum difference that is possible.Constraints:
1 ≤ T ≤ 10 2 ≤ N ≤ 5000 1 ≤ S[i] ≤ 1000000000
Example:
Input:1 5 4 9 1 32 13Output:
3Explanation: The minimum difference can be achieved if we pick horses with skills 1 and 4 for the race.
//Here is the Code cc to Arun Kumar Gupta!!!!
import java.util.*;
public class MinDistance
{
public static void main (String [] args)
{
Scanner sc = new Scanner(System.in);
int cases = sc.nextInt();
double min =1000000000 , min_next = 1000000000 , temp = 0 ;
int array_size = 0;
int finale[] = new int[cases];
double array[] ;
int tt =0;
int w =0 ;
//System.out.println(cases);
if(cases <= 10)
{
for(int i = 0 ; i<cases ; ++i)
{
array_size = sc.nextInt();
array = new double[array_size];
double diff = 1000000000;
if((array_size <= 5000) && (array_size >= 2))
{
for(int j = 0 ; j < array_size ; ++j)
{
array[j] = (double)sc.nextInt();
}
Arrays.sort(array);
for(int j =0 ; j < array_size-1 ; ++j)
{
//System.out.println("hi");
if (array[j] <= 1000000000 && array[j] > 0 )
{
//System.out.println("::::::");
//System.out.println(min +":"+ min_next);
if(diff > (array[j+1] -array[j]))
diff = array[j+1] -array[j];
//System.out.println(diff);
}
}
tt = (int)(diff);
//w =0;
//System.out.println(tt);
finale[w] = tt;
tt =0;
diff = 0;
//System.out.println(finale[w]);
//System.out.println(w);
++w;
}
}
for(int j = 0 ; j < cases ; ++j)
{
System.out.println(finale[j]);
}
}
}
}
No comments:
Post a Comment