Racing Horses | CodeChef Solution

Hello coders, today we are going to solve Racing Horses CodeChef Solution whose Problem Code is HORSES.

Racing Horses

Contents

Task

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 Format

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 Format

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

Sample Input

1
5
4 9 1 32 13

Sample Output

3

Explanation

The minimum difference can be achieved if we pick horses with skills 1 and 4 for the race.

Solution – Racing Horses

C++

#include <bits/stdc++.h>
using namespace std;

int main() {
	// your code goes here
	
	int t;
	cin>>t;
	
	while(t--){
	    int n;
	    cin>>n;
	    
	    int arr[n];
	    for(int i=0;i<n;++i){
	        cin>>arr[i];
	    }
	    
	    int min_diff=INT_MAX;
	   
	    sort(arr,arr+n);
	    
	    for(int i=1;i<n;++i){
	        int d=arr[i]-arr[i-1];
	        if(d<min_diff){
	            min_diff=d;
	        }
	    }
	    
	    cout<<min_diff<<endl;
	    
	}
	return 0;
}

Python

# cook your dish here
tests = int(input())

for _ in range(tests):
    N = int(input())
    l = [int(x) for x in input().split()]
    l.sort()

    min_g = l[-1] - l[0]
    for i in range(len(l)-1):
        min_l = l[i+1] - l[i]
        min_g = min(min_g, min_l)
    print(min_g)

Java

/* package codechef; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Codechef
{
	public static void main (String[] args) throws java.lang.Exception
	{
		Scanner sc = new Scanner(System.in);
        int T = sc.nextInt();
        while (T>0)
        {
            int N = sc.nextInt();
            int[] s = new int[N];
            for (int i = 0; i < N; i++)
                s[i] = sc.nextInt();
                
            Arrays.sort(s);
            int min = Integer.MAX_VALUE;
            
            for (int i = 0; i < N-1; i++) {
                if (s[i+1]-s[i]<min)
                    min = s[i+1]-s[i];
            }
            
            System.out.println(min);
            T--;
        }
	}
}

Disclaimer: The above Problem (Racing Horses) is generated by CodeChef but the Solution is Provided by CodingBroz. This tutorial is only for Educational and Learning Purpose.

Leave a Comment

Your email address will not be published.