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

----- Sample Input 1 ------ 
1
5
4 9 1 32 13

----- Sample Output 1 ------ 
3
----- explanation 1 ------ 
The minimum difference can be achieved if we pick horses with skills 1 and 4 for the race.

Source: codechef | Difficulty: EASY
Tags: Sorting

Complete search


Solutions:
t = int(input())
for i in range(t):
	x = int(input())
	a = list(map(int, input().split()))
	a.sort()
	m = a[1] - a[0]
	for j in range(x - 1):
		n = a[j + 1] - a[j]
		if n < m:
			m = n
	print(m)


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	l.sort()
	m = max(l)
	for i in range(0, n - 1):
		m = min(m, l[i + 1] - l[i])
	print(m)


for t in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	a.sort()
	c = max(a)
	for i in range(n - 1):
		c = min(a[i + 1] - a[i], c)
	print(c)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	a.sort()
	mini = 10 ** 9
	for i in range(1, n):
		mini = min(mini, a[i] - a[i - 1])
	print(mini)


t = int(input())
for i in range(t):
	n = int(input())
	arr = sorted(list(map(int, input().split())))
	if n == 2:
		print(abs(arr[0] - arr[1]))
	else:
		m = abs(arr[0] - arr[1])
		prev = arr[1]
		for i in range(2, n):
			m = min(m, abs(prev - arr[i]))
			prev = arr[i]
		print(m)


for _ in range(int(input())):
	n = int(input())
	lst = [int(x) for x in input().split()]
	lst.sort()
	min = lst[1] - lst[0]
	for i in range(n - 1):
		if lst[i + 1] - lst[i] < min:
			min = lst[i + 1] - lst[i]
	print(min)


for item in range(int(input())):
	x = int(input())
	y = list(map(int, input().split()))[:x]
	y.sort()
	z = []
	for i in range(x - 1):
		z.append(y[i + 1] - y[i])
	print(min(z))


n = int(input())
for i in range(n):
	ho = int(input())
	lst = list(map(int, input().split(' ')))
	lst.sort()
	diff = lst[1] - lst[0]
	for i in range(ho):
		if i + 1 != ho:
			if lst[i + 1] - lst[i] < diff:
				diff = lst[i + 1] - lst[i]
	print(diff)


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	l.sort()
	s = []
	for i in range(len(l) - 1):
		p = abs(l[i] - l[i + 1])
		s.append(p)
	print(min(s))


t = int(input())
for i in range(t):
	n = int(input())
	skills = sorted(map(int, input().split()))
	min_diff = min((skills[i + 1] - skills[i] for i in range(n - 1)))
	print(min_diff)


n = int(input())
for i in range(n):
	a = int(input())
	d = 9999999999999999
	l = list(map(int, input().split()))
	l.sort()
	for j in range(0, len(l) - 1):
		if abs(l[j + 1] - l[j]) < d:
			d = abs(l[j + 1] - l[j])
	print(d)


t = int(input())
for i in range(t):
	n = int(input())
	a = sorted(map(int, input().split()))
	l = []
	for i in range(n - 1):
		l.append(a[i + 1] - a[i])
	print(min(l))


for _ in range(int(input())):
	x = int(input())
	l = sorted(list(map(int, input().split())))
	y = []
	for i in range(x - 1):
		y.append(l[i + 1] - l[i])
	print(min(y))


for i in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	l.sort()
	min1 = l[1] - l[0]
	for i in range(n - 1):
		if l[i + 1] - l[i] < min1:
			min1 = min(min1, l[i + 1] - l[i])
	print(min1)


for i in range(int(input())):
	n = int(input())
	s = list(map(int, input().split()))
	s.sort()
	diff = []
	for i in range(n - 1):
		add = s[i + 1] - s[i]
		diff.append(add)
	print(min(diff))


for i in range(int(input())):
	x = int(input())
	l = sorted(list(map(int, input().split())))
	a = []
	for i in range(x - 1):
		m = l[i + 1] - l[i]
		a.append(m)
	print(min(a))


for x in range(0, int(input())):
	n = int(input())
	array = list(map(int, input().split()))
	array = sorted(array)
	minvalue = array[-1]
	for x in range(1, n):
		if array[x] - array[x - 1] < minvalue:
			minvalue = array[x] - array[x - 1]
	print(minvalue)


for _ in range(int(input())):
	n = int(input())
	a = sorted(list(map(int, input().split())))
	l = []
	for i in range(n - 1):
		l.append(abs(a[i] - a[i + 1]))
	print(min(l))


for i in range(int(input())):
	v = int(input())
	l = sorted(list(map(int, input().split())))
	x = []
	for i in range(v - 1):
		x.append(l[i + 1] - l[i])
	print(min(x))


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	if n < 2:
		print(a[0])
	else:
		a.sort()
		v = a[1] - a[0]
		for i in range(1, n - 1):
			temp = a[i + 1] - a[i]
			if temp < v:
				v = temp
		print(v)


t = int(input())
for tc in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	a.sort()
	s = 0
	d = []
	for j in range(1, n):
		s = abs(a[j] - a[j - 1])
		d.append(s)
	print(min(d))


for _ in range(int(input())):
	n = int(input())
	s = list(map(int, input().split()))
	s = sorted(s)
	x = []
	for i in range(n - 1):
		x.append(s[i + 1] - s[i])
	print(min(x))


for i in range(int(input())):
	N = int(input())
	S = list(map(int, input().split()))
	S.sort()
	B = []
	for i in range(N - 1):
		B.append(S[i + 1] - S[i])
	print(min(B))


n = int(input())
for _ in range(n):
	m = int(input())
	l = list(map(int, input().split()))
	l1 = []
	l.sort()
	for i in range(m - 1):
		l1.append(l[i + 1] - l[i])
	l1.sort()
	print(min(l1))


for _ in range(int(input())):
	ans = []
	n = int(input())
	skill = list(map(int, input().split()))
	skill.sort()
	for i in range(1, n):
		ans.append(skill[i] - skill[i - 1])
	ans.sort()
	print(ans[0])


t = int(input())
for i in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	arr.sort()
	mi = 10000000000
	for j in range(n - 1):
		if abs(arr[j] - arr[j + 1]) < mi:
			mi = abs(arr[j] - arr[j + 1])
	print(mi)


t = int(input())
while t > 0:
	n = int(input())
	l1 = [int(i) for i in input().split()]
	l1.sort()
	ans = l1[-1] - l1[0]
	for i in range(len(l1) - 1):
		n2n = l1[i + 1] - l1[i]
		ans = min(ans, n2n)
	print(ans)
	t -= 1


t = int(input())
for i in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	l.sort()
	min1 = []
	for i in range(n - 1):
		min1.append(l[i + 1] - l[i])
	print(min(min1))


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	a.sort()
	diff = a[-1] - a[0]
	for i in range(len(a) - 1):
		n2n_diff = a[i + 1] - a[i]
		diff = min(diff, n2n_diff)
	print(diff)


t = int(input())
for i in range(t):
	n = int(input())
	skills = input().split(' ')
	for j in range(n):
		skills[j] = int(skills[j])
	skills.sort()
	diff = []
	for x in range(n - 1):
		diff.append(skills[x + 1] - skills[x])
	print(min(diff))


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	l.sort()
	mn = l[-1]
	for i in range(1, n):
		p = abs(l[i] - l[i - 1])
		if p < mn:
			mn = p
	print(mn)


for _ in range(int(input())):
	N = int(input())
	S = list(map(int, input().split()))
	S = sorted(S)
	skills = []
	for i in range(1, len(S)):
		skills.append(S[i] - S[i - 1])
	absSkills = list(map(abs, skills))
	print(min(absSkills))


for i in range(int(input())):
	a = int(input())
	l = list(map(int, input().split()))
	y = sorted(l)
	t = []
	for j in range(a - 1):
		x = y[j + 1] - y[j]
		t.append(x)
	print(min(t))


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)


t = int(input())
for i in range(t):
	n = int(input())
	s = list(map(int, input().split(' ')))
	s.sort()
	res = []
	for i in range(len(s) - 1):
		if s[i + 1] - s[i] >= 0:
			res.append(s[i + 1] - s[i])
	print(min(res))


for _ in range(int(input())):
	n = int(input())
	lis = list(map(int, input().split()))
	lis.sort()
	mn = lis[1] - lis[0]
	for i in range(1, n - 1):
		if mn > lis[i + 1] - lis[i]:
			mn = lis[i + 1] - lis[i]
	print(mn)


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	l.sort()
	b = []
	for i in range(n - 1):
		b.append(abs(l[i] - l[i + 1]))
	print(min(b))


import sys

def racicnghorses():
	test = int(input().strip())
	for t in range(test):
		n = int(input().strip())
		arr = [int(x) for x in input().strip().split(' ')]
		list.sort(arr)
		mindiff = sys.maxsize
		num1 = 0
		num2 = 0
		for e in range(n):
			if e + 1 < n:
				currdiff = abs(arr[e] - arr[e + 1])
				if currdiff < mindiff:
					num1 = arr[e]
					num2 = arr[e + 1]
					mindiff = currdiff
		print(f'{mindiff}')
racicnghorses()


import math
for _ in range(int(input())):
	N = int(input())
	L = sorted(list(map(int, input().split())))
	res = math.inf
	for i in range(N - 1):
		res = min(res, L[i + 1] - L[i])
	print(res)


n = int(input())
for i in range(n):
	x = int(input())
	l = sorted(map(int, input().split()))
	m = []
	for j in range(x - 1):
		m.append(l[j + 1] - l[j])
	print(min(m))


t = int(input())
for i in range(t):
	x = int(input())
	s = sorted(list(map(int, input().split())))
	d = []
	for i in range(0, x - 1):
		d.append(abs(s[i] - s[i + 1]))
	print(min(d))


for i in range(int(input())):
	a = int(input())
	l = sorted(list(map(int, input().split())))
	d = l[1] - l[0]
	for i in range(a - 1):
		if l[i + 1] - l[i] < d:
			d = l[i + 1] - l[i]
	print(d)


T = int(input())
for i in range(T):
	N = int(input())
	A = list(map(int, input().split()))
	A.sort()
	P1 = A[1] - A[0]
	for j in range(1, len(A) - 1):
		P = A[j + 1] - A[j]
		if P < P1:
			P1 = P
		else:
			P1 = P1
	print(P1)


import os, sys, io, math
from re import *
from math import *
from array import *
from heapq import *
from bisect import *
from functools import *
from itertools import *
from statistics import *
from collections import *
I = lambda : [*map(int, sys.stdin.readline().split())]
IP = lambda : map(int, input().split())
IS = lambda : input()
IN = lambda : int(input())
IF = lambda : float(input())

def f():
	(n, l) = (IN(), I())
	l.sort()
	res = sys.maxsize
	for i in range(1, n):
		res = min(res, l[i] - l[i - 1])
	return res
for _ in range(IN()):
	print(f())


for t in range(int(input())):
	n = int(input())
	skill = sorted(list(map(int, input().split())))
	mini = 1000000000
	for i in range(1, n):
		if skill[i] - skill[i - 1] <= mini:
			mini = skill[i] - skill[i - 1]
	print(mini)


t = int(input())
for _ in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	l.sort()
	m = -1
	for i in range(n - 1):
		d = abs(l[i] - l[i + 1])
		if d < m or m == -1:
			m = d
	print(m)


for i in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	l.sort()
	m = float('inf')
	for i in range(n - 1):
		m = min(m, l[i + 1] - l[i])
	print(m)


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	arr.sort()
	diff = []
	for i in range(n - 1):
		d = arr[i + 1] - arr[i]
		diff.append(d)
	print(min(diff))


def findMinDiff(arr, n):
	arr = sorted(arr)
	diff = 10 ** 20
	for i in range(n - 1):
		if arr[i + 1] - arr[i] < diff:
			diff = arr[i + 1] - arr[i]
	return diff
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	print(findMinDiff(a, n))


q = int(input())
for i in range(q):
	n = int(input())
	s = list(map(int, input().split()))
	p = sorted(s)
	c = 0
	l = []
	for j in range(n - 1):
		l.append(p[j + 1] - p[j])
	print(min(l))


T = int(input())
for _ in range(T):
	N = int(input())
	S = list(map(int, input().split()))
	S.sort()
	mini = S[len(S) - 1]
	for i in range(len(S) - 1):
		if mini > S[i + 1] - S[i]:
			mini = S[i + 1] - S[i]
	print(mini)


t = int(input())
for i in range(t):
	n = int(input())
	s = list(map(int, input().split(' ')))
	s.sort()
	res = 10 ** 9
	for i in range(1, n):
		if s[i] - s[i - 1] < res:
			res = s[i] - s[i - 1]
	print(res)


test = int(input())
while test > 0:
	N = int(input())
	points = input().split()
	skills = list(map(int, points))
	skills.sort()
	diff = []
	for i in range(N - 1):
		var = abs(skills[i] - skills[i + 1])
		diff.append(var)
	print(min(diff))
	test = test - 1


import sys
t = int(input())
while t > 0:
	n = int(input())
	l1 = []
	x = input()
	x = x.split(' ')
	for i in x:
		l1.append(int(i))
	l1.sort()
	ans = sys.maxsize
	for i in range(0, n - 1):
		ans = min(ans, l1[i + 1] - l1[i])
	print(ans)
	t -= 1


res = 1e+100
for _ in range(int(input())):
	n = int(input())
	x = list(map(int, input().split(' ')))
	x.sort()
	for i in range(1, n):
		if res > x[i] - x[i - 1]:
			res = x[i] - x[i - 1]
	print(res)
	res = 1e+100


t = int(input())
for _ in range(t):
	n = int(input())
	skills = list(map(int, input().split()))
	skills.sort()
	mdiff = float('inf')
	for i in range(1, n):
		diff = skills[i] - skills[i - 1]
		if diff < mdiff:
			mdiff = diff
	print(mdiff)


t = int(input())
while t > 0:
	p = int(input())
	l = list(map(int, input().split()))
	l.sort()
	m = pow(10, 10)
	for i in range(0, p):
		if i != p - 1:
			d = l[i + 1] - l[i]
			m = min(d, m)
	print(m)
	t -= 1


a = int(input())
for i in range(a):
	n = int(input())
	k = list(map(int, input().split()))
	k.sort()
	s = 1000000000000000
	for j in range(n - 1):
		if k[j + 1] - k[j] < s:
			s = k[j + 1] - k[j]
	print(s)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	a.sort()
	diff = a[1] - a[0]
	for i in range(1, n):
		ans = a[i] - a[i - 1]
		if ans < diff:
			diff = ans
	print(diff)


T = int(input())
for t in range(T):
	N = int(input())
	S = list(map(int, input().split()))
	S.sort()
	DIFF = [S[i + 1] - S[i] for i in range(N - 1)]
	print(min(DIFF))


for _ in range(int(input())):
	onci = int(input())
	l1 = list(map(int, input().split()))
	l1.sort()
	mati = l1[-1] - l1[0]
	for i in range(len(l1) - 1):
		n2n = l1[i + 1] - l1[i]
		mati = min(mati, n2n)
	print(mati)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	a.sort()
	m = a[1] - a[0]
	for i in range(2, n):
		m = min(m, a[i] - a[i - 1])
	print(m)


for i in range(int(input())):
	n = int(input())
	s = list(map(int, input().split()))
	s.sort()
	l = []
	i = 0
	j = 0
	for i in range(n - 1):
		d = s[i + 1] - s[i]
		l.append(d)
	print(min(l))


def horses(nums):
	nums.sort()
	max_diff = nums[1] - nums[0]
	for i in range(1, len(nums) - 1):
		max_diff = min(max_diff, nums[i + 1] - nums[i])
	return max_diff
T = int(input())
for __ in range(T):
	n = int(input())
	nums = list(map(int, input().split(' ')))
	print(horses(nums))


import sys
for i in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	ans = sys.maxsize
	arr.sort()
	for i in range(1, n):
		ans = min(ans, arr[i] - arr[i - 1])
	print(ans)


for t in range(int(input())):
	N = int(input())
	S = list(map(int, input().split()))
	S.sort()
	gap = [abs(first - second) for (first, second) in zip(S[0:-1], S[1:])]
	print(min(gap))