Monday, September 17, 2018

horse found

  • Problem Description

    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

  • CODING ARENA
  • #include<stdio.h>
    #include<stdlib.h>
    int main()
    {
       int t,i,n,j,min=0,p;
       scanf("%d",&t);
       while(t--)
       {
        int a[5000];
       scanf("%d",&n);
    for(i=0;i<n;i++)
      scanf("%d",&a[i]);
       min=abs(a[0]-a[1]);
       for(i=0;i<n-1;i++)
       {
       for(j=i+1;j<n;j++)
       {
           p=abs(a[i]-a[j]);
          if(min>p)
           min=p;
       }
       }
       printf("%d\n",min);
       }
      return 0;
    }
  • Test Case 1

    Input (stdin)
    1
    
    5
    
    4 9 1 32 13
    
    
    Expected Output
    3
  • Test Case 2

    Input (stdin)
    1
    
    4
    
    5
    
    11 20 22 4
    
    
    Expected Output
    2

No comments:

Post a Comment