There are n competitors in an exam. Each competitor has his own skill value which is given by the array arr where arr_{1} is the skill of the first competitor, arr_{2} is the skill of second competitor and so on. Two competitors are said to be tough competitors if their skill difference is least i.e. they are very close in their skill values. Given n and an array arr as input, find the tough competitors among the n competitors and print the absolute of the difference of their skill values.
Example 1:
Input:
n = 4
arr[] = {9, 4, 12, 6}
Output: 2
Explanation: As |9-4|=5, |9-12|=3, |9-6|=3,
|4-12|=8, |4-6|=2, |12-6|=6 so, the tough
competitors are competitors having skill
values 4, 6 having their skill difference
as 2.
Example 2:
Input:
n = 5
arr[] = {4, 9, 1, 32, 12}
Output: 3
Explanation: Tough competitors are
competitors having skill values (4,1)
and (9,12) having their skill difference
as 3.
Your Task:
You don't need to read input or print anything. Your task is to complete the function toughCompetitor() which takes the array of integers arr and n as parameters and returns an integer denoting the answer.
Expected Time Complexity: O(n*logn)
Expected Auxiliary Space: O(1)
Constraints:
2 <= n <= 10^{5}
1 <= arr[i] <= 10^{6}

Source: geeksforgeeks | Difficulty: EASY
Tags: Data structures

Starter code: #User function Template for python3




class Solution:
    def toughCompetitor(self, arr, n):
        # code here

Solutions:
class Solution:

	def toughCompetitor(self, arr, n):
		arr.sort()
		l = 99999999
		for i in range(1, n):
			l = min(l, arr[i] - arr[i - 1])
		return l


class Solution:

	def toughCompetitor(self, arr, n):
		arr.sort()
		absolute = arr[1] - arr[0]
		for i in range(n - 1):
			if arr[i + 1] - arr[i] < absolute:
				absolute = arr[i + 1] - arr[i]
		return absolute


class Solution:

	def toughCompetitor(self, arr, n):
		op = 999999
		arr.sort()
		for i in range(n - 1):
			lp = abs(arr[i] - arr[i + 1])
			op = min(op, lp)
		return op


class Solution:

	def toughCompetitor(self, arr, n):
		arr.sort()
		ans = float('inf')
		for i in range(n - 1):
			ans = min(abs(arr[i] - arr[i + 1]), ans)
		return ans


class Solution:

	def toughCompetitor(self, arr, n):
		arr.sort()
		min_diff = float('inf')
		for index in range(n - 1):
			diff = abs(arr[index] - arr[index + 1])
			if diff < min_diff:
				min_diff = diff
		return min_diff


class Solution:

	def toughCompetitor(self, arr, n):
		arr.sort()
		min_ = 99999
		for i in range(n - 1):
			diff = abs(arr[i] - arr[i + 1])
			min_ = min(min_, diff)
		return min_


class Solution:

	def toughCompetitor(self, arr, n):
		arr.sort()
		mi = arr[-1] - arr[0]
		for i in range(1, n):
			mi = min(mi, arr[i] - arr[i - 1])
		return mi


class Solution:

	def toughCompetitor(self, arr, n):
		arr.sort()
		min_val = 99999999
		for i in range(n - 1):
			min_val = min(min_val, abs(arr[i] - arr[i + 1]))
		return min_val


class Solution:

	def toughCompetitor(self, arr, n):
		arr.sort()
		val = 10000000
		for i in range(n - 1):
			sub = abs(arr[i] - arr[i + 1])
			if sub < val:
				val = sub
		return val


class Solution:

	def toughCompetitor(self, arr, n):
		arr.sort()
		min = 100000
		for i in range(n - 1):
			a = abs(arr[i] - arr[i + 1])
			if a < min:
				min = a
		return min


class Solution:

	def toughCompetitor(self, arr, n):
		arr.sort()
		dif = 9999999999
		for i in range(1, n):
			dif = min(dif, arr[i] - arr[i - 1])
		return dif


class Solution:

	def toughCompetitor(self, nums, n):
		arr.sort()
		diff = float('inf')
		for i in range(1, n):
			diff = min(diff, arr[i] - arr[i - 1])
		return diff


class Solution:

	def toughCompetitor(self, arr, n):
		arr.sort()
		d = abs(arr[0] - arr[1])
		for i in range(1, n - 1):
			if abs(arr[i] - arr[i + 1]) < d:
				d = abs(arr[i] - arr[i + 1])
		return d


class Solution:

	def toughCompetitor(self, arr, n):
		arr.sort()
		d = 10 ** 6
		for j in range(n - 1):
			d1 = abs(arr[j + 1] - arr[j])
			if d1 < d:
				d = d1
		return d


class Solution:

	def toughCompetitor(self, arr, n):
		arr.sort()
		val = 999999999999999999999
		for i in range(n - 1):
			diff = abs(arr[i] - arr[i + 1])
			val = min(val, diff)
		return val


class Solution:

	def toughCompetitor(self, arr, n):
		arr.sort()
		min = 1000000000
		for i in range(0, n - 1):
			if arr[i + 1] - arr[i] < min:
				min = arr[i + 1] - arr[i]
		return min


class Solution:

	def toughCompetitor(self, arr, n):
		a = abs(arr[1] - arr[0])
		arr.sort()
		for i in range(len(arr) - 1):
			diff = arr[i + 1] - arr[i]
			if diff <= a:
				a = diff
		return a


class Solution:

	def toughCompetitor(self, arr, n):
		arr.sort()
		minimum = abs(arr[0] - arr[1])
		for i in range(len(arr) - 1):
			minimum = min(minimum, abs(arr[i] - arr[i + 1]))
		return minimum


class Solution:

	def toughCompetitor(self, arr, n):
		arr.sort()
		diff = 100000000000
		for i in range(1, n):
			k = arr[i] - arr[i - 1]
			if k < diff:
				diff = k
		return diff


class Solution:

	def toughCompetitor(self, arr, n):
		arr.sort()
		m = 10 ** 7
		for i in range(n - 1):
			x = abs(arr[i] - arr[i + 1])
			if x < m:
				m = x
		return m


class Solution:

	def toughCompetitor(self, arr, n):
		arr.sort()
		least_diff = 2 ** 31
		for i in range(1, n):
			least_diff = min(least_diff, arr[i] - arr[i - 1])
		return least_diff


class Solution:

	def toughCompetitor(self, arr, n):
		arr.sort()
		a = []
		for i in range(n - 1):
			a.append(arr[i + 1] - arr[i])
		a.sort()
		return a[0]


class Solution:

	def toughCompetitor(self, arr, n):
		arr.sort()
		l = 10 ** 5
		for i in range(1, n):
			l = min(l, arr[i] - arr[i - 1])
		return l


class Solution:

	def toughCompetitor(self, arr, n):
		a = arr.sort()
		l = 0
		min_v = 999999
		for i in range(len(arr) - 1):
			min_v = min(min_v, abs(arr[i] - arr[i + 1]))
		return min_v


class Solution:

	def toughCompetitor(self, arr, n):
		arr.sort()
		diff = arr[1] - arr[0]
		for i in range(1, n - 1):
			temp = arr[i + 1] - arr[i]
			if temp < diff:
				diff = temp
		return diff


class Solution:

	def toughCompetitor(self, arr, n):
		arr.sort()
		k = []
		for i in range(1, n):
			k.append(arr[i] - arr[i - 1])
		return min(k)


class Solution:

	def toughCompetitor(self, arr, n):
		arr.sort()
		min_diff = abs(arr[0] - arr[n - 1])
		for i in range(n - 1):
			diff = abs(arr[i] - arr[i + 1])
			min_diff = min(min_diff, diff)
		return min_diff


import sys

class Solution:

	def toughCompetitor(self, arr, n):
		m = sys.maxsize
		arr.sort()
		for i in range(n - 1):
			m = min(m, abs(arr[i] - arr[i + 1]))
		return m


class Solution:

	def toughCompetitor(self, arr, n):
		arr.sort()
		m = arr[1] - arr[0]
		for i in range(1, n - 1):
			t = arr[i + 1] - arr[i]
			if m > t:
				m = t
		return m


class Solution:

	def toughCompetitor(self, arr, n):
		arr.sort()
		res = []
		for x in range(n - 1):
			res.append(arr[x + 1] - arr[x])
		return min(res)


class Solution:

	def toughCompetitor(self, arr, n):
		arr.sort()
		d = 99999999
		for i in range(n - 1):
			if arr[i + 1] - arr[i] < d:
				d = arr[i + 1] - arr[i]
		return d
if __name__ == '__main__':
	tc = int(input())
	while tc > 0:
		n = int(input().strip())
		arr = list(map(int, input().strip().split()))
		ans = Solution().toughCompetitor(arr, n)
		print(ans)
		tc -= 1


class Solution:

	def toughCompetitor(self, arr, n):
		minDiff = 999999
		arr.sort()
		for itr in range(n - 1):
			if minDiff > abs(arr[itr] - arr[itr + 1]):
				minDiff = abs(arr[itr] - arr[itr + 1])
		return minDiff
if __name__ == '__main__':
	tc = int(input())
	while tc > 0:
		n = int(input().strip())
		arr = list(map(int, input().strip().split()))
		ans = Solution().toughCompetitor(arr, n)
		print(ans)
		tc -= 1


class Solution:

	def toughCompetitor(self, arr, n):
		arr.sort()
		m = 9999
		for i in range(n - 1):
			m = min(m, abs(arr[i] - arr[i + 1]))
		return m


class Solution:

	def toughCompetitor(self, arr, n):
		arr.sort()
		minl = float('inf')
		for i in range(1, n):
			if arr[i] - arr[i - 1] < minl:
				minl = arr[i] - arr[i - 1]
		return minl


class Solution:

	def toughCompetitor(self, a, n):
		m = 100000
		a.sort()
		for i in range(0, n - 1):
			val = abs(a[i] - a[i + 1])
			if val < m:
				m = val
		return m


class Solution:

	def toughCompetitor(self, arr, n):
		arr.sort()
		m1 = arr[n - 1]
		for i in range(n - 1):
			if abs(arr[i] - arr[i + 1]) < m1:
				m1 = abs(arr[i] - arr[i + 1])
		return m1


class Solution:

	def toughCompetitor(self, arr, n):
		arr.sort()
		ans = 10000000
		for i in range(n - 1):
			diff = abs(arr[i + 1] - arr[i])
			ans = min(ans, diff)
		return ans


class Solution:

	def toughCompetitor(self, arr, n):
		arr.sort()
		m = max(arr)
		for i in range(n):
			if i + 1 < n and arr[i + 1] - arr[i] < m:
				m = arr[i + 1] - arr[i]
		return m


class Solution:

	def toughCompetitor(self, arr, n):
		i = 0
		p = 10000
		arr.sort()
		while i != n - 1:
			res = abs(arr[i] - arr[i + 1])
			if res < p:
				p = res
			i = i + 1
		return p


class Solution:

	def toughCompetitor(self, arr, n):
		mini = 9999999
		arr.sort()
		for i in range(n - 1):
			curr_sum = abs(arr[i] - arr[i + 1])
			if curr_sum < mini:
				mini = curr_sum
		return mini


class Solution:

	def toughCompetitor(self, arr, n):
		arr.sort()
		a = []
		for i in range(n - 1):
			sub = abs(arr[i] - arr[i + 1])
			a.append(sub)
		m = min(a)
		return m


class Solution:

	def toughCompetitor(self, arr, n):
		arr.sort()
		min = abs(arr[1] - arr[0])
		i = 0
		while i < n - 1:
			s = arr[i] - arr[i + 1]
			s = abs(s)
			if s < min:
				min = s
			i += 1
		return min