You are given an array $a$ consisting of $n$ integer numbers.

Let instability of the array be the following value: $\max\limits_{i = 1}^{n} a_i - \min\limits_{i = 1}^{n} a_i$.

You have to remove exactly one element from this array to minimize instability of the resulting $(n-1)$-elements array. Your task is to calculate the minimum possible instability.


-----Input-----

The first line of the input contains one integer $n$ ($2 \le n \le 10^5$) — the number of elements in the array $a$.

The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^5$) — elements of the array $a$.


-----Output-----

Print one integer — the minimum possible instability of the array if you have to remove exactly one element from the array $a$.


-----Examples-----
Input
4
1 3 3 7

Output
2

Input
2
1 100000

Output
0



-----Note-----

In the first example you can remove $7$ then instability of the remaining array will be $3 - 1 = 2$.

In the second example you can remove either $1$ or $100000$ then instability of the remaining array will be $100000 - 100000 = 0$ and $1 - 1 = 0$ correspondingly.

Source: codeforces | Difficulty: EASY
Tags: Implementation


Solutions:
a = input()
arr = list(map(int, input().split()))
arr.sort()
if abs(arr[0] - arr[-2]) < abs(arr[1] - arr[-1]):
	print(abs(arr[0] - arr[-2]))
else:
	print(abs(arr[1] - arr[-1]))


def main():
	count = int(input())
	arr = input().split()
	biggest = -10 ** 6
	second_biggest = -10 ** 6
	smallest = 10 ** 6
	second_smallest = 10 ** 6
	for x in range(count):
		test = int(arr[x])
		if test > second_biggest:
			if test > biggest:
				(biggest, second_biggest) = (test, biggest)
			else:
				second_biggest = test
		if test < second_smallest:
			if test < smallest:
				(smallest, second_smallest) = (test, smallest)
			else:
				second_smallest = test
	print(min(biggest - second_smallest, second_biggest - smallest))
main()


def arrayStabilization(ls):
	if ls.count(max(ls)) > 1 and ls.count(min(ls)) > 1:
		return max(ls) - min(ls)
	ls.sort()
	n = len(ls)
	if len(ls) == 1:
		return 0
	elif ls.count(max(ls)) == 1 and ls.count(min(ls)) == 1:
		rmin = ls[n - 1] - ls[1]
		rmax = ls[n - 2] - ls[0]
		return min(rmin, rmax)
	elif ls.count(max(ls)) > 1:
		return ls[n - 1] - ls[1]
	else:
		return ls[n - 2] - ls[0]
n = int(input())
ls = list(map(int, input().split(' ')))
print(arrayStabilization(ls))


n = int(input())
ar = list(map(int, input().split()))
ar.sort()
a = ar[-1] - ar[1]
b = ar[-2] - ar[0]
val = a if a < b else b
print(val)


n = int(input())
s = list(map(int, input().split()))
ma1 = 0
ma2 = 0
mi1 = 1000000000
mi2 = 1000000
for i in s:
	if i >= ma2:
		if i >= ma1:
			ma2 = ma1
			ma1 = i
		else:
			ma2 = i
	if i <= mi2:
		if i <= mi1:
			mi2 = mi1
			mi1 = i
		else:
			mi2 = i
if ma2 - mi1 > ma1 - mi2:
	print(ma1 - mi2)
else:
	print(ma2 - mi1)


n = int(input())
a = sorted(map(int, input().split()))
print(min(a[n - 2] - a[0], a[n - 1] - a[1]))


n = int(input())
m = list(map(int, input().split()))
n = m.copy()
x = max(m)
m.remove(x)
a = max(m) - min(m)
y = min(n)
n.remove(y)
b = max(n) - min(n)
print(min(a, b))


n = int(input())
goo = list(map(int, input().split()))
goo.sort()
mi = goo[0]
ma = goo[-1]
mi1 = goo[1]
ma1 = goo[-2]
a2 = ma - mi1
a3 = ma1 - mi
print(min(a2, a3))


n = int(input())
l = input().split()
lis = []
for i in l:
	lis.append(int(i))
lis.sort()
x = lis[1] - lis[0]
y = lis[len(lis) - 1] - lis[len(lis) - 2]
if x >= y:
	print(lis[len(lis) - 1] - lis[1])
else:
	print(lis[len(lis) - 2] - lis[0])


x = int(input())
ar = sorted(map(int, input().split(' ')))
print(min(ar[-2] - ar[0], ar[-1] - ar[1]))


import copy
n = int(input())
s = [int(i) for i in input().split()]
if s.count(max(s)) > 1:
	del s[s.index(min(s))]
	print(max(s) - min(s))
elif s.count(min(s)) > 1:
	del s[s.index(max(s))]
	print(max(s) - min(s))
else:
	s1 = copy.copy(s)
	s2 = copy.copy(s)
	del s1[s.index(min(s))]
	del s2[s.index(max(s))]
	r1 = max(s1) - min(s1)
	r2 = max(s2) - min(s2)
	print(min(r1, r2))


from sys import stdin, stdout
n = int(input())
a = list(map(int, stdin.readline().strip().split()))
a.sort()
stdout.write(str(min(a[-1] - a[1], a[-2] - a[0])) + '\n')


n = int(input())
l = list(map(int, input().split()))
l.sort()
x = l[n - 1] - l[1]
y = l[n - 2] - l[0]
if x > y:
	print(y)
else:
	print(x)


size = int(input())
arr = list(map(int, input().split()))
arr.sort()
arr1 = arr[:-1]
x1 = arr1[-1] - arr1[0]
arr2 = arr[1:]
x2 = arr2[-1] - arr2[0]
print(min(x1, x2))


n = int(input())
l = [int(x) for x in input().split()]
x = max(l)
del l[l.index(x)]
y = max(l) - min(l)
l.append(x)
x = min(l)
del l[l.index(x)]
x = max(l) - min(l)
print(min(x, y))


n = int(input())
l = [int(i) for i in input().split()]
s2 = i2 = l[0]
(s1, i1) = (0, 1000000.0)
for j in range(1, n):
	if l[j] > s2:
		(s1, s2) = (s2, l[j])
	elif l[j] > s1:
		s1 = l[j]
	if l[j] < i2:
		(i1, i2) = (i2, l[j])
	elif l[j] < i1:
		i1 = l[j]
print(min(s2 - i1, s1 - i2))


def minInstability(nums):
	if len(nums) == 2:
		return 0
	nums.sort()
	return min(nums[-1] - nums[1], nums[-2] - nums[0])
n = int(input().rstrip())
nums = [int(x) for x in input().split()]
print(minInstability(nums))


n = int(input())
li = list(map(int, input().split()))
li.sort()
if li[-1] - li[1] >= li[-2] - li[0]:
	print(li[-2] - li[0])
else:
	print(li[-1] - li[1])


n = int(input())
a = input().split()
for i in range(n):
	a[i] = int(a[i])
x = max(a)
y = min(a)
a.remove(x)
u = max(a)
a.append(x)
a.remove(y)
v = min(a)
a.append(y)
print(min(u - y, x - v))


n = int(input())
A = list(map(int, input().split()))
B = A[:]
C = A[:]
B.pop(B.index(min(B)))
C.pop(C.index(max(C)))
print(min(max(B) - min(B), max(C) - min(C)))


q = int(input())
numbers = [q]
numbers = list(map(int, input().split(' ')))
numbers.sort()
a = numbers[q - 1] - numbers[0]
b = numbers[q - 2] - numbers[0]
c = numbers[q - 1] - numbers[1]
print(min(a, b, c))


n = int(input())
a = list(map(int, input().split()))
c = a.copy()
a.remove(max(a))
b = []
b.append(max(a) - min(a))
c.remove(min(c))
b.append(max(c) - min(c))
print(min(b))


inp = int(input())
s = sorted(list(map(int, input().split())))
(mx, mn) = (max(s[:inp - 1]), min(s[:inp - 1]))
(Mx, Mn) = (max(s[1:]), min(s[1:]))
print(min(mx - mn, Mx - Mn))


def main():
	n = int(input())
	data = list(map(int, input().split()))
	if n == 2:
		print(0)
	else:
		w = min(data)
		z = max(data)
		data.remove(w)
		x = min(data)
		if n == 3:
			print(min(z - x, x - w))
		else:
			data.remove(z)
			y = max(data)
			print(min(z - x, y - w))
main()


n = int(input().strip())
a = list(map(int, input().strip().split()))
a = sorted(a)
if a[n - 1] - a[n - 2] > a[1] - a[0]:
	print(a[n - 2] - a[0])
else:
	print(a[n - 1] - a[1])


t = int(input())
m = list(map(int, input().split()))
ma1 = 0
ma2 = 0
mi1 = 100000000000000000
mi2 = 100000000000000000
for i in m:
	if i > ma1:
		ma2 = ma1
		ma1 = i
	elif i > ma2:
		ma2 = i
	if i < mi1:
		mi2 = mi1
		mi1 = i
	elif i < mi2:
		mi2 = i
print(min(ma1 - mi2, ma2 - mi1))


n = int(input())
a = sorted([int(z) for z in input().split()])
if n - 2:
	print(min(a[-1] - a[1], a[-2] - a[0]))
else:
	print(0)


n = int(input())
l = list(map(int, input().split()))
l.sort()
if n == 2:
	print(0)
else:
	print(min(l[-1] - l[0], l[-1] - l[1], l[-2] - l[0]))


n = int(input())
lis = sorted(list(map(int, input().split())))
print(min(lis[n - 2] - lis[0], lis[n - 1] - lis[1]))


import sys

class Solution(object):

	def solve(self):
		n = int(input())
		nums = list(map(int, input().strip().split(' ')))
		_max = 0
		_min = float('inf')
		_maxI = -1
		_minI = -1
		for i in range(n):
			if nums[i] > _max:
				_max = nums[i]
				_maxI = i
			if nums[i] < _min:
				_min = nums[i]
				_minI = i
		_sMax = 0
		_sMin = float('inf')
		for i in range(n):
			if nums[i] > _sMax and i != _maxI:
				_sMax = nums[i]
			if nums[i] < _sMin and i != _minI:
				_sMin = nums[i]
		return min(_max - _min, _max - _sMin, _sMax - _min)
sol = Solution()
print(sol.solve())


n = int(input())
arr = list(map(int, input().split()))
arr.sort()
if n <= 2:
	print(0)
elif n == 3:
	print(min(arr[1] - arr[0], arr[2] - arr[1]))
elif arr[1] - arr[0] < arr[-1] - arr[-2]:
	print(arr[-2] - arr[0])
else:
	print(arr[-1] - arr[1])


import math
import bisect
import itertools
import sys
I = lambda : sys.stdin.readline()
mod = 10 ** 9 + 7

def mindiff(a):
	b = a[:]
	b.sort()
	m = 10000000000
	for i in range(len(b) - 1):
		if b[i + 1] - b[i] < m:
			m = b[i + 1] - b[i]
	return m

def lcm(a, b):
	return a * b // math.gcd(a, b)

def merge(a, b):
	i = 0
	j = 0
	c = 0
	ans = []
	while i < len(a) and j < len(b):
		if a[i] < b[j]:
			ans.append(a[i])
			i += 1
		else:
			ans.append(b[j])
			c += len(a) - i
			j += 1
	ans += a[i:]
	ans += b[j:]
	return (ans, c)

def mergesort(a):
	if len(a) == 1:
		return (a, 0)
	mid = len(a) // 2
	(left, left_inversion) = mergesort(a[:mid])
	(right, right_inversion) = mergesort(a[mid:])
	(m, c) = merge(left, right)
	c += left_inversion + right_inversion
	return (m, c)

def is_prime(num):
	if num == 1:
		return False
	if num == 2:
		return True
	if num == 3:
		return True
	if num % 2 == 0:
		return False
	if num % 3 == 0:
		return False
	t = 5
	a = 2
	while t <= int(math.sqrt(num)):
		if num % t == 0:
			return False
		t += a
		a = 6 - a
	return True

def ceil(a, b):
	if a % b == 0:
		return a // b
	else:
		return a // b + 1

def binsearch(arr, b, low, high):
	if low == high:
		return low
	if arr[math.ceil((low + high) / 2)] < b:
		return binsearch(arr, b, low, math.ceil((low + high) / 2) - 1)
	else:
		return binsearch(arr, b, math.ceil((low + high) / 2), high)

def ncr1(n, r):
	s = 1
	for i in range(min(n - r, r)):
		s *= n - i
		s %= mod
		s *= pow(i + 1, mod - 2, mod)
		s %= mod
	return s

def calc(n, m, r):
	s = 0
	for i in range(0, r + 1, 2):
		s += ncr1(n, i) * ncr1(m, i)
		s %= mod
	return s
n = int(input())
a = list(map(int, input().split()))
a.sort()
print(min(a[-1] - a[0], a[-2] - a[0], a[-1] - a[1]))


(n, a) = (int(input()), list(map(int, input().split())))
ma = max(a)
mi = min(a)
a.sort()
x = min(a[n - 2] - mi, ma - a[1])
if x < 0:
	print(0)
else:
	print(x)


t = 1
for test in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	a.sort()
	if a[1] - a[0] > a[-1] - a[-2]:
		print(a[-1] - a[1])
	else:
		print(a[-2] - a[0])


n = int(input())
a = list(map(int, input().split()))
a.sort()
b = []
x = a[-2] - a[0]
b.append(x)
x = a[-1] - a[1]
b.append(x)
print(min(b))


n = int(input())
a = input().split()
a = [int(i) for i in a]
a.sort()
print(min(a[len(a) - 1] - a[1], a[len(a) - 2] - a[0]))


from sys import stdin

def myfunction(n, arr):
	if n <= 2:
		return 0
	arr.sort()
	return min(arr[-1] - arr[1], arr[-2] - arr[0])
n = int(stdin.readline().strip())
arr = list(map(int, stdin.readline().split()))
print(myfunction(n, arr))


length = int(input())
arr = sorted([int(i) for i in input().split()])
print(min(arr[length - 1] - arr[1], arr[length - 2] - arr[0]))


n = int(input())
q = sorted(map(int, input().split()))
print(min(q[-1] - q[1], q[-2] - q[0]))


n = int(input())
arr = list(map(int, input().split()))
mn = min(arr)
mx = max(arr)
mn_count = arr.count(mn)
mx_count = arr.count(mx)
if mn_count > 1 and mx_count > 1:
	print(mx - mn)
elif mn_count > 1 and mx_count == 1:
	arr.remove(mx)
	print(max(arr) - mn)
elif mx_count > 1 and mn_count == 1:
	arr.remove(mn)
	print(mx - min(arr))
elif mx_count == 1 and mx_count == 1:
	arr1 = arr[:]
	arr1.remove(mn)
	arr2 = arr[:]
	arr2.remove(mx)
	print(min(mx - min(arr1), max(arr2) - mn))


x = int(input())
arr = list(map(int, input().split()))
if x != 2:
	(mi, ma) = (max(arr), min(arr))
	(c1, c2) = (arr.copy(), arr.copy())
	c1.remove(mi)
	c2.remove(ma)
	print(min(max(c1) - min(c1), max(c2) - min(c2)))
else:
	print(0)


N = int(input())
brojevi = list(map(int, input().split()))
c = brojevi.index(min(brojevi))
a = brojevi.pop(c)
d = brojevi.index(max(brojevi))
b = brojevi.pop(d)
if N == 2:
	print(0)
else:
	print(min(max(brojevi) - a, b - min(brojevi)))


n = int(input())
ma1 = -1
ma2 = -1
C = 10 ** 5 + 7
mi1 = C
mi2 = C
for i in input().split():
	i = int(i)
	if i > ma1:
		ma2 = ma1
		ma1 = i
	elif i > ma2:
		ma2 = i
	if i < mi1:
		mi2 = mi1
		mi1 = i
	elif i < mi2:
		mi2 = i
if ma2 == -1 and mi2 == C:
	print(0)
else:
	print(min(ma1 - mi2, ma2 - mi1))


n = int(input())
a = list(map(int, input().split()))
a = sorted(a, reverse=True)
second_max = a[1]
second_min = a[-2]
minimum = a[-1]
maximum = a[0]
first = maximum - second_min
second = second_max - minimum
if first > second:
	print(second)
else:
	print(first)


n = int(input())
a = sorted(list(map(int, input().split())))
last = len(a) - 1
print(min(a[last] - a[1], a[last - 1] - a[0]))


n = int(input())
x = list(map(int, input().split()))
x = sorted(x)
a = 0
if n > 2:
	a = min(x[n - 1] - x[1], x[n - 2] - x[0])
print(a)


n = int(input())
li = [int(x) for x in input().split()]
li.sort()
print(min(li[n - 1] - li[1], li[n - 2] - li[0]))


n = int(input())
a = list(map(int, input().split(' ')))
ma = max(a)
mi = min(a)
b = a.copy()
b.pop(b.index(ma))
mba = max(b)
r1 = mba - mi
b = a.copy()
b.pop(b.index(mi))
mbi = min(b)
r2 = ma - mbi
print(min(r1, r2))


n = int(input())
nums = sorted(list(map(int, input().split())))
r1 = nums[-2] - nums[0]
r2 = nums[-1] - nums[1]
print(min(r1, r2))


n = int(input())
a = []
a = list(map(int, input().strip().split()))[:n]
a.sort()
min1 = a[0]
min2 = a[1]
max1 = a[n - 2]
max2 = a[n - 1]
stab1 = max2 - min2
stab2 = max1 - min1
stab3 = max2 - min1
if stab1 < stab2:
	print(stab1)
elif stab1 >= stab2:
	print(stab2)
else:
	print(stab3)


n = int(input())
s = input().split()
s = list(map(int, s))
s.sort()
print(min(s[n - 2] - s[0], s[n - 1] - s[1]))


n = int(input())
a = [int(s) for s in input().split()]
a.sort()
if a[-1] - a[1] < a[-2] - a[0]:
	print(a[-1] - a[1])
else:
	print(a[-2] - a[0])


length = int(input())
arr = list(map(int, input().split()))
orig = list(arr)
(arr1, arr2) = (list(arr), list(arr))
arr1.remove(max(arr1))
arr2.remove(min(arr2))
print(min([max(arr1) - min(arr1), max(arr2) - min(arr2)]))


n = int(input())
int_list = [int(x) for x in input().split()]
max_int_list = max(int_list)
temp = max_int_list
min_int_list = min(int_list)
instability = max_int_list - min_int_list
int_list.remove(max_int_list)
max_int_list = max(int_list)
instability1 = max_int_list - min_int_list
int_list.append(temp)
int_list.remove(min_int_list)
min_int_list = min(int_list)
instability2 = temp - min_int_list
if instability1 > instability2:
	print(instability2)
else:
	print(instability1)


n = int(input())
a = list(map(int, input().strip().split()))
a.sort()
if n > 1:
	print(min(a[n - 2] - a[0], a[n - 1] - a[1]))
else:
	print('0')


from sys import stdout, stdin
import math
n = int(input())
a = list(sorted(map(int, input().split())))
d1 = a[1] - a[0]
d2 = a[-1] - a[-2]
print(a[-1] - a[1] if d2 < d1 else a[-2] - a[0])


def masiv(i):
	i %= n
	return mas[i]
n = int(input())
mas = list(map(int, input().split()))
mas.sort()
print(min(masiv(-1) - masiv(1), masiv(-2) - masiv(0)))


n = int(input())
array = list(map(int, input().split()))
array.sort()
print(min(array[len(array) - 1] - array[1], array[len(array) - 2] - array[0]))


n = int(input())
s = input()
a = [int(i) for i in s.split()]
a = sorted(a)
rem_one = max(a[1:]) - min(a[1:])
rem_last = max(a[:-1]) - min(a[:-1])
print(min(rem_one, rem_last))


input()
a = list(map(int, input().split()))
a.sort()
print(min(a[-1] - a[1], a[-2] - a[0]))


def ins():
	n = int(input())
	if n == 2:
		return 0
	nums = [int(i) for i in input().split()]
	ma = max(nums)
	mi = min(nums)
	pos_res = ma - mi
	nums.remove(ma)
	pos_res = min(pos_res, max(nums) - mi)
	nums.append(ma)
	nums.remove(mi)
	pos_res = min(pos_res, ma - min(nums))
	return pos_res
print(ins())


n = int(input())
a = list(map(int, input().split()))
a.sort()
if a[1] - a[0] < a[n - 1] - a[n - 2]:
	ans = a[n - 2] - a[0]
else:
	ans = a[n - 1] - a[1]
print(ans)


n = int(input())
arr = list(map(int, input().split()))
arr.sort()
t = arr[n - 2] - arr[0]
tt = arr[n - 1] - arr[1]
print(min(t, tt))


n = int(input())
size = n - 1
a = [int(x) for x in input().split()]
a.sort()
print(min(a[size] - a[1], a[size - 1] - a[0]))


from sys import stdin, stdout
import cProfile, math
from collections import Counter
from bisect import bisect_left, bisect, bisect_right
import itertools
from copy import deepcopy
from fractions import Fraction
import sys, threading
import operator as op
from functools import reduce
sys.setrecursionlimit(10 ** 6)
threading.stack_size(2 ** 27)
printHeap = str()
memory_constrained = False
P = 10 ** 9 + 7
import sys
sys.setrecursionlimit(10000000)

class Operation:

	def __init__(self, name, function, function_on_equal, neutral_value=0):
		self.name = name
		self.f = function
		self.f_on_equal = function_on_equal

def add_multiple(x, count):
	return x * count

def min_multiple(x, count):
	return x

def max_multiple(x, count):
	return x
sum_operation = Operation('sum', sum, add_multiple, 0)
min_operation = Operation('min', min, min_multiple, 1000000000.0)
max_operation = Operation('max', max, max_multiple, -1000000000.0)

class SegmentTree:

	def __init__(self, array, operations=[sum_operation, min_operation, max_operation]):
		self.array = array
		if type(operations) != list:
			raise TypeError('operations must be a list')
		self.operations = {}
		for op in operations:
			self.operations[op.name] = op
		self.root = SegmentTreeNode(0, len(array) - 1, self)

	def query(self, start, end, operation_name):
		if self.operations.get(operation_name) == None:
			raise Exception('This operation is not available')
		return self.root._query(start, end, self.operations[operation_name])

	def summary(self):
		return self.root.values

	def update(self, position, value):
		self.root._update(position, value)

	def update_range(self, start, end, value):
		self.root._update_range(start, end, value)

	def __repr__(self):
		return self.root.__repr__()

class SegmentTreeNode:

	def __init__(self, start, end, segment_tree):
		self.range = (start, end)
		self.parent_tree = segment_tree
		self.range_value = None
		self.values = {}
		self.left = None
		self.right = None
		if start == end:
			self._sync()
			return
		self.left = SegmentTreeNode(start, start + (end - start) // 2, segment_tree)
		self.right = SegmentTreeNode(start + (end - start) // 2 + 1, end, segment_tree)
		self._sync()

	def _query(self, start, end, operation):
		if end < self.range[0] or start > self.range[1]:
			return None
		if start <= self.range[0] and self.range[1] <= end:
			return self.values[operation.name]
		self._push()
		left_res = self.left._query(start, end, operation) if self.left else None
		right_res = self.right._query(start, end, operation) if self.right else None
		if left_res is None:
			return right_res
		if right_res is None:
			return left_res
		return operation.f([left_res, right_res])

	def _update(self, position, value):
		if position < self.range[0] or position > self.range[1]:
			return
		if position == self.range[0] and self.range[1] == position:
			self.parent_tree.array[position] = value
			self._sync()
			return
		self._push()
		self.left._update(position, value)
		self.right._update(position, value)
		self._sync()

	def _update_range(self, start, end, value):
		if end < self.range[0] or start > self.range[1]:
			return
		if start <= self.range[0] and self.range[1] <= end:
			self.range_value = value
			self._sync()
			return
		self._push()
		self.left._update_range(start, end, value)
		self.right._update_range(start, end, value)
		self._sync()

	def _sync(self):
		if self.range[0] == self.range[1]:
			for op in self.parent_tree.operations.values():
				current_value = self.parent_tree.array[self.range[0]]
				if self.range_value is not None:
					current_value = self.range_value
				self.values[op.name] = op.f([current_value])
		else:
			for op in self.parent_tree.operations.values():
				result = op.f([self.left.values[op.name], self.right.values[op.name]])
				if self.range_value is not None:
					bound_length = self.range[1] - self.range[0] + 1
					result = op.f_on_equal(self.range_value, bound_length)
				self.values[op.name] = result

	def _push(self):
		if self.range_value is None:
			return
		if self.left:
			self.left.range_value = self.range_value
			self.right.range_value = self.range_value
			self.left._sync()
			self.right._sync()
			self.range_value = None

	def __repr__(self):
		ans = '({}, {}): {}\n'.format(self.range[0], self.range[1], self.values)
		if self.left:
			ans += self.left.__repr__()
		if self.right:
			ans += self.right.__repr__()
		return ans

def display(string_to_print):
	stdout.write(str(string_to_print) + '\n')

def ncr(n, r):
	r = min(r, n - r)
	numer = reduce(op.mul, range(n, n - r, -1), 1)
	denom = reduce(op.mul, range(1, r + 1), 1)
	return numer / denom

def primeFactors(n):
	factors = dict()
	for i in range(2, math.ceil(math.sqrt(n)) + 1):
		while n % i == 0:
			if i in factors:
				factors[i] += 1
			else:
				factors[i] = 1
			n = n // i
	if n > 2:
		factors[n] = 1
	return factors

def isprime(n):
	if n < 4:
		return True
	if n % 2 == 0:
		return False
	if n % 3 == 0:
		return False
	i = 5
	w = 2
	while i * i <= n:
		if n % i == 0:
			return False
		i += w
		w = 6 - w
	return True

def test_print(*args):
	if testingMode:
		print(args)

def display_list(list1, sep=' '):
	stdout.write(sep.join(map(str, list1)) + '\n')

def get_int():
	return int(stdin.readline().strip())

def get_tuple():
	return map(int, stdin.readline().split())

def get_list():
	return list(map(int, stdin.readline().split()))
memory = dict()

def clear_cache():
	global memory
	memory = dict()

def cached_fn(fn, *args):
	global memory
	if args in memory:
		return memory[args]
	else:
		result = fn(*args)
		memory[args] = result
		return result
TestCases = False
testingMode = False
optimiseForReccursion = False

def main():
	n = get_int()
	li = get_list()
	(a, b) = (max(li), min(li))
	li.remove(a)
	li.remove(b)
	if len(li) < 1:
		print(0)
		return
	(c, d) = (max(li), min(li))
	print(a - b - max(a - c, d - b))
if TestCases:
	for _ in range(get_int()):
		cProfile.run('main()') if testingMode else main()
else:
	(cProfile.run('main()') if testingMode else main()) if not optimiseForReccursion else threading.Thread(target=main).start()


t = int(input())
lis = list(map(int, input().split()))
if len(lis) > 2:
	lis.sort()
	n = len(lis)
	print(min(lis[n - 2] - lis[0], lis[n - 1] - lis[1]))
else:
	print(0)


n = int(input())
a = [int(x) for x in input().split()]
a.sort()
if len(a) == 2:
	print(0)
else:
	diff1 = a[-1] - a[-2]
	diff2 = a[1] - a[0]
	if diff2 >= diff1:
		print(a[-1] - a[1])
	else:
		print(a[-2] - a[0])


n = input()
n2 = int(n)
nstr = input()
nstr2 = nstr.split()
ns = [0 for i in range(n2)]
for x in range(n2):
	ns[x] = int(nstr2[x])
ns2 = sorted(ns)
max_num = min(int(ns2[n2 - 1]) - int(ns2[1]), int(ns2[n2 - 2]) - int(ns2[0]))
print(max_num)


n = int(input())
l = [int(i) for i in input().split()]
max1 = max(l)
min1 = min(l)
if len(l) == 2:
	print(0)
else:
	l.remove(max1)
	l.remove(min1)
	max2 = max(l)
	min2 = min(l)
	if abs(max1 - max2) > abs(min2 - min1):
		print(max2 - min1)
	else:
		print(max1 - min2)


n = int(input())
a = map(int, input().split())
a = sorted(a)
ans = 0 if n == 1 else min(a[-2] - a[0], a[-1] - a[1])
print(ans)


n = int(input())
t = list(map(int, input().split()))
l1 = max(t[0], t[1])
l2 = t[0] + t[1] - l1
s1 = l2
s2 = l1
for i in range(2, len(t)):
	if t[i] >= l1:
		l2 = l1
		l1 = t[i]
	elif t[i] > l2:
		l2 = t[i]
	if t[i] <= s1:
		s2 = s1
		s1 = t[i]
	elif t[i] < s2:
		s2 = t[i]
print(min(l1 - s2, l2 - s1))


n = int(input())
a = [int(s) for s in input().split()]
a.sort()
mn1 = a[n - 2] - a[0]
mn2 = a[n - 1] - a[1]
print(min(mn1, mn2))


t = int(input())
t = list(map(int, input().split()))
t.sort()
print(min(t[-2] - t[0], t[-1] - t[1]))


import copy
input()
a = list(map(int, input().split()))
b = copy.copy(a)
max_rem = a.remove(max(a))
min_rem = b.remove(min(b))
print(min(max(a) - min(a), max(b) - min(b)))


n = int(input())
li = sorted([int(i) for i in input().split()])
if n == 1 or n == 2:
	print(0)
else:
	print(min(li[-2] - li[0], li[-1] - li[1]))


x = int(input())
y = list(map(int, input().split()))
y.sort()
z = y.copy()
del y[-1]
m = y[0]
n = y[-1]
k = n - m
del z[0]
a = z[0]
b = z[-1]
l = b - a
if k > l:
	print(l)
else:
	print(k)


a = int(input())
b = list(map(int, input().split()))
b.sort()
c = b[1]
d = b[a - 2]
e = b[a - 1] - c
f = d - b[0]
print(min(e, f))


a = int(input())
b = input().split()
l1 = []
for i in b:
	l1.append(int(i))
b = l1[:]
if a == 2:
	print(0)
else:
	x = b.index(min(b))
	y = b.index(max(b))
	b.pop(x)
	l1.pop(y)
	i = max(b) - min(b)
	j = max(l1) - min(l1)
	if i > j:
		print(j)
	else:
		print(i)


n = int(input())
arr = input().split()
for i in range(n):
	arr[i] = int(arr[i])
maxi = max(arr)
mini = min(arr)
if len(arr) == 2:
	print('0')
	exit(0)
if arr.count(maxi) == 1:
	if arr.count(mini) == 1:
		arr.remove(mini)
		arr.remove(maxi)
		if maxi - max(arr) > min(arr) - mini:
			print(max(arr) - mini)
		else:
			print(maxi - min(arr))
	else:
		arr.remove(maxi)
		print(max(arr) - mini)
elif arr.count(mini) == 1:
	arr.remove(mini)
	print(maxi - min(arr))
else:
	print(maxi - mini)


n = int(input())
a = list(map(int, input().split()))
b = a[:]
if a.count(max(a)) == 1:
	a.pop(a.index(max(a)))
else:
	a.pop(a.index(min(a)))
if b.count(min(b)) == 1:
	b.pop(b.index(min(b)))
else:
	b.pop(b.index(max(b)))
print(min(max(a) - min(a), max(b) - min(b)))


n = int(input())
a = sorted(list(map(int, input().split())))
ar = a[1:]
a.pop()
print(min(max(ar) - min(ar), max(a) - min(a)))


import sys
n = int(sys.stdin.readline())
t = list(map(int, sys.stdin.readline().split()))
if n <= 2:
	print(0)
else:
	mx = max(t)
	t.remove(mx)
	mx2 = max(t)
	mn = min(t)
	t.remove(mn)
	mn2 = min(t)
	print(mx2 - mn) if mx2 - mn < mx - mn2 else print(mx - mn2)


n = int(input())
l = list(map(int, input().strip().split(' ')))
l = sorted(l)
mi = l[1:]
a1 = max(mi) - min(mi)
ma = l[:n - 1]
a2 = max(ma) - min(ma)
print(min(a1, a2))


from bisect import bisect_right as br
from bisect import bisect_left as bl
from collections import defaultdict
from itertools import combinations
import functools
import sys
import math
MAX = sys.maxsize
MAXN = 10 ** 6 + 10
MOD = 10 ** 9 + 7

def isprime(n):
	n = abs(int(n))
	if n < 2:
		return False
	if n == 2:
		return True
	if not n & 1:
		return False
	for x in range(3, int(n ** 0.5) + 1, 2):
		if n % x == 0:
			return False
	return True

def mhd(a, b, x, y):
	return abs(a - x) + abs(b - y)

def numIN():
	return map(int, sys.stdin.readline().strip().split())

def charIN():
	return sys.stdin.readline().strip().split()
t = [(-1, -1)] * 1000010

def create(a):
	global t, n
	for i in range(n, 2 * n):
		t[i] = (a[i - n], i - n)
	for i in range(n - 1, 0, -1):
		x = [t[2 * i], t[2 * i + 1]]
		x.sort(key=lambda x: x[0])
		t[i] = x[1]

def update(idx, value):
	global t, n
	idx = idx + n
	t[idx] = value
	while idx > 1:
		idx = idx // 2
		x = [t[2 * idx], t[2 * idx + 1]]
		x.sort(key=lambda x: x[0])
		t[idx] = x[1]

def cal(n, k):
	res = 1
	c = [0] * (k + 1)
	c[0] = 1
	for i in range(1, n + 1):
		for j in range(min(i, k), 0, -1):
			c[j] = (c[j] + c[j - 1]) % MOD
	return c[k]
n = int(input())
l = list(numIN())
l.sort()
x = l[:]
x.pop(0)
l.pop()
print(min(l[-1] - l[0], x[-1] - x[0]))


input()
a = sorted(list(map(int, input().split())))
print(min(a[-2] - a[0], a[-1] - a[1]))


n = int(input())
mat = list(map(int, input().split()))
mat = sorted(mat)
if n == 2:
	print(0)
else:
	print(min(mat[-2] - mat[0], mat[-1] - mat[1]))


n = int(input())
m = list(map(int, input().split()))
n = list(m)
m.sort(reverse=True)
n.sort(reverse=True)
del m[0]
del n[-1]
print(min(m[0] - m[-1], n[0] - n[-1]))


n = int(input())
x = sorted(list(map(int, input().split())))
s = []
s.append(x[n - 2] - x[0])
s.append(x[n - 1] - x[1])
print(min(s))


import sys
n = int(input())
a = [int(i) for i in input().split()]
a.sort()
print(min(a[n - 2] - a[0], a[n - 1] - a[1]))


n = int(input())
array = [int(c) for c in input().split()]
array.sort()
if n > 2:
	menor = array[0]
	segundo_menor = array[1]
	maior = array[n - 1]
	segundo_maior = array[n - 2]
	instability = maior - menor
	instability = min(instability, maior - segundo_menor)
	instability = min(instability, segundo_maior - menor)
	print(instability)
else:
	print(0)


n = int(input())
set = []
k = input().split()
for i in k:
	set.append(int(i))
j = max(set)
l = min(set)
if len(set) > 2:
	set.remove(max(set))
	set.remove(min(set))
	if j - max(set) > min(set) - l:
		set.append(l)
	else:
		min(set) - l > j - max(set)
		set.append(j)
	print(max(set) - min(set))
else:
	set.remove(max(set))
	print(max(set) - min(set))


def stabilizirovanie(lst):
	if len(lst) == 2:
		return 0
	a = sorted(lst)
	return min(a[len(lst) - 1] - a[1], a[len(lst) - 2] - a[0])
n = int(input())
b = [int(i) for i in input().split()]
print(stabilizirovanie(b))


n = int(input())
arr = list(map(int, input().split()))
arr.sort()
ans = min(arr[n - 2] - arr[0], arr[n - 1] - arr[1])
print(ans)


n = int(input())
nums = list(map(int, input().split()))
nums.sort()
if len(nums) == 2:
	print(0)
else:
	a_n = nums[len(nums) - 1]
	a_n_minus_1 = nums[len(nums) - 2]
	a_2 = nums[1]
	a_1 = nums[0]
	print(min(a_n - a_2, a_n_minus_1 - a_1))


n = int(input())
l = list(map(int, input().split(' ')))
l = sorted(l)
if l[n - 2] - l[0] > l[n - 1] - l[1]:
	print(l[n - 1] - l[1])
else:
	print(l[n - 2] - l[0])


n = int(input())
l = [int(x) for x in input().split()]
l.sort()
if l[0] == l[n - 1]:
	print(0)
else:
	print(min(l[n - 2] - l[0], l[n - 1] - l[1]))


def min(a, b):
	if a < b:
		print(a)
	else:
		print(b)
n = input()
n = int(n)
a = [0 for i in range(n)]
a = input().split()
for i in range(n):
	a[i] = int(a[i])
if n == 2:
	print(0)
else:
	a.sort()
	a1 = a[0]
	a4 = a[n - 1]
	a2 = a[0 + 1]
	a3 = a[n - 2]
	min(a3 - a1, a4 - a2)


n = int(input())
a = [int(i) for i in input().split()]
a.sort(reverse=True)
print(min(a[0] - a[-2], a[1] - a[-1]))


n = int(input())
l = list(map(int, input().split()))
if len(l) == 1 or len(l) == 2:
	print(0)
else:
	l.sort()
	if l[n - 2] - l[0] < l[n - 1] - l[1]:
		print(l[n - 2] - l[0])
	else:
		print(l[n - 1] - l[1])


l = int(input())
ln = [int(i) for i in input().split(' ')]
mn = 10000000
mn2 = 10000000
omn = mn
mx = 0
mx2 = 0
omx = mx
for i in range(0, len(ln)):
	if ln[i] < mn:
		mn2 = mn
		mn = ln[i]
	elif ln[i] < mn2:
		mn2 = ln[i]
	if ln[i] > mx:
		mx2 = mx
		mx = ln[i]
	elif ln[i] > mx2:
		mx2 = ln[i]
if len(ln) == 2:
	print(0)
elif mx2 - mn < mx - mn2:
	print(mx2 - mn)
else:
	print(mx - mn2)


N = int(input())
L = list((int(i) for i in input().split()))
L.sort()
print(min(L[N - 1] - L[1], L[N - 2] - L[0]))


t = int(input())
a = sorted(map(int, input().split()))
b = a[1:]
c = a[:len(a) - 1]
print(min(max(b) - min(b), max(c) - min(c)))


int(input())
a = [int(x) for x in input().split()]
z = max(a)
x = min(a)
a.remove(z)
d = max(a)
s = d - x
a.append(z)
a.remove(x)
n = min(a)
b = z - n
print(min(s, b))


import math
import os
import random
import re
import sys

def array(arr, n):
	arr.sort()
	ans = arr[n - 2] - arr[0]
	ans1 = arr[n - 1] - arr[1]
	print(min(ans, ans1))
n = int(input())
x = input().split()
x = [int(i) for i in x]
array(x, n)


n = int(input().strip())
a = [int(i) for i in input().strip().split(' ')]
a.sort()
if a[1] - a[0] > a[-1] - a[-2]:
	print(a[-1] - a[1])
else:
	print(a[-2] - a[0])


n = input()
a = [int(i) for i in input().strip().split(' ')]
a = sorted(a)
print(min(a[-1] - a[1], a[-2] - a[0]))


n = int(input())
l = input().split()
l = [int(i) for i in l]
l.sort()
a = l[-2] - l[0]
b = l[-1] - l[1]
print(min([a, b]))


num = int(input())
listt = list(map(int, input().split(' ')))
maxx = max(listt)
listt.remove(maxx)
inst1 = max(listt) - min(listt)
listt.append(maxx)
listt.remove(min(listt))
inst2 = max(listt) - min(listt)
print(min(inst1, inst2))


from copy import copy
n = int(input())
line = [int(i) for i in input().split()]
line_dup = copy(line)
ans = []
line_dup.remove(max(line_dup))
ans.append(max(line_dup) - min(line_dup))
line.remove(min(line))
ans.append(max(line) - min(line))
print(min(ans))


from collections import defaultdict
n = int(input())
if n == 2:
	print('0')
else:
	l = [int(x) for x in input().split()]
	freq = defaultdict(int)
	for x in l:
		freq[x] += 1
	mx = max(l)
	mn = min(l)
	if freq[mx] > 1 and freq[mn] > 1:
		print(mx - mn)
	elif freq[mx] > 1:
		l.remove(mn)
		print(mx - min(l))
	elif freq[mn] > 1:
		l.remove(mx)
		print(max(l) - mn)
	else:
		l.remove(mx)
		l.remove(mn)
		if mx - max(l) > min(l) - mn:
			print(max(l) - mn)
		else:
			print(mx - min(l))


n = int(input())
arr = []
array = input().split()
for i in range(n):
	arr.append(int(array[i]))
arr.sort()
p1 = arr[-1] - arr[1]
p2 = arr[-2] - arr[0]
if p1 < p2:
	print(p1)
else:
	print(p2)


def main():
	k = int(input())
	a = [int(x) for x in input().split()]
	a.sort()
	print(min(a[-2] - a[0], a[-1] - a[1]))
main()


n = int(input())
a = list(map(int, input().split(' ')))
t = min(a)
a.remove(t)
d1 = max(a) - min(a)
a.append(t)
t = max(a)
a.remove(t)
d2 = max(a) - min(a)
if d1 < d2:
	print(d1)
else:
	print(d2)


n = int(input())
L = [int(i) for i in input().strip().split()]
if len(L) == 2:
	print(0)
	exit()
mx = max(L)
mn = min(L)
L.remove(mx)
L.remove(mn)
mx2 = max(L)
mn2 = min(L)
val = mx - mn
x = max(mx - mx2, mn2 - mn)
print(val - x)


n = int(input())
l = list(map(int, input().split()))
l.sort()
(max1, max2) = (l[n - 1], l[n - 2])
(min1, min2) = (l[0], l[1])
print(max1 - min2 if max1 - min2 < max2 - min1 else max2 - min1)


n = int(input())
arr = list(map(int, input().split()))
arr.sort()
if len(arr) == 2:
	print(0)
else:
	first_removed = arr[-1] - arr[1]
	last_removed = arr[-2] - arr[0]
	print(min(first_removed, last_removed))


n = int(input())
arr = list(map(int, input().split()))
if n == 2:
	print(0)
else:
	arr.sort()
	a = abs(arr[0] - arr[n - 2])
	b = abs(arr[1] - arr[n - 1])
	min_ = min(a, b)
	print(min_)


n = int(input())
p = list(map(int, input().split()))
la = []
p.sort()
if n == 2:
	print(0)
else:
	la.append(p[-1] - p[1])
	la.append(p[-2] - p[0])
	print(min(la))


n = int(input())
l = list(map(int, input().split()))
if n == 2:
	print(0)
	exit()
l = sorted(l)
print(min(l[len(l) - 1] - l[1], l[len(l) - 2] - l[0]))


def arr_inp():
	return [int(x) for x in input().split()]
(n, a) = (int(input()), arr_inp())
a.sort()
print(min(a[n - 1] - a[1], a[n - 2] - a[0]))


n = int(input())
a = sorted([int(x) for x in input().split()])
k = a[0:-1]
l = a[1:]
if max(k) - min(k) <= max(l) - min(l):
	print(max(k) - min(k))
else:
	print(max(l) - min(l))


import sys
import collections
input = sys.stdin.readline

def printnl(val):
	sys.stdout.write(str(val) + '\n')
n = int(input())
a = list(map(int, input().split()))
a.sort()
print(min(a[-2] - a[0], a[-1] - a[1]))


n = int(input())
a = list(map(int, input().split(' ')))
a.sort()
t1 = a[n - 2] - a[0]
t2 = a[n - 1] - a[1]
ans = min(t1, t2)
print(ans)


compute_instability = lambda a: max(a) - min(a)
n = int(input())
array = list(map(int, input().split()))
cp_array = array.copy()
array.remove(max(array))
cp_array.remove(min(cp_array))
inst_min = compute_instability(array)
inst_max = compute_instability(cp_array)
if inst_max > inst_min:
	print(inst_min)
else:
	print(inst_max)


n = int(input())
array = input().split()
for i in range(n):
	array[i] = int(array[i])
array.sort()
test1 = array[n - 2] - array[0]
test2 = array[n - 1] - array[1]
if n == 2:
	print(0)
else:
	print(min(test1, test2))


def solve():
	n = int(input())
	arr = [int(k) for k in input().split()]
	arr.sort()
	if n == 2:
		print(0)
		return
	ans = arr[-1] - arr[1]
	ans = min(ans, arr[-2] - arr[0])
	print(ans)

def __starting_point():
	solve()
__starting_point()


n = int(input())
l = input().split(' ')
a = []
for i in range(0, n):
	a.append(int(l[i]))
if n == 2:
	print(0)
else:
	ma = max(a)
	mi = min(a)
	a.remove(ma)
	a.remove(mi)
	r1 = ma - min(a)
	r2 = max(a) - mi
	print(min(r1, r2))


import heapq
n = int(input())
a = list(map(int, input().split()))
(Min, Max) = ([], [])
heapq.heapify(Min)
heapq.heapify(Max)
for num in a:
	heapq.heappush(Min, num)
	heapq.heappush(Max, (-num, num))
Max_val = heapq.heappop(Max)[1]
Min_val = heapq.heappop(Min)
print(min(Max_val - Min[0], Max[0][1] - Min_val))


n = int(input())
nums = [int(i) for i in input().split()]
nums = sorted(nums)
opt1 = nums[-2] - nums[0]
opt2 = nums[-1] - nums[1]
print(min(opt1, opt2))


n = int(input())
l = list(map(int, input().split()))
if n == 1 or n == 2:
	print('0')
else:
	l.sort()
	temp = l[n - 2] - l[0]
	temp2 = l[n - 1] - l[1]
	if temp < temp2:
		print(temp)
	else:
		print(temp2)


length = int(input())
numbers = list(map(int, input().split()))
numbers.sort()
if numbers[length - 1] - numbers[1] > numbers[length - 2] - numbers[0]:
	numbers.remove(max(numbers))
else:
	numbers.remove(min(numbers))
maximum = max(numbers)
minimum = min(numbers)
print(maximum - minimum)


import copy
n = int(input())
s = list(map(int, input().split()))
c = copy.copy(s)
s.sort()
c.sort()
s.pop(-1)
c.pop(0)
a = s[-1] - s[0]
b = c[-1] - c[0]
print(min(a, b))


a = int(input())
l = list(map(int, input().split()))
b = list(l)
b.remove(min(b))
l.remove(max(l))
an1 = max(l) - min(l)
an2 = max(b) - min(b)
if an1 > an2:
	print(an2)
else:
	print(an1)


n = input()
l1 = [int(x) for x in input().split()]
l1.sort()
if len(l1) <= 2:
	print(0)
else:
	print(min(l1[-1] - l1[1], l1[-2] - l1[0]))


n = int(input())
a = list(map(int, input().split()))
s = []
k = max(a)
l = min(a)
a = sorted(a)
del a[0]
s.append(max(a) - min(a))
a.append(l)
a = sorted(a)
del a[-1]
s.append(max(a) - min(a))
print(min(s))


n = int(input())
arr = list(map(int, input().split()))
arr.sort()
minimum1 = arr[0]
maximum1 = arr[-2]
instability1 = maximum1 - minimum1
minimum2 = arr[1]
maximum2 = arr[-1]
instability2 = maximum2 - minimum2
if instability1 < instability2:
	print(instability1)
else:
	print(instability2)


n = input()
arr = [int(x) for x in input().split()]
arr.sort()
(x1, x2) = (arr[0], arr[1])
(y1, y2) = (arr[-1], arr[-2])
if y2 - x1 > y1 - x2:
	print(y1 - x2)
else:
	print(y2 - x1)


n = int(input())
a = list(map(int, input().split()))
a.sort()
x = a[-1] - a[1]
y = a[-2] - a[0]
print(min(x, y))


n = int(input())
l = list(map(int, input().split()))
myL = []
myL.append(max(l))
a = l.index(max(l))
l.remove(max(l))
myL.append(max(l))
l.insert(a, myL[0])
myL.append(min(l))
a = l.index(min(l))
l.remove(min(l))
myL.append(min(l))
l.insert(a, myL[2])
min_ins = -1
if n < 4:
	for i in range(n):
		a = l.pop(i)
		ins = max(l) - min(l)
		if min_ins < 0 or ins < min_ins:
			min_ins = ins
			l.insert(i, a)
		else:
			l.insert(i, a)
else:
	for i in range(4):
		a = myL.pop(i)
		ins = max(myL) - min(myL)
		if min_ins < 0 or ins < min_ins:
			min_ins = ins
			myL.insert(i, a)
		else:
			myL.insert(i, a)
print(min_ins)


n = int(input())
a = sorted([int(x) for x in input().split()])
p = a.pop()
mina = a[-1] - a[0]
a += [p]
a.pop(0)
minak = a[-1] - a[0]
print(min(mina, minak))


n = int(input())
L = [int(i) for i in input().split()]
L.sort()
if n == 2:
	print(0)
elif L[n - 2] - L[0] > L[n - 1] - L[1]:
	min = L[n - 1] - L[1]
	print(min)
else:
	min = L[n - 2] - L[0]
	print(min)


n = int(input())
data = list(map(int, input().split()))
data1 = max(data)
data.remove(max(data))
a = max(data) - min(data)
data.append(data1)
data2 = min(data)
data.remove(min(data))
b = max(data) - min(data)
if a > b:
	print(b)
else:
	print(a)


n = int(input())
arr = [int(i) for i in input().split()]
(max0, max1, min0, min1) = (0, 0, 10 ** 6, 10 ** 6)
for i in arr:
	if i > max0:
		(max0, max1) = (i, max0)
	elif i > max1:
		max1 = i
	if i < min0:
		(min0, min1) = (i, min0)
	elif i < min1:
		min1 = i
if n > 2:
	print(min(max1 - min0, max0 - min1))
if n == 2:
	print(0)


def inst(nos):
	instability = max(nos) - min(nos)
	return instability
t = int(input())
nos = [int(x) for x in input().split()]
nosone = nos.copy()
nostwo = nos.copy()
maxindex = nos.index(max(nos))
minindex = nos.index(min(nos))
nosone[maxindex] = nos[minindex]
nostwo[minindex] = nos[maxindex]
print(min(inst(nosone), inst(nostwo)))


N = int(input())
a = input().split()
max1 = 0
max2 = 0
k = 0
min1 = 1000000
min2 = 1000000
t = 0
for i in range(N):
	if int(a[i]) > max1:
		max1 = int(a[i])
		k = i
	if int(a[i]) < min1:
		min1 = int(a[i])
		t = i
for i in range(N):
	if int(a[i]) > max2 and i != k:
		max2 = int(a[i])
	if int(a[i]) < min2 and i != t:
		min2 = int(a[i])
if max2 == 0:
	max2 = max1
if min2 == 1000000:
	min2 = min1
if max1 - min2 <= max2 - min1:
	print(max1 - min2)
else:
	print(max2 - min1)


n = int(input())
array = [int(s) for s in input().split()]
array.sort()
n -= 1
if array[1] - array[0] > array[n] - array[n - 1]:
	print(array[n] - array[1])
else:
	print(array[n - 1] - array[0])


import sys
n = int(sys.stdin.readline().strip())
a = list(map(int, sys.stdin.readline().strip().split()))
max1 = 0
max2 = 0
min1 = 10 ** 5 + 1
min2 = 10 ** 5 + 1
for i in range(0, n):
	if a[i] >= max1:
		max2 = max1
		max1 = a[i]
	elif a[i] > max2:
		max2 = a[i]
	if a[i] <= min1:
		min2 = min1
		min1 = a[i]
	elif a[i] < min2:
		min2 = a[i]
print(min([max2 - min1, max1 - min2]))


n = int(input())
a = list(map(int, input().split()))
a.sort()
b = a[n - 2] - a[0]
c = a[n - 1] - a[1]
if b > c:
	print(c)
elif b == c:
	print(b)
else:
	print(b)


a = int(input())
ar = list(map(int, input().split()))
if a == 1 or a == 2:
	print(0)
	quit()
ar.sort()
print(min(ar[a - 2] - ar[0], ar[a - 1] - ar[1]))


a = int(input())
l = list(map(int, input().split()))
l.sort()
if l[-1] - l[0] > l[-1] - l[1]:
	if l[-1] - l[1] > l[-2] - l[0]:
		print(l[-2] - l[0])
	else:
		print(l[-1] - l[1])
elif l[-1] - l[0] > l[-2] - l[0]:
	print(l[-2] - l[0])
else:
	print(l[-1] - l[0])


n = int(input())
s = input()
l = list(map(int, s.split()))
if n <= 2:
	print('0')
else:
	l.sort()
	y = len(l)
	x = []
	x.append(l[y - 2] - l[0])
	x.append(l[y - 1] - l[1])
	print(min(x))


def read(type=1):
	if type:
		file = open('input.dat', 'r')
		n = int(file.readline())
		line = list(map(int, file.readline().split()))
		file.close()
	else:
		n = int(input().strip())
		line = list(map(int, input().strip().split()))
	return (n, line)

def solve():
	a.sort()
	if a[len(a) - 1] - a[1] < a[len(a) - 2] - a[0]:
		return a[len(a) - 1] - a[1]
	else:
		return a[len(a) - 2] - a[0]
(n, a) = read(0)
sol = solve()
print(sol)


n = int(input().strip())
an = list(map(int, input().strip().split()))
an.sort(reverse=False)
if len(an) > 2:
	if an[-1] == [-2]:
		print(an[-1] - an[1])
	elif an[0] == [1]:
		print(an[-2] - an[0])
	else:
		result = an[-2] - an[0] if an[-1] - an[1] > an[-2] - an[0] else an[-1] - an[1]
		print(result)
else:
	print(0)


n = int(input())
l = list(map(int, input().split()))
l1 = l.copy()
l2 = l.copy()
l1.remove(max(l1))
l2.remove(min(l2))
print(min(max(l1) - min(l1), max(l2) - min(l2)))


input()
X = list(map(int, input().split()))
X = sorted(X)
print(min(max(X[1:]) - min(X[1:]), max(X[:len(X) - 1]) - min(X[:len(X) - 1])))


n = int(input())
l = list(map(int, input().split()))
cpy = l.copy()
l.remove(max(l))
cpy.remove(min(cpy))
print(min(max(l) - min(l), max(cpy) - min(cpy)))


n = int(input())
a = [int(x) for x in input().split()]
minn = min(a)
maxx = max(a)
b = a.copy()
b.remove(maxx)
res1 = max(b) - minn
a.remove(minn)
res2 = maxx - min(a)
if res1 < res2:
	print(res1)
else:
	print(res2)


n = int(input())
mas = sorted(list(map(int, input().split())))
if mas[1] - mas[0] > mas[len(mas) - 1] - mas[len(mas) - 2]:
	mas.pop(0)
else:
	mas.pop()
print(mas[len(mas) - 1] - mas[0])


le = int(input())
arr = list(map(int, input().split(' ')))
arr.sort()
menu = arr[le - 2] - arr[0]
mend = arr[le - 1] - arr[1]
print(min(menu, mend))


ml = int(input())
m = sorted(list(map(int, input().split())))
if m[-1] - m[-2] >= m[1] - m[0]:
	print(m[-2] - m[0])
else:
	print(m[-1] - m[1])


n = int(input())
array = list(map(int, input().split()))
mini = array[0]
maxi = array[0]
count1 = 1
count2 = 1
for val in range(1, len(array)):
	if array[val] > maxi:
		maxi = array[val]
		count1 = 1
	elif array[val] == maxi:
		count1 += 1
	if array[val] < mini:
		mini = array[val]
		count2 = 1
	elif array[val] == mini:
		count2 += 1
if maxi == mini:
	print(0)
elif count1 > 1 and count2 > 1:
	print(maxi - mini)
elif count1 > 1:
	array.remove(mini)
	mini = min(array)
	print(maxi - mini)
elif count2 > 1:
	array.remove(maxi)
	maxi = max(array)
	print(maxi - mini)
else:
	array.remove(mini)
	mini1 = maxi - min(array)
	array.append(mini)
	array.remove(maxi)
	mini2 = max(array) - mini
	if mini1 < mini2:
		print(mini1)
	else:
		print(mini2)


n = int(input())
A = list(map(int, input().split()))
A.sort()
if n <= 2:
	print(0)
elif A[-1] - A[-2] > A[1] - A[0]:
	print(A[-2] - A[0])
else:
	print(A[-1] - A[1])


n = int(input())
l = list(map(int, input().split()))
a = max(l)
b = min(l)
l1 = l.copy()
l1.remove(a)
ans = max(l1) - min(l1)
l1 = l.copy()
l1.remove(b)
temp = max(l1) - min(l1)
ans = min(temp, ans)
print(ans)


n = int(input())
data = [int(i) for i in input().split(' ')]
data.sort()
val = [data[n - 2] - data[0], data[n - 1] - data[1]]
print(min(val))


n = int(input())
l = list(map(int, input().split()))
l.sort()
d1 = l[1] - l[0]
d2 = l[-1] - l[-2]
if d1 > d2:
	l.pop(0)
else:
	l.pop(-1)
print(l[-1] - l[0])


input()
p = sorted(map(int, input().split()))
print(min(p[-1] - p[1], p[-2] - p[0]))


n = int(input())
A = list(map(int, input().split()))
A.sort()
k = max(A)
m = min(A)
if A[1] - m < k - A[-2]:
	print(A[-2] - m)
else:
	print(k - A[1])


import sys
import os
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
mod = 10 ** 9 + 7

def lcm(x, y):
	return x * y / gcd(x, y)

def fact(x, mod=mod):
	ans = 1
	for i in range(1, x + 1):
		ans = ans * i % mod
	return ans

def arr2D(n, m, default=0):
	lst = []
	for i in range(n):
		temp = [default] * m
		lst.append(temp)
	return lst

def sortDictV(x):
	return {k: v for (k, v) in sorted(x.items(), key=lambda item: item[1])}

def solve(n, lst):
	lst = sorted(lst)
	return min(lst[-2] - lst[0], lst[-1] - lst[1])
n = int(input())
lst = list(map(int, input().split()))
print(solve(n, lst))


def array_instability(L):
	n = len(L)
	maxK = max(L)
	minJ = min(L)
	L.remove(maxK)
	J = L
	maxJ = max(J)
	K = J + [maxK]
	K.remove(minJ)
	minK = min(K)
	if maxJ - minJ >= maxK - minK:
		return maxK - minK
	else:
		return maxJ - minJ
n = int(input())
while n > 10 ** 5 or n < 2:
	n = int(input())
L = [int(i) for i in input().split()]
if len(L) > n:
	while len(L) > n:
		L.remove(L[len(L) - 1])
print(array_instability(L))


input()
num = [int(x) for x in input().split()]
num.sort()
if len(num) == 2:
	print(0)
	exit()
else:
	a = num[-2] - num[0]
	b = num[-1] - num[1]
	print(min(a, b))


t = int(input())
a = input().split(' ')
s = [int(i) for i in a]
s.sort()
value = min(s[t - 1] - s[1], s[t - 2] - s[0])
print(value)


n = input()
a = [int(i) for i in input().split()]
a = sorted(a)
n1 = a[-2] - a[0]
n2 = a[-1] - a[1]
print(min(n1, n2))


import sys
import math

def scan(input_type='int'):
	return list(map(int, sys.stdin.readline().strip().split()))

def solution():
	n = int(input())
	a = scan()
	b = {}
	for i in a:
		if i in b:
			b[i] += 1
		else:
			b[i] = 1
	a.sort()
	c = max(a)
	d = min(a)
	if n > 1:
		e = a[n - 1] - a[1]
		f = a[n - 2] - a[0]
		print(min(f, e))
	else:
		print(0)
solution()


from sys import stdin
stdin.readline()
nums = list(map(int, stdin.readline().rstrip().split(' ')))
nums.sort()
print(min(nums[-2] - nums[0], nums[-1] - nums[1]))


k = int(input())
p = list(map(int, input().split()))
p = sorted(p)
s = p[-1]
q = p[1]
r = p[-2]
rt = p[0]
print(min(s - q, r - rt))


n = int(input())
a = input()
ai = list(map(int, a.rstrip().split()))
bi = sorted(ai)
if bi[n - 1] - bi[1] > bi[n - 2] - bi[0]:
	print(bi[n - 2] - bi[0])
else:
	print(bi[n - 1] - bi[1])


import sys
t = int(input())
tab = list(map(int, sys.stdin.readline().split()))
tab.sort(reverse=True)
print(min(tab[1] - tab[t - 1], tab[0] - tab[t - 2]))


length = int(input())
array = list(map(int, input().rstrip().split()))
array.sort()
i1 = array[-1] - array[1]
i2 = array[-2] - array[0]
print(min(i1, i2))


import os
import sys
leng = int(input())
arr = list(map(int, input().split()))
mnm = min(arr)
mxm = max(arr)
arr2 = arr[:]
arr3 = arr[:]
arr2.remove(mnm)
nstb1 = max(arr2) - min(arr2)
arr3.remove(mxm)
nstb2 = max(arr3) - min(arr3)
print(min(nstb1, nstb2))


x = int(input())
A = list(map(int, input().split()))
B = A[:]
A.remove(max(A))
a = max(A) - min(A)
B.remove(min(B))
b = max(B) - min(B)
if a > b:
	print(b)
else:
	print(a)


n = input()
v = input()
v = v.split(' ')
A = []
for i in v:
	A.append(int(i))
mx = max(A)
mn = min(A)
s = mx - mn
A.remove(mx)
if s > max(A) - min(A):
	s = max(A) - min(A)
A.append(mx)
A.remove(mn)
if s > max(A) - min(A):
	s = max(A) - min(A)
print(s)


n = int(input())
list_n = []
for i in range(n):
	list_n.append(i)
list_n = map(int, input().split(' '))
array = []
for i in list_n:
	array.append(i)
array.sort()
array_first = array[1:]
array_last = array[:-1]
insta_first = array_first[-1] - array_first[0]
insta_last = array_last[-1] - array_last[0]
if insta_first < insta_last:
	print(insta_first)
else:
	print(insta_last)


a = int(input())
x = [int(i) for i in input().split()]
l = sorted(x)
print(min(l[a - 1] - l[1], l[a - 2] - l[0]))


import copy
n = int(input())
array = list(map(int, input().split()))
array2 = copy.deepcopy(array)
array3 = copy.deepcopy(array)
array2.remove(max(array2))
array3.remove(min(array3))
unst2 = max(array2) - min(array2)
unst3 = max(array3) - min(array3)
if unst2 > unst3:
	print(unst3)
else:
	print(unst2)


n = int(input())
a = input().strip().split(' ')
a = list(map(lambda x: int(x), a))
cur_max = max(a)
cur_min = min(a)
new_arr_max = a[:]
new_arr_max.remove(cur_max)
new_instability_max = max(new_arr_max) - min(new_arr_max)
new_arr_min = a[:]
new_arr_min.remove(cur_min)
new_instability_min = max(new_arr_min) - min(new_arr_min)
if new_instability_max < new_instability_min:
	print(new_instability_max)
else:
	print(new_instability_min)


n = int(input())
a = [int(e) for e in input().split()]
b = a.copy()
(mn, mx) = (min(a), max(a))
for (j, i) in enumerate(a):
	if i == mx:
		a[j] = mn
		break
mx = max(a)
(MN, MX) = (min(b), max(b))
for (j, i) in enumerate(b):
	if i == MN:
		b[j] = MX
		break
MN = min(b)
print(min(mx - mn, MX - MN))


n = int(input())
a = list(map(int, input().split()))
if n <= 2:
	exit(print('0'))
a.sort()
t = min(a[-1] - a[1], a[-2] - a[0])
print(t)


n = int(input())
x = list(map(int, input().split()))[:n]
s = max(x) - min(x)
x.sort()
rmvMin = max(x[1:len(x)]) - min(x[1:len(x)])
rmvMax = max(x[0:len(x) - 1]) - min(x[0:len(x) - 1])
avg = sum(x) // n
if rmvMin < rmvMax:
	print(rmvMin)
else:
	print(rmvMax)


n = int(input())
a = list(map(int, input().split()))
y = len(a)
a.sort()
m = a[y - 2] - a[0]
n = a[y - 1] - a[1]
if m > n:
	print(n)
else:
	print(m)


import math
n = int(input())
cl = list(map(int, input().split()))
cl = sorted(cl)
if n == 2:
	print(0)
else:
	a = cl[-1] - cl[1]
	b = cl[n - 2] - cl[0]
	if a < b:
		print(a)
	else:
		print(b)


x = int(input())
m = []
for i in input().split():
	m.append(int(i))
min = m[0]
max = m[0]
for i in range(x):
	if m[i] < min:
		min = m[i]
	if m[i] > max:
		max = m[i]
m.remove(min)
x -= 1
min2 = m[0]
for i in range(x):
	if min2 > m[i]:
		min2 = m[i]
m.append(min)
m.remove(max)
max2 = m[0]
for i in range(x):
	if max2 < m[i]:
		max2 = m[i]
r = max - min
if max2 - min < r:
	r = max2 - min
if max - min2 < r:
	r = max - min2
print(r)


n = int(input())
a = list(map(int, input().split()))
a.sort()
l = len(a)
m = min(a[l - 1] - a[1], a[l - 2] - a[0])
print(min(m, a[l - 1] - a[0]))


n = input()
l = list(map(int, input().split()))
mx = max(l)
mn = min(l)
if len(l) == 2:
	print(0)
else:
	l.remove(mx)
	l.remove(mn)
	print(min(max(l) - mn, mx - min(l)))


A = int(input())
L = list(map(int, input().split()))
R = min(L)
B = max(L)
if len(L) > 2:
	L.remove(min(L))
	L.remove(max(L))
	if max(L) - R < B - min(L):
		print(max(L) - R)
	else:
		print(B - min(L))
else:
	print(0)


n = int(input())
mylist = [int(x) for x in input().split()]

def ins(a):
	return max(a) - min(a)
x = [int(y) for y in mylist]
mylist.remove(max(mylist))
x.remove(min(x))
u = min(ins(mylist), ins(x))
print(u)


def solve():
	a = []
	line = input()
	n = int(line.split(' ')[0])
	line = input()
	for x in line.split(' '):
		a.append(int(x))
	minimum = 100001
	maximum = 0
	for x in a:
		if minimum > x:
			minimum = x
		if maximum < x:
			maximum = x
	premin = 100001
	premax = 0
	flagmin = 0
	flagmax = 0
	for x in a:
		if premin > x and (x != minimum or flagmin):
			premin = x
		if x == minimum:
			flagmin = 1
		if premax < x and (x != maximum or flagmax):
			premax = x
		if x == maximum:
			flagmax = 1
	if maximum - premin < premax - minimum:
		print(maximum - premin)
	else:
		print(premax - minimum)
solve()


input()
xs = list(map(int, input().split()))
xs.sort()
print(min(xs[-2] - xs[0], xs[-1] - xs[1]))


import sys
from collections import defaultdict
n = int(input())
arr = [int(__) for __ in input().split()]
if len(arr) == 2:
	print(0)
else:
	arr.sort()
	print(min(arr[-2] - arr[0], arr[-1] - arr[1]))


n = int(input())
s = input()
a = [int(i) for i in s.split()]
b = []
b.append(max(a))
b.append(min(a))
a.remove(b[0])
b.append(max(a))
a.append(b[0])
a.remove(b[1])
b.append(min(a))
print(min([abs(b[0] - b[1]), abs(b[0] - b[3]), abs(b[1] - b[2])]))


input()
sequence = [int(i) for i in input().split(' ')]
sequence.sort()
(a, b) = (sequence[-2] - sequence[0], sequence[-1] - sequence[1])
print(a) if a < b else print(b)


from math import *
n = int(input())
a = sorted(list(map(int, input().split())))
print(min(a[-1] - a[1], a[-2] - a[0]))


tam = int(input())
nums = [int(s) for s in input().split()]
nums.sort()
pos1 = nums[tam - 2] - nums[0]
pos2 = nums[tam - 1] - nums[1]
if pos1 > pos2:
	print(pos2)
else:
	print(pos1)


k = int(input())
a = list(map(int, input().split()))
x = max(a)
n = min(a)
new = []
flag = False
for i in a:
	if flag and i == x:
		new.append(i)
	if i != x:
		new.append(i)
	if i == x:
		flag = True
mx = max(new) - min(new)
new = []
flag = False
for i in a:
	if flag and i == n:
		new.append(i)
	if i != n:
		new.append(i)
	if i == n:
		flag = True
mn = max(new) - min(new)
if mn > mx:
	print(mx)
else:
	print(mn)


def read_nums():
	return [int(x) for x in input().split()]

def main():
	(n,) = read_nums()
	nums = sorted(read_nums())
	res = min(nums[-2] - nums[0], nums[-1] - nums[1])
	print(res)
main()


total = int(input())
numbers = [int(s) for s in input().split(' ')]
numbers.sort()
if numbers[-1] - numbers[-2] >= numbers[1] - numbers[0]:
	print(numbers[-2] - numbers[0])
else:
	print(numbers[-1] - numbers[1])


n = int(input())
l = list(map(int, input().split()))
l.sort()
ma = max(l)
mi = min(l)
l2 = l[:]
l2.remove(mi)
op1 = max(l2) - min(l2)
l2 = l[:]
l2.remove(ma)
op2 = max(l2) - min(l2)
print(min(op1, op2))


n = int(input())
l = list(map(int, input().split()))
l.sort()
cur = l[-1] - l[0]
print(cur - max(l[1] - l[0], l[-1] - l[-2]))


n = int(input())
arr = []
arr = [int(x) for x in input().split()]
arr.sort()
if n == 2:
	print('0')
else:
	a = arr[n - 1] - arr[0]
	b = arr[n - 2] - arr[0]
	c = arr[n - 1] - arr[1]
	print(min(a, b, c))


x = []
y = int(input())
x = list(map(int, input().split()))
x.sort()
a = x[0]
x.remove(a)
f = max(x) - min(x)
x.insert(0, a)
x.remove(x[y - 1])
l = max(x) - min(x)
if f < l:
	print(f)
else:
	print(l)


_ = input()
xs = [int(x) for x in input().split()]

def instability(xs, ignore):
	return max((n for (i, n) in enumerate(xs) if i != ignore)) - min((n for (i, n) in enumerate(xs) if i != ignore))
a = min(range(len(xs)), key=lambda i: xs[i])
b = max(range(len(xs)), key=lambda i: xs[i])
print(min(instability(xs, a), instability(xs, b)))


n = int(input())
if n == 2:
	s = input()
	print('0')
else:
	l = [int(i) for i in input().split()]
	l.sort()
	print(min(l[n - 1] - l[1], l[n - 2] - l[0]))


import sys
n = int(input())
a = list(map(int, sys.stdin.readline().strip().split()))
maxa = max(a)
mina = min(a)
maxindex = a.index(maxa)
minindex = a.index(mina)
a1 = a[:]
a2 = a[:]
del a1[maxindex]
res1 = max(a1) - min(a1)
del a2[minindex]
res2 = max(a2) - min(a2)
if res1 < res2:
	print(res1)
else:
	print(res2)


n = int(input())
arr = sorted(list(map(int, input().split())))
if len(arr) > 2:
	print(min(arr[len(arr) - 2] - arr[0], arr[len(arr) - 1] - arr[1]))
else:
	print(0)


n = int(input())
s = []
ma = 0
ma_n = 0
ma2 = 0
mi = 100004
mi_n = 0
mi2 = 100004
s = [int(i) for i in input().split()]
for i in range(n):
	if s[i] < mi:
		mi = s[i]
	if s[i] > ma:
		ma = s[i]
for i in range(n):
	if s[i] == ma:
		ma_n += 1
	if s[i] == mi:
		mi_n += 1
if mi_n == 1 and ma_n == 1:
	for i in range(n):
		if s[i] > ma2 and s[i] != ma:
			ma2 = s[i]
		if s[i] < mi2 and s[i] != mi:
			mi2 = s[i]
	if ma - mi2 < ma2 - mi:
		print(ma - mi2)
	else:
		print(ma2 - mi)
elif ma_n == 1:
	for i in range(n):
		if s[i] > ma2 and s[i] != ma:
			ma2 = s[i]
	print(ma2 - mi)
elif mi_n == 1:
	for i in range(n):
		if s[i] < mi2 and s[i] != mi:
			mi2 = s[i]
	print(ma - mi2)
else:
	print(ma - mi)


n = int(input())
a = input().split()
if n == 2:
	print(0)
	exit()
for i in range(n):
	a[i] = int(a[i])
a.sort()
if a[-1] - a[1] > a[-2] - a[0]:
	print(a[-2] - a[0])
else:
	print(a[-1] - a[1])


n = int(input())
a = [int(x) for x in input().split()]
a.sort()
m1 = a[0]
m2 = a[1]
n1 = a[n - 2]
n2 = a[n - 1]
d1 = n1 - m1
d2 = n2 - m2
if d2 >= d1:
	a.remove(n2)
else:
	a.remove(m1)
d = max(a) - min(a)
print(d)


n = int(input())
a = list(map(int, input().split()))
if n == 2:
	print(0)
else:
	a.sort()
	b = []
	c = []
	b = a[:n - 1]
	c = a[1:n]
	val1 = max(b) - min(b)
	val2 = max(c) - min(c)
	if val1 >= val2:
		print(val2)
	else:
		print(val1)


tam = int(input())
list = list(map(int, input().split()))
list.sort()
if list.count == 2:
	print(0)
elif list[len(list) - 1] - list[1] < list[len(list) - 2] - list[0]:
	print(list[len(list) - 1] - list[1])
else:
	print(list[len(list) - 2] - list[0])


n = int(input())
t = [int(i) for i in input().split()]
t.sort()
if t[-2] - t[0] < t[-1] - t[1]:
	print(t[-2] - t[0])
else:
	print(t[-1] - t[1])


n = int(input())
A = list(map(int, input().split()))
A.sort()
B = A[1:]
C = A[:-1]
print(min(max(B) - min(B), max(C) - min(C)))


n = int(input())
l = list(map(int, input().split()))
l.sort()
k = len(l)
if k > 1:
	m1 = l[k - 2] - l[0]
	m2 = l[k - 1] - l[1]
	print(min(m1, m2))
else:
	print('0')


t = int(input())
a = list(map(int, input().split(' ')))
b = sorted(a)
if t < 3:
	print(0)
else:
	t = max(a)
	g = min(a)
	a.remove(t)
	b.remove(g)
	print(min(max(a) - min(a), max(b) - min(b)))


(n, a) = (int(input()), sorted(list(map(int, input().split()))))
b = []
for i in a:
	b.append(i)
del a[0]
del b[-1]
print(min(max(a) - min(a), max(b) - min(b)))


from fractions import gcd
from itertools import combinations, permutations, accumulate, product
from collections import deque, defaultdict, Counter
import decimal
import re
import math
import bisect
import sys
sys.setrecursionlimit(10000000)
mod = 10 ** 9 + 7
from sys import stdin
readline = stdin.readline

def readInts():
	return list(map(int, readline().split()))

def readTuples():
	return tuple(map(int, readline().split()))

def I():
	return int(readline())
n = I()
A = readInts()
if n == 2:
	print(0)
else:
	A = sorted(A)
	print(min(abs(A[0] - A[-2]), abs(A[1] - A[-1])))


x = int(input())
y = list(map(int, input().split()))
y.sort()
i = y[len(y) - 1]
y.remove(i)
u = y[len(y) - 1] - y[0]
y.append(i)
y.remove(y[0])
v = y[len(y) - 1] - y[0]
if u > v:
	print(v)
else:
	print(u)


n = int(input())
arr = [int(x) for x in input().split()]
MIN = min(arr)
MAX = max(arr)
minIndex = arr.index(MIN)
maxIndex = arr.index(MAX)
sub_arr = arr[0:minIndex] + arr[minIndex + 1:]
sec_MIN = min(sub_arr)
sub_arr = arr[0:maxIndex] + arr[maxIndex + 1:]
sec_MAX = max(sub_arr)
print(min(sec_MAX - MIN, MAX - sec_MIN))


n = int(input())
l = list([int(x) for x in input().split()])
l.sort()
a = l[n - 2] - l[0]
b = l[n - 1] - l[1]
print(min(a, b))


length = int(input())
arr = list(map(lambda x: int(x), input().split(' ')))

def calculate_instability(arr):
	return arr[len(arr) - 1] - arr[0]
sorted_arr = sorted(arr)
print(min(calculate_instability(sorted_arr[:length - 1]), calculate_instability(sorted_arr[:length - 1]), calculate_instability(sorted_arr[1:])))


a = int(input())
b = [int(x) for x in input().split()]
instab = max(b) - min(b)
b = sorted(b)
if a > 2:
	print(instab - max(b[1] - b[0], b[-1] - b[-2]))
else:
	print(0)


n = int(input())
arr = sorted(list(map(int, input().split())))
tmp1 = arr[-1] - arr[1]
tmp2 = arr[-2] - arr[0]
print(min(tmp1, tmp2))


inp = int(input())
x = [int(x) for x in input().split()]
x.sort()
z = x.pop(inp - 1)
maximum = int(x[-1]) - int(x[0])
x.append(z)
l = x.pop(0)
minimum = int(x[-1]) - int(x[0])
if maximum > minimum:
	print(abs(minimum))
else:
	print(abs(maximum))


def calculate_inestability(ls):
	return max(ls) - min(ls)
n = int(input())
seq = list(map(int, input().split()))
if n == 2:
	print(0)
else:
	minimum = seq[0]
	idx_min = 0
	maximum = seq[-1]
	idx_max = n - 1
	for (idx, i) in enumerate(seq):
		if i <= minimum:
			minimum = i
			idx_min = idx
		if i >= maximum:
			maximum = i
			idx_max = idx
	if minimum == maximum:
		print(0)
	else:
		del seq[idx_max]
		ins_max = calculate_inestability(seq)
		seq.insert(idx_max, maximum)
		del seq[idx_min]
		ins_min = calculate_inestability(seq)
		if ins_max >= ins_min:
			print(ins_min)
		else:
			print(ins_max)


n = int(input())
l = list(map(int, input().split()))
l.sort()
l1 = l[0:len(l) - 1]
l2 = l[1:len(l)]
if max(l1) - min(l1) > max(l2) - min(l2):
	print(max(l2) - min(l2))
else:
	print(max(l1) - min(l1))


n = int(input())
a = list(map(int, input().split()))
fmax = 0
smax = 0
fmin = 10 ** 9
smin = 10 ** 9
for i in a:
	if i > fmax:
		(fmax, smax) = (i, fmax)
	elif i > smax:
		smax = i
	if i < fmin:
		(fmin, smin) = (i, fmin)
	elif i < smin:
		smin = i
print(min(fmax - smin, smax - fmin))


n = int(input())
array = [int(i) for i in input().split()]
array.sort()
instability = min(array[n - 1] - array[1], array[n - 2] - array[0])
print(instability)


n = int(input())
arr = list(map(int, input().split()))
(max1, max2, min1, min2) = (-1000000000.0, -1000000000.0, 2000000000.0, 2000000000.0)
for i in range(n):
	if max1 < arr[i]:
		max2 = max1
		max1 = arr[i]
	elif max2 < arr[i]:
		max2 = arr[i]
	if min1 > arr[i]:
		min2 = min1
		min1 = arr[i]
	elif min2 > arr[i]:
		min2 = arr[i]
print(min(max1 - min2, max2 - min1))


n = int(input())
a = list(map(int, input().split()))
a1 = []
a2 = []
for i in range(n):
	a1.append(a[i])
	a2.append(a[i])
a1.remove(max(a))
a2.remove(min(a))
print(min(max(a2) - min(a2), max(a1) - min(a1)))


n = int(input())
a = list(map(int, input().split()))
if n == 2:
	print(0)
	exit()
a.sort()
mina = min(abs(a[0] - a[n - 2]), abs(a[1] - a[n - 1]))
print(mina)


n = eval(input())
l = input().split(' ')
i = 0
while i < len(l):
	l[i] = int(l[i])
	i = i + 1
l.sort()
i = 0
maxa = l[len(l) - 2]
maxb = l[len(l) - 1]
mina = l[0]
minb = l[1]
x = maxa - mina
y = maxb - minb
if x < y:
	print(x)
else:
	print(y)


n = int(input())
a = [int(x) for x in input().split()]
tmp = list(a)
tmp.remove(max(tmp))
t = max(tmp) - min(tmp)
tmp = list(a)
tmp.remove(min(tmp))
m = max(tmp) - min(tmp)
if m < t:
	print(m)
else:
	print(t)


n = int(input())
a = [int(x) for x in input().split()]
a.sort()
if n <= 2:
	print(0)
else:
	ans = min(a[n - 1] - a[1], a[n - 2] - a[0])
	print(ans)


t = int(input())
A = list(map(int, input().strip().split()))
A = sorted(A)
if len(A) == 2:
	print(0)
else:
	print(min(A[-1] - A[0], A[-2] - A[0], A[-1] - A[1]))


n = int(input())
a = list(map(int, input().split()))
maxi = max(a)
mini = min(a)
kol_min = 0
kol_max = 0
for elem in a:
	if elem == maxi:
		kol_max += 1
	if elem == mini:
		kol_min += 1
pred_max = mini
pred_min = maxi
for elem in a:
	if elem > pred_max and elem != maxi:
		pred_max = elem
	if elem < pred_min and elem != mini:
		pred_min = elem
if kol_max > 1 and kol_min > 1:
	print(maxi - mini)
elif kol_max == 1 and kol_min > 1:
	print(pred_max - mini)
elif kol_max > 1 and kol_min == 1:
	print(maxi - pred_min)
else:
	print(min(maxi - pred_min, pred_max - mini))


import sys
n = int(input())
arr = [int(x) for x in input().split()]
if n == 2:
	print(0)
	sys.exit()
t = []
for i in range(n):
	t.append((arr[i], i))
t.sort()
a = abs(t[1][0] - t[n - 1][0])
b = abs(t[0][0] - t[n - 2][0])
print(min(a, b))


n = int(input())
l = list(map(int, input().split()))
l.sort()
if len(l) == 2:
	ans = 0
elif l[1] - l[0] > l[-1] - l[-2]:
	ans = l[-1] - l[1]
else:
	ans = l[-2] - l[0]
print(ans)


n = int(input())
a = sorted([int(i) for i in input().split(' ')])
inst_1 = a[-2] - a[0]
inst_2 = a[-1] - a[1]
print(min(inst_1, inst_2))


n = int(input())
a = sorted([int(x) for x in input().split()])
n -= 1
if a[n] - a[n - 1] > a[1] - a[0]:
	print(a[n - 1] - a[0])
else:
	print(a[n] - a[1])


trsah = input()
numbers = list(map(int, input().split(' ')))
numbers.sort()
results = []
if len(numbers) == 2:
	print(0)
else:
	first_possibility = numbers[-2] - numbers[0]
	second_possibility = numbers[-1] - numbers[1]
	if first_possibility > second_possibility:
		print(second_possibility)
	else:
		print(first_possibility)


n = int(input())
array = [int(i) for i in input().split()]
array.sort()
inst1 = array[len(array) - 1] - array[1]
inst2 = array[len(array) - 2] - array[0]
if inst1 <= inst2:
	print(inst1)
else:
	print(inst2)


n = int(input())
a = [int(i) for i in input().split()]
a = sorted(a)
min_0 = a[n - 2]
max_0 = 0
min_0 = a[n - 1] - a[1]
max_0 = a[n - 2] - a[0]
print(min(max_0, min_0))


tam = int(input())
v = list(map(int, input().split()))
v.sort()
print(min(v[tam - 1] - v[1], v[tam - 2] - v[0]))


n = int(input())
li = [int(x) for x in input().split()]
if len(li) == 2:
	print(0)
else:
	mi = min(li)
	ma = max(li)
	li.remove(mi)
	li.remove(ma)
	mi1 = min(li)
	ma2 = max(li)
	act = ma - mi
	act1 = ma - mi1
	act2 = ma2 - mi
	if act1 > act2:
		print(act2)
	else:
		print(act1)


gi = lambda : list(map(int, input().split()))
(n,) = gi()
l = gi()
l.sort()
ans = min(max(l) - min(l[1:]), max(l[:-1]) - min(l))
print(ans)


n = int(input())
a = list(map(int, input().split()))
i = a.index(max(a))
mx = a[i]
del a[i]
j = a.index(min(a))
mn = a[j]
del a[j]
if len(a) != 0:
	print(min(max(a) - mn, mx - min(a)))
else:
	print('0')


n = int(input())
a = list(map(int, input().split()))
b = a.copy()
c = a.copy()
x = max(b)
b.remove(x)
ans1 = max(b) - min(b)
y = min(c)
c.remove(y)
ans2 = max(c) - min(c)
if ans1 < ans2:
	print(ans1)
else:
	print(ans2)


n = int(input())
a = list(map(int, input().split()))
a.sort()
p = a[-1] - a[0]
k = a[-1] - a[1:][0]
t = a[:-1][-1] - a[0]
print(min(p, k, t))


w = int(input())
list1 = list(map(int, input().strip().split()))
list1.sort()
list2 = []
if len(list1) == 2:
	print('0')
else:
	for ele in list1:
		list2.append(ele)
	k = max(list1)
	p = min(list1)
	list1.remove(k)
	P = max(list1) - min(list1)
	list2.remove(p)
	Q = max(list2) - min(list2)
	print(min(P, Q))


import math

def mergeSort(lst):
	if len(lst) <= 1:
		return lst
	mid = math.floor(len(lst) / 2)
	left = mergeSort(lst[0:mid])
	right = mergeSort(lst[mid:])
	return merge(left, right)

def merge(arr1, arr2):
	i = 0
	j = 0
	lst = []
	while i < len(arr1) and j < len(arr2):
		if arr1[i] < arr2[j]:
			lst.append(arr1[i])
			i += 1
		else:
			lst.append(arr2[j])
			j += 1
	while i < len(arr1):
		lst.append(arr1[i])
		i += 1
	while j < len(arr2):
		lst.append(arr2[j])
		j += 1
	return lst
x = int(input())
lst = list(map(int, input().split()))
lst = mergeSort(lst)
print(lst[len(lst) - 1] - lst[1] if lst[len(lst) - 1] - lst[1] < lst[len(lst) - 2] - lst[0] else lst[len(lst) - 2] - lst[0])


def stabilize(a, n):
	if n <= 2:
		return 0
	lar = max(a)
	small = min(a)
	diff = lar - small
	a.remove(small)
	diff1 = lar - min(a)
	a.remove(lar)
	diff2 = max(a) - small
	return min(diff, diff1, diff2)
n = int(input())
a = [int(i) for i in range(n) and input().split()]
print(stabilize(a, n))


def stabilization(n, s):
	s.sort()
	if n > 2:
		if s[n - 2] - s[0] > s[n - 1] - s[1]:
			result = s[n - 1] - s[1]
		else:
			result = s[n - 2] - s[0]
	else:
		result = 0
	print(result)
n = int(input())
inp = list(map(int, input().split()))
stabilization(n, inp)


n = int(input())
a = list(map(int, input().split()))
a = sorted(a)
rmmax = a[:len(a) - 1]
rmmin = a[1:]
maxans = max(rmmax) - min(rmmax)
minans = max(rmmin) - min(rmmin)
print(min(maxans, minans))


input = __import__('sys').stdin.readline
n = int(input())
lis = sorted(map(int, input().split()))
if n == 2:
	print(0)
else:
	ans = min(lis[-1] - lis[1], lis[-2] - lis[0])
	print(ans)


from collections import defaultdict
from functools import reduce
mi = lambda : [int(i) for i in input().split()]
flat = lambda l: reduce(lambda a, b: a + b, l)
n = int(input())
a = sorted(mi())
print(min(a[-2] - a[0], a[-1] - a[1]))


def stable():
	a = int(input())
	b = input().split(' ')
	if a == 2:
		print(0)
	else:
		max1 = 0
		max2 = 0
		min1 = 10 ** 5
		min2 = 10 ** 5
		for i in b:
			i = int(i)
			if i > max2:
				if i > max1:
					max2 = max1
					max1 = i
				else:
					max2 = i
			if i < min2:
				if i < min1:
					min2 = min1
					min1 = i
				else:
					min2 = i
		print(min(max1 - min2, max2 - min1))
stable()


n = int(input())
l = list(map(int, input().split()))
l.sort()
le = len(l)
if le > 1:
	mx1 = l[le - 2] - l[0]
	mx2 = l[le - 1] - l[1]
	print(min(mx1, mx2))
else:
	print('0')


import sys
import math
get_string = lambda : sys.stdin.readline().strip()
get_int_list = lambda : list(map(int, sys.stdin.readline().strip().split()))
get_int = lambda : int(sys.stdin.readline())
pt = lambda x: sys.stdout.write(str(x) + '\n')
n = get_int()
lst = sorted(get_int_list())
in1 = lst[-2] - lst[0]
in2 = lst[-1] - lst[1]
if in1 < in2:
	print(in1)
else:
	print(in2)


n = int(input())
entrada = input().split(' ')
for i in range(len(entrada)):
	entrada[i] = int(entrada[i])
entrada.sort()
instabilidade1 = entrada[-1] - entrada[0]
instabilidade2 = entrada[-1] - entrada[1]
instabilidade3 = entrada[-2] - entrada[0]
print(min(instabilidade1, instabilidade2, instabilidade3))


import sys
from io import StringIO
from atexit import register
BUFFER = StringIO(sys.stdin.read())
input = lambda : BUFFER.readline()
sys.stdout = StringIO()
register(lambda : sys.__stdout__.write(sys.stdout.getvalue()))

def Rd(f=int):
	return [f(x) for x in input().split()]

def Debug(x):
	print('\x1b[0;31m{}\x1b[0m'.format(x), file=sys.stderr)

def PrintFloats(arr):
	return ' '.join(('{:.10f}'.format(x) for x in arr))
n = int(input())
A = Rd()
A.sort()
c1 = A[-1] - A[1]
c2 = A[-2] - A[0]
print(min(c1, c2))


n = int(input())
m_list = list(map(int, input().split()))
m_min = min(m_list)
m_max = max(m_list)
m_list.remove(m_min)
m_list.remove(m_max)
if len(m_list) == 0:
	print(0)
else:
	new_min = min(m_list)
	new_max = max(m_list)
	print(min(m_max - new_min, new_max - m_min))


n = int(input())
a = list(map(int, input().split()))
b = a.copy()
mx = max(a)
mn = min(a)
a.remove(mx)
b.remove(mn)
amx = max(a)
amn = min(a)
bmx = max(b)
bmn = min(b)
print(min(amx - amn, bmx - bmn))


num = int(input())
arr = list(map(int, input().split()))
arr.sort()
top_pop = arr[-2] - arr[0]
bottom_pop = arr[-1] - arr[1]
if top_pop > bottom_pop:
	print(bottom_pop)
else:
	print(top_pop)


def maxL(list):
	g = 0
	for num in list:
		if num > g:
			g = num
	return g

def minL(list):
	l = -1
	for num in list:
		if num < l or l == -1:
			l = num
	return l

def Cloning(li1):
	li_copy = li1[:]
	return li_copy
input()
x = input().split()
n = []
for s in x:
	n.append(int(s))
g = maxL(n)
l = minL(n)
n1 = Cloning(n)
n2 = Cloning(n)
n1.remove(g)
n2.remove(l)
print(min(maxL(n1) - min(n1), maxL(n2) - min(n2)))


n = int(input())
p = [int(x) for x in input().split()]
if n == 2:
	print(0)
else:
	p = sorted(p)
	print(min(p[n - 1] - p[1], p[n - 2] - p[0]))


n = int(input())
line = input().split()
array = []
for number in line:
	array.append(int(number))
array.sort()
if len(array) == 1 or len(array) == 2:
	print(0)
elif len(array) == 3:
	print(min(array[1] - array[0], array[2] - array[1]))
else:
	min_1 = array[0]
	min_2 = array[1]
	max_1 = array[n - 1]
	max_2 = array[n - 2]
	print(min(max_2 - min_1, max_1 - min_2))


input()
X = list(map(int, input().split()))
X = sorted(X)
print(min(X[1:][-1] - X[1:][0], X[:len(X) - 1][-1] - X[:len(X) - 1][0]))


import sys
import math
import bisect

def solve(A):
	n = len(A)
	A.sort()
	v1 = A[n - 2] - A[0]
	v2 = A[n - 1] - A[1]
	return min(v1, v2)

def main():
	n = int(input())
	A = list(map(int, input().split()))
	print(solve(A))
main()


n = int(input())
l = sorted(list(map(int, input().split())))
k = []
if n == 2:
	print(0)
else:
	k.append(l[-1] - l[0])
	k.append(l[-1] - l[1])
	k.append(l[-2] - l[0])
	print(min(k))


n = int(input())
array = input()
l = list(sorted(list(map(int, array.split(' ')))))
result1 = l[len(l) - 1] - l[1]
result2 = l[len(l) - 2] - l[0]
if result1 < result2:
	print(result1)
else:
	print(result2)


n = int(input())
l = list(map(int, input().split()))
l.sort()
ans = l[-2] - l[0]
ans1 = max(l) - min(l[1:])
print(min(ans, ans1))


n = int(input())
a = list(map(int, input().split()))
a.sort()
b = a[:n - 1]
c = a[1:]
print(min(max(b) - min(b), max(c) - min(c)))


def main():
	n = [int(item) for item in input().split()]
	p = n[0]
	a = [int(item) for item in input().split()]
	a.sort()
	k = a[p - 1] - a[1]
	l = a[p - 2] - a[0]
	ans = min(k, l)
	print(ans)
main()


n = int(input())
L = list(map(int, input().split()))
L.sort()
initial = L[-1] - L[0]
new1 = L[-2] - L[0]
new2 = L[-1] - L[1]
minim = min(initial, new1, new2)
print(minim)


n = int(input())
num_list = list(map(int, input().strip().split(' ')))
num_list.sort()
result = list()
result.append(num_list[n - 2] - num_list[0])
result.append(num_list[n - 1] - num_list[1])
print(result[0] if result[0] < result[1] else result[1])


input()
a = list(map(int, input().split()))
q = max(a)
w = min(a)
n = a * 1
m = a * 1
n.pop(n.index(q))
m.pop(m.index(w))
print(min(max(m) - min(m), max(n) - min(n)))


n = int(input())
a = list(map(int, input().split(' ')))
max_a = max(a)
min_a = min(a)
l = []
a.remove(max_a)
l.append(max(a) - min(a))
a.append(max_a)
a.remove(min_a)
l.append(max(a) - min(a))
a.append(min_a)
print(min(l))


import sys
n = int(input())
lst = list((int(x) for x in input().split(' ')))
lst.sort()
print(min(lst[n - 2] - lst[0], lst[n - 1] - lst[1]))


(n, l) = (int(input()), sorted(map(int, input().split())))
print(min(l[-1] - l[1], l[-2] - l[0]))


n = int(input())
a = [int(x) for x in input().split()]
a.sort()
if n == 2:
	print(0)
else:
	(max1, max2) = (a[-1], a[-2])
	(min1, min2) = (a[0], a[1])
	x = max2 - min1
	y = max1 - min2
	print(min(x, y))


n = int(input())
while n < 2 or n > 10 ** 5:
	n = int(input())
A = [int(i) for i in input().split()]
if len(A) > n:
	while len(A) > n:
		A.remove(A[len(A) - 1])
maxK = max(A)
minJ = min(A)
A.remove(maxK)
J = A
maxJ = max(J)
K = J + [maxK]
K.remove(minJ)
minK = min(K)
if maxJ - minJ >= maxK - minK:
	print(maxK - minK)
else:
	print(maxJ - minJ)


n = int(input())
A_raw = list(map(int, input().split(' ')))
if len(A_raw) == 2:
	print(0)
else:
	A = sorted(A_raw)
	if abs(A[0] - A[1]) <= abs(A[-1] - A[-2]):
		print(A[-2] - A[0])
	else:
		print(A[-1] - A[1])


n = int(input())
l = [int(x) for x in input().split()]
l.sort()
if l[0] == l[n - 1]:
	print(0)
else:
	mini_count = 1
	maxi_count = 1
	for i in range(1, n):
		if l[i] == l[0]:
			mini_count = 2
			break
	for i in range(n - 2, -1, -1):
		if l[i] == l[n - 1]:
			maxi_count = 2
			break
	if maxi_count < mini_count:
		print(l[n - 2] - l[0])
	elif maxi_count > mini_count:
		print(l[n - 1] - l[1])
	elif maxi_count == 1 and mini_count == 1:
		print(min(l[n - 2] - l[0], l[n - 1] - l[1]))
	else:
		print(l[n - 1] - l[0])


array_len = int(input())
arrey = list(map(int, input().rstrip().split()))
if len(arrey) > 2:
	min_arrey = min(arrey)
	max_arrey = max(arrey)
	arrey.remove(max_arrey)
	arrey.remove(min_arrey)
	s_min_arrey = min(arrey)
	s_max_arrey = max(arrey)
	print(min(max_arrey - s_min_arrey, s_max_arrey - min_arrey))
else:
	print(0)


def g():
	n = int(input())
	l = list(map(int, input().split()))
	l.sort()
	f = l[-1] - l[1]
	a = l[-2] - l[0]
	print(min(f, a))
g()


def solve(l):
	n = len(l)
	if n < 4:
		if n == 2:
			print(0)
		else:
			l.sort()
			ans = 10 ** 6
			for i in range(n):
				for j in range(i + 1, n):
					ans = min(ans, l[j] - l[i])
			print(ans)
		exit()
	max1 = max(l)
	l.remove(max1)
	max2 = max(l)
	l.remove(max2)
	min1 = min(l)
	l.remove(min1)
	min2 = min(l)
	l.remove(min(l))
	print(min(max2 - min1, max1 - min2))
n = int(input())
l = [*map(int, input().split())]
solve(l)


n = int(input())
A = sorted(map(int, input().split()))
print(min(A[-2] - A[0], A[-1] - A[1]))


def tester(c, a):
	x = 0
	for i in range(0, n):
		if c == a[i]:
			x += 1
		else:
			continue
	return x
n = int(input())
a = list(map(int, input().split()))
c = max(a)
d = tester(c, a)
c = min(a)
e = tester(c, a)
if d == 1:
	if e == 1:
		g = min(a)
		a.remove(g)
		f = max(a) - min(a)
		a.append(g)
		a.remove(max(a))
		h = max(a) - g
		print(min(f, h))
	else:
		a.remove(max(a))
		print(max(a) - min(a))
elif e == 1:
	a.remove(c)
	print(max(a) - min(a))
else:
	print(max(a) - min(a))


n = int(input())
a = list((int(x) for x in input().split()))
a.sort()
ans = min(a[n - 2] - a[0], a[n - 1] - a[1])
print(ans)


n = int(input())
L = list(map(int, input().split()))
Mx = max(L)
Mn = min(L)
L1 = L[:]
L1.remove(Mx)
L2 = L[:]
L2.remove(Mn)
print(min(max(L1) - Mn, Mx - min(L2)))


import copy
t = int(input())
lst = list(map(int, input().strip().split()))
a = max(lst)
b = min(lst)
lst_1 = copy.copy(lst)
lst_2 = copy.copy(lst)
lst_1.remove(a)
d1 = max(lst_1) - b
lst_2.remove(b)
d2 = a - min(lst_2)
if d1 <= d2:
	print(d1)
else:
	print(d2)


n = int(input())
t = [int(i) for i in input().split()]
l = sorted(t)
if n <= 2:
	print(0)
else:
	d = l[n - 2] - l[0]
	k = l[n - 1] - l[1]
	print(min(d, k))


def solve():
	n = int(input())
	arr = list(map(int, input().split()))
	arr = sorted(arr)
	if n <= 2:
		print(0)
		return
	else:
		x = arr[n - 1] - arr[1]
		y = arr[n - 2] - arr[0]
		print(min(x, y))
solve()


t = int(input())
numbers = list(map(lambda x: int(x), input().split(' ')))
numbers.sort()
if numbers[t - 2] - numbers[0] > numbers[t - 1] - numbers[1]:
	print(numbers[t - 1] - numbers[1])
else:
	print(numbers[t - 2] - numbers[0])


import sys
n = int(input())
arr = list(map(int, input().split()))
min = -1
max = -1
min2 = -1
max2 = -1
for num in arr:
	if num >= max:
		max2 = max
		max = num
	elif num > max2:
		max2 = num
	if num <= min or min == -1:
		min2 = min
		min = num
	elif num < min2 or min2 == -1:
		min2 = num
if min2 == -1 or max2 == -1:
	print(max - min)
	sys.exit()
if max - min2 < max2 - min:
	print(max - min2)
else:
	print(max2 - min)


n = int(input())
arr = list(map(int, input().split()))
max_el = max(arr)
min_el = min(arr)
arr.remove(max_el)
arr.remove(min_el)
try:
	max_el_2 = max(arr)
	min_el_2 = min(arr)
	otv1 = max_el - min_el
	otv2 = max_el - min_el_2
	otv3 = max_el_2 - min_el
	print(min(otv1, otv2, otv3))
except:
	print(0)


def rmv(n, listi):
	for i in range(len(listi)):
		if listi[i] == n:
			listi.pop(i)
			return listi
n = int(input())
listi = list(map(int, input().split()))
listii = listi[:]
listi = rmv(min(listi), listi)
listii = rmv(max(listii), listii)
print(min(max(listi) - min(listi), max(listii) - min(listii)))


def array_instability(L):
	n = len(L)
	maxK = max(L)
	minJ = min(L)
	L.remove(maxK)
	J = L
	maxJ = max(J)
	K = J + [maxK]
	K.remove(minJ)
	minK = min(K)
	if maxJ - minJ >= maxK - minK:
		return maxK - minK
	else:
		return maxJ - minJ
n = int(input())
L = list((int(i) for i in input().split()))
print(array_instability(L))


n = int(input())
A = [int(k) for k in input().split()]
if n == 2:
	print(0)
else:
	max1 = 0
	max2 = 0
	for i in range(n):
		if A[i] > max1:
			max2 = max1
			max1 = A[i]
		elif A[i] > max2:
			max2 = A[i]
	min1 = 100001
	min2 = 100001
	for i in range(n):
		if A[i] < min1:
			min2 = min1
			min1 = A[i]
		elif A[i] < min2:
			min2 = A[i]
	print(min(max2 - min1, max1 - min2))


n = int(input())
a = list(map(int, input().split()))
a.sort()
b = a[n - 2] - a[0]
c = a[n - 1] - a[1]
print(min(b, c))


A = int(input())
L = list(map(int, input().split()))
min1 = float('inf')
min2 = float('inf')
max1 = -float('inf')
max2 = -float('inf')
for i in L:
	if i >= max1:
		max2 = max1
		max1 = i
	elif i > max2:
		max2 = i
	if i <= min1:
		min2 = min1
		min1 = i
	elif i < min2:
		min2 = i
print(min(max2 - min1, max1 - min2))


t = int(input())
a = []
a = input().split()

def instable(arr):
	return max(arr) - min(arr)
for i in range(t):
	a[i] = int(a[i])
a.sort()
b = a[1:]
c = a[:len(a) - 1]
print(min(instable(c), instable(b)))


from copy import deepcopy
n = int(input())
a = [int(i) for i in input().split()]
a.sort()
print(min(a[n - 2] - a[0], a[n - 1] - a[1]))


def main():
	n = int(input())
	if n == 3:
		a = [i for i in map(int, input().split())]
		a.sort()
		if a[1] - a[0] > a[2] - a[1]:
			print(a[2] - a[1])
		else:
			print(a[1] - a[0])
		return
	minimum = p_minimum = 99999
	maximum = p_maximum = -1
	for i in map(int, input().split()):
		if i > maximum:
			p_maximum = maximum
			maximum = i
		elif i > p_maximum:
			p_maximum = i
		if i < minimum:
			p_minimum = minimum
			minimum = i
		elif i < p_minimum:
			p_minimum = i
	delta_max = maximum - p_maximum
	delta_min = -(minimum - p_minimum)
	print(maximum - minimum - max(delta_max, delta_min))
main()


a = input()
b = input().split()
b.sort(key=lambda x: int(x))
e = int(b[len(b) - 1]) - int(b[1])
f = int(b[len(b) - 2]) - int(b[0])
if f > e:
	print(e)
else:
	print(f)


n = int(input())
k = sorted((int(i) for i in input().split()))
print(min(k[-1] - k[1], k[-2] - k[0]))


n = int(input())
l = list(map(int, input().split()))
a = max(l)
b = min(l)
l.remove(a)
c = max(l)
e = c - b
l.append(a)
l.remove(b)
c = min(l)
f = a - c
print(min(e, f))


import math, string, itertools, fractions, heapq, collections, re, array, bisect, sys, random, time, copy, functools
sys.setrecursionlimit(10 ** 7)
inf = 10 ** 20
eps = 1.0 / 10 ** 10
mod = 10 ** 9 + 7
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]
ddn = [(-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1)]

def LI():
	return [int(x) for x in sys.stdin.readline().split()]

def LI_():
	return [int(x) - 1 for x in sys.stdin.readline().split()]

def LF():
	return [float(x) for x in sys.stdin.readline().split()]

def LS():
	return sys.stdin.readline().split()

def I():
	return int(sys.stdin.readline())

def F():
	return float(sys.stdin.readline())

def S():
	return input()

def pf(s):
	return print(s, flush=True)

def main():
	n = I()
	a = sorted(LI())
	if n == 2:
		return 0
	t = a[-1] - a[1]
	u = a[-2] - a[0]
	return min(t, u)
print(main())


class Solution(object):

	def get_min_instability(self, input):
		if input == None or len(input) == 0:
			return None
		firstMax = secondMax = firstMin = secondMin = input[0]
		for i in range(1, len(input)):
			firstMax = max(firstMax, input[i])
			firstMin = min(firstMin, input[i])
		if firstMax == firstMin:
			return 0
		secondMax = firstMin
		secondMin = firstMax
		firstMaxCount = 0
		firstMinCount = 0
		for i in range(0, len(input)):
			if input[i] < firstMax:
				secondMax = max(secondMax, input[i])
			if input[i] > firstMin:
				secondMin = min(secondMin, input[i])
			if input[i] == firstMax:
				firstMaxCount += 1
			if input[i] == firstMin:
				firstMinCount += 1
		if firstMaxCount > 1 and firstMinCount > 1:
			return firstMax - firstMin
		diffMaxes = firstMax - secondMax
		diffMins = secondMin - firstMin
		if firstMaxCount == 1 and (diffMaxes > diffMins or firstMinCount > 1):
			return secondMax - firstMin
		elif firstMinCount == 1 and (diffMins > diffMaxes or firstMaxCount > 1):
			return firstMax - secondMin
		else:
			return firstMax - secondMin

def read_input():
	return [int(x) for x in input().split()]
n = read_input()[0]
input = read_input()
sol = Solution()
result = sol.get_min_instability(input)
print(result)


n = int(input())
l = list(map(int, input().split()))
if len(l) > 2:
	a = max(l)
	l.remove(a)
	b = min(l)
	l.remove(b)
	c = max(l)
	d = min(l)
	print(min(c - b, a - d))
else:
	print(0)


import sys
n = int(input())
a = list(map(int, input().split()))
a.sort()
print(min(a[-1] - a[1], a[-2] - a[0]))
sys.exit()


n = int(input())
min1 = 10000000
min2 = 10000000
max1 = 0
max2 = 0
li = [int(x) for x in input().split()]
for a in li:
	if a < min2:
		min2 = a
		if min2 < min1:
			(min1, min2) = (min2, min1)
	if a > max2:
		max2 = a
		if max2 > max1:
			(max1, max2) = (max2, max1)
print(min(max2 - min1, max1 - min2))


n = int(input(''))
a = [int(x) for x in input().strip().split(' ')]
mi = min(a)
ma = max(a)
diff = ma - mi
for i in range(len(a)):
	if a[i] == mi:
		a.pop(i)
		break
diff1 = max(a) - min(a)
a.append(mi)
for i in range(len(a)):
	if a[i] == ma:
		a.pop(i)
		break
diff2 = max(a) - min(a)
result = min(diff1, diff2)
print(result)


n = int(input())
a = list(map(int, input().split()))
a.sort()

def stabilize(ar):
	if len(ar) < 2:
		return 0
	if ar[1] - ar[0] > ar[-1] - ar[-2]:
		ar.pop(0)
	else:
		ar.pop(-1)
	return max(a) - min(a)
print(stabilize(a))


(n, lst) = (int(input()), list(map(int, input().split())))
if n > 2:
	(mx1, mn1) = (max(lst), min(lst))
	mami = []
	mami.append(mx1 - mn1)
	del lst[lst.index(mx1)]
	del lst[lst.index(mn1)]
	(mx2, mn2) = (max(lst), min(lst))
	mami.append(mx1 - mn2)
	mami.append(mx2 - mn1)
	print(min(mami))
else:
	print(0)


n = int(input())
a = list(map(int, input().split()))
large = 0
large2 = 0
small = 1000000
small2 = 1000000
for i in range(n):
	if a[i] > large:
		large2 = large
		large = a[i]
	elif a[i] > large2:
		large2 = a[i]
	if a[i] < small:
		small2 = small
		small = a[i]
	elif a[i] < small2:
		small2 = a[i]
print(min(large2 - small, large - small2))


length = int(input())
arr = [int(x) for x in input().split()]
if length < 3:
	arr.sort()
	if arr[-1] - arr[-2] > arr[1] - arr[0]:
		print(arr[-2] - arr[0])
	else:
		print(arr[-1] - arr[1])
	exit()
maxis = max(arr)
arr.remove(maxis)
maxis_2 = max(arr)
minis = min(arr)
arr.remove(minis)
minis_2 = min(arr)
if maxis - maxis_2 > minis_2 - minis:
	print(maxis_2 - minis)
else:
	print(maxis - minis_2)


n = int(input())
a = input()
a = a.split()
a = [int(x) for x in a]
a.sort()
x = a[n - 2] - a[0]
y = a[n - 1] - a[1]
if x < y:
	print(x)
else:
	print(y)


n = int(input())
ll = list(map(int, input().split()))
g = max(ll)
ll.remove(g)
d = max(ll)
e = min(ll)
f = d - e
ll.append(g)
ll.remove(min(ll))
d = max(ll)
e = min(ll)
h = d - e
print(min(f, h))


def max_list(x):
	max_list = x[0]
	for i in x:
		if int(i) > int(max_list):
			max_list = i
	return max_list

def min_list(x):
	min_list = x[0]
	for i in x:
		if int(i) < int(min_list):
			min_list = i
	return min_list
n = int(input())
a = input().split()
b = []
for j in range(n):
	b.append(a[j])
a.remove(max_list(a))
b.remove(min_list(b))
x = int(max_list(a)) - int(min_list(a))
y = int(max_list(b)) - int(min_list(b))
if x < y:
	print(x)
else:
	print(y)


n = int(input())
numbers = list(map(int, input().split()))
numbers.sort()
if n < 3:
	result = 0
elif numbers[1] - numbers[0] > numbers[-1] - numbers[-2]:
	result = numbers[1:][-1] - numbers[1:][0]
else:
	result = numbers[:-1][-1] - numbers[:-1][0]
print(result)


n = int(input())
L = [int(s) for s in input().split()]
L.sort()
S = []
if n == 2:
	print(0)
else:
	print(min(max(L[:-1]) - min(L[:-1]), max(L[1:]) - min(L[1:])))


n = int(input())
a = list(map(int, input().split()))
max1 = max(a)
min1 = min(a)
a.remove(max1)
max2 = max(a)
ins2 = max2 - min1
b = []
b.append(ins2)
a.append(max1)
a.remove(min1)
min2 = min(a)
b.append(max1 - min2)
print(min(b))


n = int(input())
a = list(map(int, input().split()))
mx = smn = max(a[:2])
mn = smx = min(a[:2])
for q in a[2:]:
	if q > smx:
		smx = q
	if q < smn:
		smn = q
	if q > mx:
		smx = mx
		mx = q
	if q < mn:
		smn = mn
		mn = q
if mx - smx > smn - mn:
	print(smx - mn)
else:
	print(mx - smn)


import math
n = int(input())
arr = list(map(int, input().split()))
arr = sorted(arr)
xx = arr[0:n - 1]
yy = max(xx) - min(xx)
rr = arr[1:n]
tt = max(rr) - min(rr)
print(min(tt, yy))


def solve(A, n):
	if n == 1:
		return 0
	A = sorted(A)
	opt1 = A[-2] - A[0]
	opt2 = A[-1] - A[1]
	mn = min(opt1, opt2)
	return mn
n = int(input())
arr = input().split(' ')
A = [int(n) for n in arr]
print(solve(A, n))


n = int(input())
s = [int(x) for x in input().split()]
s.sort()
if len(s) == 2:
	print(0)
elif s[-1] - s[1] > s[-2] - s[0]:
	print(s[-2] - s[0])
else:
	print(s[-1] - s[1])


n = int(input())
arr = [int(x) for x in input().strip().split()]
arr = sorted(arr)
print(min(arr[-1] - arr[1], arr[-2] - arr[0]))


n = int(input())
a = list(map(int, input().split()))
(min_dif, max_dif) = (0, 0)
a.sort()
if n > 2:
	min_dif = a[1] - a[0]
	max_dif = a[-1] - a[-2]
	if max_dif > min_dif:
		a.pop(-1)
	else:
		a.pop(0)
	print(max(a) - min(a))
else:
	print(0)


n = int(input())
a = list(map(int, input().split()))
if n == 2:
	print(0)
else:
	a.sort()
	b = a[-1]
	c = a[1]
	d = a[-2]
	e = a[0]
	if b - c < d - e:
		print(b - c)
	else:
		print(d - e)


n = int(input())
A = list(map(int, input().split()))
A = sorted(A)
a = A[-1] - A[1]
b = A[-2] - A[0]
print(min(a, b))


n = int(input())
a = [int(x) for x in input().split()]
mini = min(a)
maxi = max(a)
score1 = 0
score2 = 0
a.remove(mini)
score1 = max(a) - min(a)
a.append(mini)
a.remove(maxi)
score2 = max(a) - min(a)
print(min(score1, score2))


n = int(input())
a = [int(x) for x in input().split()]
a.sort(reverse=True)
i = a[0] - a[len(a) - 2]
j = a[1] - a[len(a) - 1]
if i > j:
	print(j)
else:
	print(i)


a = int(input())
l = list(map(int, input().split()))
l.sort()
if len(l) == 2:
	print(0)
else:
	print(min(l[-1] - l[1], l[-2] - l[0]))


input()
line = list(map(int, input().split()))
line.sort()
print(min(abs(line[-2] - line[0]), abs(line[-1] - line[1])))


n = int(input())
s = list(map(int, input().split()))
s.sort()
r = s[-1] - s[-2]
l = s[1] - s[0]
if r > l:
	print(s[-2] - s[0])
else:
	print(s[-1] - s[1])


n = int(input())
l = list(map(int, input().split()))
(mx1, mn1) = (max(l), min(l))
l.remove(mx1)
l.remove(mn1)
if len(l) == 0:
	print(0)
else:
	(mx2, mn2) = (max(l), min(l))
	print(min(mx2 - mn1, mx1 - mn2))


length = int(input())
lst = list(map(int, input().split()))
lst = list(sorted(lst))
max1 = lst[-1]
max2 = lst[-2]
min1 = lst[1]
min0 = lst[0]
print(min(max1 - min1, max2 - min0))


n = int(input())
arr = list(map(int, input().split()))
arr.sort()
val1 = max(arr) - min(arr[1:])
val2 = max(arr[:n - 1]) - min(arr)
print(min(val1, val2))


t = int(input())
a = list(map(int, input().split()))
a.sort()
if len(a) <= 2:
	print(0)
else:
	p = abs(a[len(a) - 2] - a[0])
	a.reverse()
	q = abs(a[0] - a[len(a) - 2])
	print(min(p, q))


N = int(input())
aN = list(map(int, input().split()))
aN.sort()
print(min(aN[-1] - aN[1], aN[-2] - aN[0]))


import copy
l = int(input())
a = list(map(int, input().split()))
b = copy.deepcopy(a)
a.remove(max(a))
b.remove(min(b))
print(min(max(a) - min(a), max(b) - min(b)))


n = input()
list1 = input().split()
list2 = list(map(int, list1))

def GetMax(l):
	max = l[0]
	for i in range(0, len(l)):
		if l[i] > max:
			max = l[i]
	return max

def GetMin(l):
	min = l[0]
	for i in range(0, len(l)):
		if l[i] < min:
			min = l[i]
	return min
rawMax = GetMax(list2)
rawMin = GetMin(list2)
list3 = list2.copy()
list2.remove(rawMax)
max = GetMax(list2)
min = rawMin
a = max - min
list3.remove(rawMin)
max1 = GetMax(list3)
min1 = GetMin(list3)
b = max1 - min1
if a < b:
	print(a)
else:
	print(b)


a = int(input())
arr = [int(x) for x in input().split()]
arr.sort()
if arr[-2] - arr[0] < arr[-1] - arr[1]:
	print(arr[-2] - arr[0])
else:
	print(arr[-1] - arr[1])


nn = int(input())
l = [int(i) for i in input().split()]
if nn == 3:
	print(min([abs(l[0] - l[1]), abs(l[0] - l[2]), abs(l[1] - l[2])]))
elif nn == 2:
	print(0)
else:
	maxm = max(l)
	minm = min(l)
	l.remove(maxm)
	l.remove(minm)
	mx = max(l)
	mn = min(l)
	print(min([maxm - mn, mx - minm]))


n = int(input())
arr = [int(x) for x in input().split()]
arr.sort()
print(min(arr[len(arr) - 2] - arr[0], arr[len(arr) - 1] - arr[1]))


def go():
	n = int(input())
	a = [int(i) for i in input().split(' ')]
	if n == 2:
		return 0
	m1 = max(a)
	a.remove(m1)
	m2 = max(a)
	mi1 = min(a)
	a.remove(mi1)
	mi2 = min(a)
	if m2 - mi1 < m1 - mi2:
		return m2 - mi1
	return m1 - mi2
print(go())


n = int(input())
a = input().split()
a = [int(i) for i in a]
a.sort()
gap1 = a[n - 2] - a[0]
gap2 = a[n - 1] - a[1]
if gap1 < gap2:
	print(gap1)
else:
	print(gap2)


t = int(input())
l = list(map(int, input().split()))
l = sorted(l)
a = abs(l[0] - l[-2])
b = abs(l[-1] - l[1])
print(min(a, b))


a = int(input())
b = map(int, input().split(' '))
b = sorted(b)
if len(b) == 0:
	print(0)
elif int(b[1]) - int(b[0]) >= int(b[-1]) - int(b[-2]):
	print(int(b[-1]) - int(b[1]))
else:
	print(int(b[-2]) - int(b[0]))


n = int(input())
a = list(map(int, input().split()))
max_a = 1
min_a = 100000
for i in range(n):
	if a[i] <= min_a:
		min_a = a[i]
		i_min = i
	if a[i] >= max_a:
		max_a = a[i]
		i_max = i
a_senza_min = a[:i_min] + a[i_min + 1:]
a_senza_max = a[:i_max] + a[i_max + 1:]
st_a = max(a) - min(a)
st_a_senza_min = max(a_senza_min) - min(a_senza_min)
st_a_senza_max = max(a_senza_max) - min(a_senza_max)
risu = min(st_a, st_a_senza_min, st_a_senza_max)
print(risu)


a = int(input())
m = list(map(int, input().split()))
m1 = list(m)
m2 = list(m1)
m2.remove(max(m2))
m1.remove(min(m1))
print(min(max(m1) - min(m1), max(m2) - min(m2)))


n = int(input())
arr = list(map(int, input().strip().split()))
arr = sorted(arr)
ma = n - 1
mi = 0
ans = arr[ma] - arr[mi]
if n > 2:
	ans = min(ans, arr[ma - 1] - arr[mi])
	ans = min(ans, arr[ma] - arr[mi + 1])
else:
	ans = 0
print(ans)


N = int(input())
D = sorted([*map(int, input().split())])
print(min(D[len(D) - 1] - D[1], D[len(D) - 2] - D[0]))


n = int(input())
a = [int(i) for i in input().split()]
a = sorted(a)
a1 = a[n - 2] - a[0]
a2 = a[n - 1] - a[1]
res = min(a1, a2)
print(res)


from array import array
from sys import exit
n = int(input())
if n < 3:
	print(0)
	exit(0)
arr = array('L', map(int, input().split()))
(h1, l1, hi, li) = (arr[0], arr[0], 0, 0)
for i in range(1, n):
	if arr[i] > h1:
		(h1, hi) = (arr[i], i)
	if arr[i] < l1:
		(l1, li) = (arr[i], i)
(arr[hi], arr[li], h2, l2) = (0, 0, l1, h1)
for i in range(0, n):
	if arr[i] == 0:
		continue
	if arr[i] > h2:
		h2 = arr[i]
	if arr[i] < l2:
		l2 = arr[i]
if h1 - h2 > l2 - l1:
	print(h2 - l1)
else:
	print(h1 - l2)


n = int(input())
xs = list(map(int, str(input()).split(' ')))
a = min(xs)
b = max(xs)
x = xs.index(a)
y = xs.index(b)
ys = xs[0:x] + xs[x + 1:]
zs = xs[0:y] + xs[y + 1:]
print(min(max(ys) - min(ys), max(zs) - min(zs)))


import math, itertools, fractions, heapq, collections, bisect, sys, queue, copy
sys.setrecursionlimit(10 ** 7)
inf = 10 ** 20
mod = 10 ** 9 + 7
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]
ddn = [(-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1)]

def LI():
	return [int(x) for x in sys.stdin.readline().split()]

def I():
	return int(sys.stdin.readline())

def F():
	return float(sys.stdin.readline())

def LS():
	return sys.stdin.readline().split()

def S():
	return input()

def main():
	n = I()
	l = LI()
	l.sort()
	if n == 2:
		return 0
	return min(l[-1] - l[1], l[-2] - l[0])
print(main())


n = int(input())
a = list(map(int, input().split()))
a.sort()
if len(a) < 3:
	print(0)
else:
	print(min(a[-2] - a[0], a[-1] - a[1]))


_ = input()
a = [int(x) for x in input().split(' ')]
a.sort()
max_ = a[-1]
min_ = a[0]
max_2 = a[-2]
min_2 = a[1]
print(max_2 - min_ if max_2 - min_ < max_ - min_2 else max_ - min_2)


def solve():
	n = int(input())
	arr = [int(x) for x in input().split(' ')]
	if n == int(2):
		print(0)
		return 0
	arr.sort()
	max = arr[-2] - arr[0]
	max1 = arr[-1] - arr[1]
	print(min(max, max1))
solve()


n = int(input())
a = list(map(int, input().split()))
a.sort()
first = a[1] - a[0]
last = a[-1] - a[-2]
if first < last:
	print(a[-2] - a[0])
else:
	print(a[-1] - a[1])


n = int(input())
arr = [int(i) for i in input().split()]
if n == 2:
	print('0')
else:
	arr.sort()
	(x, y) = (arr[n - 1] - arr[1], arr[n - 2] - arr[0])
	print(min(x, y))


n = int(input())
arr = sorted(map(int, input().split()))
print(min(arr[-1] - arr[1], arr[-2] - arr[0]))


def func():
	n = int(input())
	a = list(map(int, input().split()))
	a.sort()
	print(min(a[-1] - a[1], a[-2] - a[0]))
func()


n = int(input())
inp = input().split()
arr = []
arr2 = []
for i in range(n):
	arr.append(int(inp[i]))
	arr2.append(arr[i])
arr.sort()
arr2.sort()
arr.pop(0)
instability = arr[-1] - arr[0]
arr2.pop(-1)
instability2 = arr2[-1] - arr2[0]
print(min(instability, instability2))


tamArray = int(input())
array = list(map(int, input().split()))
array.sort()
(max, min) = (array[tamArray - 1], array[0])
a = array[tamArray - 1] - array[1]
b = array[tamArray - 2] - array[0]
if tamArray <= 2:
	print(0)
elif a < b:
	print(a)
else:
	print(b)


qtd = int(input())
array = list(map(int, input().split()))
minimo = min(array)
maximo = max(array)
del array[array.index(maximo)]
maximo2 = max(array)
array.append(maximo)
del array[array.index(minimo)]
minimo2 = min(array)
instamin = maximo - minimo2
instamax = maximo2 - minimo
if instamax > instamin:
	print(instamin)
else:
	print(instamax)


n = int(input())
c = list(map(int, input().split()))
t1 = min(c)
t2 = max(c)
c.remove(t1)
l = max(c) - min(c)
c.append(t1)
c.remove(t2)
l2 = max(c) - min(c)
print(min(l, l2))


num = int(input())
lis = list(map(int, input().split()))
lis.sort()
w = len(lis)
if w > 1:
	ma2 = lis[w - 1] - lis[1]
	ma1 = lis[w - 2] - lis[0]
	print(min(ma1, ma2))
else:
	print('0')


n = int(input())
arr = list(map(int, input().split()))
min1 = 1000000
min2 = 1000000
max1 = 0
max2 = 0
for i in arr:
	if i < min1:
		min2 = min1
		min1 = i
	elif i < min2:
		min2 = i
	if i > max1:
		max2 = max1
		max1 = i
	elif i > max2:
		max2 = i
if min2 - min1 > max1 - max2:
	print(max1 - min2)
else:
	print(max2 - min1)


import sys
n = int(sys.stdin.readline().strip())
a = list(map(int, sys.stdin.readline().strip().split()))
a1 = a.copy()
a2 = a.copy()
a1.remove(max(a))
a2.remove(min(a))
print(min(max(a1) - min(a1), max(a2) - min(a2)))


x = eval(input())
y = list(map(int, input().split()))
y.sort()
m = y[x - 2] - y[0]
n = y[x - 1] - y[1]
print(min(m, n))


n = int(input())
l = [int(x) for x in input().split()]
l.sort()
s = []
for i in l:
	s.append(i)
l.remove(l[n - 1])
s.remove(s[0])
m1 = max(l) - min(l)
m2 = max(s) - min(s)
if m1 < m2:
	print(m1)
else:
	print(m2)


n = int(input())
a = []
a = list(map(int, input().split()))
if n <= 2:
	print(0)
else:
	a.sort()
	ab = a[n - 2] - a[0]
	abc = a[n - 1] - a[1]
	print(min(ab, abc))


try:
	n = int(input())
	a = [int(x) for x in input().split()]
	a.sort()
	print(min(a[n - 2] - a[0], a[n - 1] - a[0], a[n - 1] - a[1]))
except:
	pass


n = int(input())
a_list = [int(a) for a in input().split(' ')]
a_min = -1
a_max = 0
a_min_2 = -1
a_max_2 = 0
for a in a_list:
	if a > a_max:
		a_max_2 = a_max
		a_max = a
	elif a > a_max_2:
		a_max_2 = a
	if a_min == -1 or a < a_min:
		a_min_2 = a_min
		a_min = a
	elif a_min_2 == -1 or a < a_min_2:
		a_min_2 = a
if a_max - a_min_2 < a_max_2 - a_min:
	print(a_max - a_min_2)
else:
	print(a_max_2 - a_min)


from operator import itemgetter
n = int(input())
ai = list(map(int, input().split()))
ai.sort()
print(min(ai[-2] - ai[0], ai[-1] - ai[1]))


def main():
	(n, a) = (int(input()), list(map(int, input().split(' '))))
	min1 = min(a)
	max1 = max(a)
	a.remove(min1)
	min2 = min(a)
	a.append(min1)
	a.remove(max1)
	max2 = max(a)
	print(min(max1 - min2, max2 - min1))
main()


t = int(input())
s = input()
a = s.split()
for i in range(t):
	a[i] = int(a[i])
x = max(a)
y = min(a)
a.remove(x)
x1 = max(a)
s1 = x1 - y
a.append(x)
a.remove(y)
y1 = min(a)
a.append(y)
s2 = x - y1
print(min(s1, s2))


n = int(input())
arr = list(map(int, input().split()))
if n == 1 or n == 2:
	print(0)
else:
	arr.sort()
	print(min(arr[n - 2] - arr[0], arr[n - 1] - arr[1]))


n = int(input())
a = [int(i) for i in input().split()]
if n == 1:
	print(0)
elif len(a) == 2:
	print(0)
else:
	a = sorted(a)
	b = a[len(a) - 1] - a[0]
	c = a[len(a) - 2] - a[0]
	d = a[len(a) - 1] - a[1]
	print(min(b, min(c, d)))


import heapq
input()
L = list(map(int, input().split()))
heapq.heapify(L)
print(min(heapq.nlargest(2, L)[-1] - heapq.nsmallest(1, L)[0], heapq.nlargest(1, L)[0] - heapq.nsmallest(2, L)[-1]))


n = int(input())
s = [int(i) for i in input().split()]
if len(s) == 2:
	print(0)
	exit(0)
else:
	first_max = -1000
	second_max = -100
	i = 0
	while i < n:
		element = s[i]
		if element > first_max:
			(second_max, first_max) = (first_max, element)
		elif element > second_max:
			second_max = element
		i += 1
	first_min = 1000000
	second_min = 1000000
	i = 0
	while i < n:
		element = s[i]
		if element < first_min:
			(second_min, first_min) = (first_min, element)
		elif element < second_min:
			second_min = element
		i += 1
	q = second_max - min(s)
	w = max(s) - second_min
	print(min(q, w))


n = int(input())
s = input()
s = s.split(' ')
for i in range(len(s)):
	s[i] = int(s[i])
s.sort()
if n != 2:
	x = s[len(s) - 1] - s[1]
	y = s[len(s) - 2] - s[0]
	if x > y:
		print(y)
	else:
		print(x)
else:
	print(0)


n = int(input())
arr = sorted(list(map(int, input().split())))
diff_one = arr[-2] - arr[0]
diff_two = arr[-1] - arr[1]
print(min(diff_one, diff_two))


int(input())
l = list(map(int, input().split()))
(x, y) = (l[:], l[:])
del x[x.index(max(x))]
del y[y.index(min(y))]
print(min(max(x) - min(x), max(y) - min(y)))


import sys
import math
fileoperation = 0
if fileoperation:
	orig_stdout = sys.stdout
	orig_stdin = sys.stdin
	inputfile = open('W:/Competitive Programming/input.txt', 'r')
	outputfile = open('W:/Competitive Programming/output.txt', 'w')
	sys.stdin = inputfile
	sys.stdout = outputfile
mod = 1000000007

def nospace(l):
	ans = ''.join((str(i) for i in l))
	return ans
t = 1
for tt in range(t):
	n = int(input())
	a = list(map(int, sys.stdin.readline().split(' ')))
	a.sort()
	print(min(a[-2] - a[0], a[-1] - a[1]))
if fileoperation:
	sys.stdout = orig_stdout
	sys.stdin = orig_stdin
	inputfile.close()
	outputfile.close()


n = int(input())
str1 = list(map(int, input().split()))
str2 = max(str1)
str1.remove(max(str1))
a = max(str1) - min(str1)
str1.append(str2)
str3 = min(str1)
str1.remove(min(str1))
b = max(str1) - min(str1)
if a > b:
	print(b)
else:
	print(a)


num = int(input())
arr = list(map(int, input().split()))
arr = sorted(arr, reverse=False)
if arr[num - 1] != arr[num - 2] and arr[0] != arr[1]:
	print(min(arr[num - 2] - arr[0], arr[num - 1] - arr[1]))
elif arr[num - 1] == arr[num - 2]:
	print(arr[num - 1] - arr[1])
else:
	print(arr[num - 2] - arr[1])


import collections, atexit, math, sys, bisect
sys.setrecursionlimit(1000000)

def getIntList():
	return list(map(int, input().split()))
try:
	import numpy

	def dprint(*args, **kwargs):
		print(*args, file=sys.stderr)
	dprint('debug mode')
except Exception:

	def dprint(*args, **kwargs):
		pass
inId = 0
outId = 0
if inId > 0:
	dprint('use input', inId)
	sys.stdin = open('input' + str(inId) + '.txt', 'r')
if outId > 0:
	dprint('use output', outId)
	sys.stdout = open('stdout' + str(outId) + '.txt', 'w')
	atexit.register(lambda : sys.stdout.close())
(N,) = getIntList()
za = getIntList()
za.sort()
t = za[-1] - za[1]
t1 = za[-2] - za[0]
print(min(t, t1))


n = int(input())
li = [int(i) for i in input().split()]
li.sort()
if n == 2:
	print(0)
elif n == 3:
	print(min(li[2] - li[1], li[1] - li[0]))
else:
	print(min(li[-1] - li[1], li[-2] - li[0]))


import math
import sys
import itertools
import itertools

def sa(Type=int):
	return [Type(x) for x in input().split()]

def solve(t):
	n = int(input())
	arr = sa()
	arr.sort()
	if len(arr) == 2:
		print(0)
		return
	mnDiff = arr[1] - arr[0]
	mxDiff = arr[-1] - arr[-2]
	if mnDiff > mxDiff:
		print(arr[-1] - arr[1])
	else:
		print(arr[-2] - arr[0])
solve(1)


n = int(input())
a = input().split()
for i in range(n):
	a[i] = int(a[i])
a.sort()
ans = []
ans.append(a[-1] - a[1])
ans.append(a[-2] - a[0])
print(min(ans))


n = int(input())
p = input().rstrip().split(' ')
p.sort(key=int, reverse=True)
x = list(p)
del p[0]
f = int(p[0]) - int(p[len(p) - 1])
del x[len(x) - 1]
g = int(x[0]) - int(x[len(x) - 1])
print(min(f, g))


n = int(input())
a = list(map(int, input().rstrip().split()))
a.sort()
if n == 2:
	print('0')
	exit()
maxi = a.count(a[0])
mini = a.count(a[n - 1])
if maxi > 1 and mini > 1:
	print(a[n - 1] - a[0])
	exit()
if maxi == 1 and mini > 1:
	print(a[n - 1] - a[1])
	exit()
if maxi > 1 and mini == 1:
	print(a[n - 2] - a[0])
	exit()
if maxi == 1 and mini == 1:
	print(min(a[n - 1] - a[1], a[n - 2] - a[0]))


n = int(input())
m_list = list(map(int, input().split()))
new_list = sorted(m_list)[1:-1]
m_min = min(m_list)
m_max = max(m_list)
if len(new_list) == 0:
	print(0)
else:
	print(min(m_max - new_list[0], new_list[-1] - m_min))


import sys
n = int(input())
an = list(map(int, sys.stdin.readline().split()))
an.sort()
print(min(an[n - 2] - an[0], an[n - 1] - an[1]))


n = int(input())
inputs = input()
inlst = list(map(int, inputs.split(' ')))
if n == 2:
	print('0')
else:
	inlst = sorted(inlst)
	if inlst[-1] != inlst[-2] and inlst[0] != inlst[1]:
		a = max(inlst)
		b = min(inlst)
		inlst.remove(max(inlst))
		inlst.remove(min(inlst))
		diffmax = a - max(inlst)
		diffmin = min(inlst) - b
		if diffmax > diffmin:
			print(max(inlst) - b)
		else:
			print(a - min(inlst))
	elif inlst[-1] != inlst[-2] and inlst[0] == inlst[1]:
		inlst.remove(max(inlst))
		print(max(inlst) - min(inlst))
	elif inlst[-1] == inlst[-2] and inlst[0] != inlst[1]:
		inlst.remove(min(inlst))
		print(max(inlst) - min(inlst))
	else:
		print(max(inlst) - min(inlst))


n = int(input())
a = [int(j) for j in input().split()]
a.sort()
ans = min(a[n - 1] - a[1], a[n - 2] - a[0])
print(ans)


n = int(input())
a = list(map(int, input().split()))
a.sort()
b = len(a)
if a[1] - a[0] <= a[b - 1] - a[b - 2]:
	print(a[b - 2] - a[0])
else:
	print(a[b - 1] - a[1])


n = int(input())
y = input()
inlist = y.split(' ')
for ii in range(n):
	inlist[ii] = int(inlist[ii])
inlist.sort()
max_margin = inlist[-2] - inlist[0]
min_margin = inlist[-1] - inlist[1]
if max_margin > min_margin:
	print(min_margin)
else:
	print(max_margin)


n = int(input())
listN = [int(b) for b in input().split()]
listN1 = []
for num in listN:
	listN1.append(num)
max = listN[0]
for num in listN:
	if num > max:
		max = num
index = listN.index(max)
listN.pop(index)
min = listN[0]
max = listN[0]
for num in listN:
	if num < min:
		min = num
for num in listN:
	if num > max:
		max = num
instability1 = max - min
min = listN1[0]
for num in listN1:
	if num < min:
		min = num
index = listN1.index(min)
listN1.pop(index)
min = listN1[0]
max = listN1[0]
for num in listN1:
	if num < min:
		min = num
for num in listN1:
	if num > max:
		max = num
instability2 = max - min
if instability1 < instability2:
	print(instability1)
else:
	print(instability2)


n = int(input())
a = list(map(int, input().split()))
max1 = a.pop(a.index(max(a)))
min1 = a.pop(a.index(min(a)))
if n == 2:
	print(0)
elif n == 3:
	print(min(max1 - a[0], a[0] - min1))
else:
	max2 = a.pop(a.index(max(a)))
	min2 = a.pop(a.index(min(a)))
	print(min(max1 - min2, max2 - min1))


n = int(input())
a = list(map(int, input().split()))
b = a.copy()
a.remove(max(a))
mina = max(a) - min(a)
b.remove(min(b))
minb = max(b) - min(b)
if mina <= minb:
	print(mina)
else:
	print(minb)


n = int(input())
a = [int(i) for i in input().split()]
maxRem = a[:]
maxRem.remove(max(a))
minRem = a[:]
minRem.remove(min(a))
print(min(max(maxRem) - min(maxRem), max(minRem) - min(minRem)))


n = int(input())
a = [int(x) for x in input().split()]
a = sorted(a)
if n == 2:
	print(0)
	exit()
if n == 3:
	print(min(abs(a[0] - a[1]), abs(a[1] - a[2])))
	exit()
tcm = a[0]
tcma = a[n - 1]
tcm1 = a[1]
tcma1 = a[n - 2]
s1 = abs(a[0] - a[n - 2])
s2 = abs(a[1] - a[n - 1])
print(min(s1, s2))


n = int(input())
a = list(map(int, input().split()))[0:n]
x = a.count(max(a))
c = a.copy()
if x != 1:
	a.remove(min(a))
else:
	a.remove(max(a))
b = []
b.append(max(a) - min(a))
if x == 1:
	c.remove(min(c))
else:
	c.remove(max(c))
b.append(max(c) - min(c))
print(min(b))


n = int(input())
l = [int(x) for x in input().split()]
l.sort()
v1 = l[-1] - l[1]
v2 = l[-2] - l[0]
print(min(v1, v2))


n = int(input())
a = list(map(int, input().split()))
a.sort()
if n == 2:
	print(0)
	exit()
r = a[-1] - a[0]
b = a[-2] - a[0]
c = a[-1] - a[1]
print(min(min(r, b), c))


n = int(input())
mass = sorted(map(int, input().split()))
print(min(abs(mass[1] - mass[-1]), abs(mass[0] - mass[-2])))


n = int(input())
li = sorted(list(map(int, input().split())))
if li[1] - li[0] > li[-1] - li[-2]:
	del li[0]
else:
	del li[-1]
print(li[-1] - li[0])


a = int(input())
s = input()
l = list(map(int, s.split()))
l.sort()
for i in range(0, a):
	res = min(l[i - 1] - l[0], l[i] - l[1])
print(res)


n = int(input())
arr = input().split(' ')
arr = [int(s) for s in arr]
case_a = arr[:]
case_a.remove(max(arr))
inst_a = max(case_a) - min(case_a)
case_b = arr[:]
case_b.remove(min(arr))
inst_b = max(case_b) - min(case_b)
print(min([inst_a, inst_b]))


n = int(input())
ls = sorted(map(int, input().split()))
if n <= 2:
	print(0)
else:
	n1 = ls[0]
	n2 = ls[1]
	x1 = ls[-1]
	x2 = ls[-2]
	print(min(x2 - n1, x1 - n2))


try:
	n = int(input())
	l = list(map(int, input().split()))
	s = list(set(l))
	l1 = list(l)
	l2 = list(l)
	mi = l1.remove(min(s))
	ma = l2.remove(max(s))
	mi = max(l1) - min(l1)
	ma = max(l2) - min(l2)
	print(min(mi, ma))
except:
	pass


import sys
n_ints = int(sys.stdin.readline())
input_list = list(map(int, sys.stdin.readline().split(' ')))
input_list.sort(key=int)
max_value = max(input_list)
min_value = min(input_list)
min_bool = False
max_bool = False
try:
	if input_list[1] == min_value:
		min_bool = True
except:
	pass
try:
	if input_list[-2] == max_value:
		max_bool = True
except:
	pass
if min_bool == True:
	if max_bool == True:
		print(max_value - min_value)
	else:
		newlist = input_list[:-1]
		print(max(newlist) - min(newlist))
elif min_bool == False:
	if max_bool == True:
		newlist = input_list[1:]
		print(max(newlist) - min(newlist))
	else:
		list1 = input_list[1:]
		list2 = input_list[:-1]
		diff1 = max(list1) - min(list1)
		diff2 = max(list2) - min(list2)
		print(min(diff1, diff2))


from __future__ import division, print_function
import os
import sys
from io import BytesIO, IOBase
if sys.version_info[0] < 3:
	from __builtin__ import xrange as range
	from future_builtins import ascii, filter, hex, map, oct, zip

def main():
	pass
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

def print(*args, **kwargs):
	(sep, file) = (kwargs.pop('sep', ' '), kwargs.pop('file', sys.stdout))
	at_start = True
	for x in args:
		if not at_start:
			file.write(sep)
		file.write(str(x))
		at_start = False
	file.write(kwargs.pop('end', '\n'))
	if kwargs.pop('flush', False):
		file.flush()
if sys.version_info[0] < 3:
	(sys.stdin, sys.stdout) = (FastIO(sys.stdin), FastIO(sys.stdout))
else:
	(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()
n = int(input())
arr = [int(x) for x in input().split()]
arr.sort()
print(min(arr[len(arr) - 2] - arr[0], arr[len(arr) - 1] - arr[1]))


n = int(input())
b = list(map(int, input().split()))
a = sorted(b)
m = a[-2] - a[0]
x = a[-1] - a[1]
print(min(m, x))


input()
a = sorted(list(map(int, input().split())), reverse=True)
print(min(max(a[1:]) - min(a[1:]), max(a[::-1][1:]) - min(a[::-1][1:])))


from copy import copy

def solve():
	n = input()
	rd = lambda : list(map(int, input().split()))
	arr = rd()
	mi = 1000000.0
	ma = -1
	ima = imi = -1
	for (i, val) in enumerate(arr):
		if val > ma:
			ma = val
			ima = i
		if val < mi:
			mi = val
			imi = i
	ins = ma - mi
	tmp = copy(arr)
	del tmp[ima]
	ins = min(ins, max(tmp) - min(tmp))
	tmp = copy(arr)
	del tmp[imi]
	ins = min(ins, max(tmp) - min(tmp))
	print(ins)
solve()


n = int(input())
a = list(map(int, input().split()))
k1 = 0
mx = 0
k2 = 0
mn = 100000
for i in range(n):
	if a[i] >= mx:
		mx = a[i]
		k1 = i
for i in range(n):
	if a[i] <= mn:
		mn = a[i]
		k2 = i
a1 = a[:k1] + a[k1 + 1:]
a2 = a[:k2] + a[k2 + 1:]
print(min(max(a1) - min(a1), max(a2) - min(a2)))


n = int(input())
l = list(map(int, input().split()))
m = max(l)
mm = min(l)
l.remove(m)
l.remove(mm)
if l:
	if m - min(l) < max(l) - mm:
		print(m - min(l))
	else:
		print(max(l) - mm)
else:
	print('0')


from sys import stdin, stdout
import collections
import math
N = int(input())
arr = [int(x) for x in stdin.readline().split()]
arr.sort()
max1 = arr[-1]
max2 = arr[-2]
min1 = arr[0]
min2 = arr[1]
res = max(arr) - min(arr)
for i in range(N):
	if arr[i] == max1:
		d = max2 - min1
	elif arr[i] == min1:
		d = max1 - min2
	else:
		d = max1 - min1
	res = min(d, res)
print(res)


def GetList():
	return list(map(int, input().split()))

def main():
	n = int(input())
	arr = GetList()
	arr.sort()
	if arr[1] - arr[0] > arr[-1] - arr[-2]:
		del arr[0]
	else:
		del arr[-1]
	print(abs(arr[0] - arr[-1]))
main()


n = int(input())
a = list(map(int, input().split()))
x = max(a)
y = min(a)
a.remove(x)
r1 = max(a) - y
a.append(x)
a.remove(y)
r2 = x - min(a)
print(min(r1, r2))


a = int(input())
b = list(map(int, input().split()))
b.sort(reverse=True)
print(min(b[1] - b[-1], b[0] - b[-2]))


n = int(input())
if n > 2:
	lst = list(map(int, input().split()))
	lst.sort()
	res = max(lst) - min(lst)
	for i in range(2):
		res = int(min(res, lst[n + i - 2] - lst[i]))
	print(res)
if n == 2:
	lst = list(map(int, input().split()))
	print('0')


import collections
import math
try:
	n = int(input())
	l = list(map(int, input().split()))
	l = sorted(l)
	a = abs(l[0] - l[-2])
	b = abs(l[-1] - l[1])
	print(min(a, b))
except:
	pass


def stability(array):
	return max(array) - min(array)

def remove_to_stabilize(num, array):
	max_val = max(array)
	min_val = min(array)
	array.remove(max_val)
	max_array_stab = stability(array)
	array.append(max_val)
	array.remove(min_val)
	min_array_stab = stability(array)
	array.append(min_val)
	if max_array_stab > min_array_stab:
		array.remove(min_val)
	else:
		array.remove(max_val)
	return array
num = int(input())
array = [int(x) for x in input().split()]
removed = remove_to_stabilize(num, array)
print(stability(removed))


t = int(input())
p = list(map(int, input().split()))
q = list(set(p))
q.sort()
mo = len(q)
ins = 0
if t == 2:
	print(ins)
else:
	po = max(p)
	pi = min(p)
	l = p.count(po)
	m = p.count(pi)
	if l > 1 and m > 1:
		ins = po - pi
	elif l == 1 and m > 1:
		ins = q[mo - 2] - pi
	elif m == 1 and l > 1:
		ins = po - q[1]
	elif m == 1 and l == 1:
		ins = min(po - q[1], q[mo - 2] - pi)
	print(ins)


n = int(input())
s = list(map(int, input().split()))
M = max(s)
m = min(s)
if M == m:
	print(0)
else:
	m1 = M
	M1 = m
	cm = 0
	cM = 0
	for i in range(n):
		if s[i] < m1 and s[i] > m:
			m1 = s[i]
		if s[i] > M1 and s[i] < M:
			M1 = s[i]
		if s[i] == m:
			cm += 1
		if s[i] == M:
			cM += 1
	if cm > 1 and cM > 1:
		print(M - m)
	elif cm == 1 and cM > 1 and (M - m1 < M - m):
		print(M - m1)
	elif cM == 1 and cm > 1 and (M1 - m < M - m):
		print(M1 - m)
	elif cm == 1 and cM == 1:
		if M1 - m < M - m1:
			print(M1 - m)
		else:
			print(M - m1)


n = int(input())
a = list(map(int, input().split(' ')))
maxm = max(a)
minm = min(a)
a.remove(maxm)
maxm1 = max(a)
a.append(maxm)
a.remove(minm)
minm1 = min(a)
print(min(maxm1 - minm, maxm - minm1))


n = int(input())
ls = list(map(int, input().split()))
mx = max(ls)
mn = min(ls)
ls.sort()
ans = min(mx - ls[1], ls[n - 2] - mn)
print(ans)


n = int(input())
arr = input()
l = list(map(int, arr.split(' ')))
l.sort()
a = l[n - 2] - l[0]
b = l[n - 1] - l[1]
if a > b:
	print(b)
else:
	print(a)


n = int(input())
a = [int(x) for x in input().split(' ')]
if n > 2:
	M1 = max(a)
	m1 = min(a)
	a.remove(M1)
	a.remove(m1)
	M2 = max(a)
	m2 = min(a)
	ans = min(M1 - m2, M2 - m1)
else:
	ans = 0
print(ans)


n = int(input())
a = list(map(int, input().split()))
a.sort()
(p1, p2) = (a[-1], a[-2])
(q1, q2) = (a[0], a[1])
ans = 100000000000000000
for i in range(n):
	s1 = p1
	s2 = q1
	if a[i] == p1:
		s1 = p2
	elif a[i] == q1:
		s2 = q2
	ans = min(ans, s1 - s2)
print(ans)


n = int(input())
A = list(map(int, input().split()))
A = sorted(A)
small_top = A[0]
small_second = A[1]
large_top = A[-1]
large_second = A[-2]
ans = float('inf')
ans = min(ans, large_top - small_second)
ans = min(ans, large_second - small_top)
print(ans)


n = int(input())
massif = [int(a) for a in input().split()]
fmin = float('inf')
smin = float('inf')
fmax = float('-inf')
smax = float('-inf')
for element in massif:
	if element < fmin:
		if smin > fmin:
			smin = fmin
		fmin = element
	elif element < smin:
		if fmin > smin:
			fmin = smin
		smin = element
	if element > fmax:
		if smax < fmax:
			smax = fmax
		fmax = element
	elif element > smax:
		if fmax < smax:
			fmax = smax
		smax = element
print(min(smax - fmin, fmax - smin))


n = input()
n2 = int(n)
nstr = input()
nstr2 = nstr.split()
max1 = 0
max2 = 0
min1 = 100001
min2 = 100001
for x in nstr2:
	x_int = int(x)
	if x_int > max1:
		max2 = max1
		max1 = x_int
	elif x_int > max2:
		max2 = x_int
	if x_int < min1:
		min2 = min1
		min1 = x_int
	elif x_int < min2:
		min2 = x_int
print(min(max2 - min1, max1 - min2))


input()
lis = list(map(int, input().split()))
if len(lis) > 2:
	a_max = max(lis)
	a_min = min(lis)
	lis.remove(max(lis))
	lis.remove(min(lis))
	b_min = min(lis)
	b_max = max(lis)
	print(min(b_max - a_min, a_max - b_min))
else:
	print(0)


lenArray = int(input())
array = [int(i) for i in input().split()]
array.sort()
instability = max(array) - min(array)
print(min(max(array) - min(array[1:]), max(array[:-1]) - min(array)))


n = int(input())
a = list(map(int, input().split()))
a.sort()
large = a[n - 1]
small = a[0]
print(min(large - a[1], a[n - 2] - small))


n = int(input())
numbers = list(map(int, input().split()))
numbers.sort()
if n == 2:
	print(0)
else:
	upper = numbers[n - 1] - numbers[n - 2]
	lower = numbers[1] - numbers[0]
	if upper > lower:
		print(numbers[n - 2] - numbers[0])
	else:
		print(numbers[n - 1] - numbers[1])


n = int(input())
a = list(map(int, input().split()))
a.sort()
res1 = a[n - 2] - a[0]
res2 = a[n - 1] - a[1]
print(min(res1, res2))


def ans(n, arr):
	arr = sorted(arr)
	if n <= 2:
		return 0
	else:
		a1 = max(arr[1:]) - min(arr[1:])
		a2 = max(arr[:n - 1]) - min(arr[:n - 1])
		return min(a1, a2)
size = int(input())
arr = list(map(int, input().split()))
print(ans(size, arr))


n = int(input())
s = list(map(int, input().split()))

def sol(n, s):
	if len(s) == 2:
		return 0
	s.sort(reverse=True)
	return min(s[0] - s[-2], s[1] - s[-1])
print(sol(n, s))


length = int(input())
a = list(map(int, input().split()))
if len(a) == 2:
	print(0)
else:
	a.sort()
	v1 = a[-2] - a[0]
	v2 = a[-1] - a[1]
	print(min(v1, v2))


n = int(input())
arr = sorted(list(map(int, input().split())))
(diff1, diff2) = (arr[1] - arr[0], arr[-1] - arr[-2])
if diff1 > diff2:
	print(arr[-1] - arr[1])
else:
	print(arr[-2] - arr[0])


import sys
from sys import stdin
inf = 1000000000

def solve():
	n = int(input())
	a = list(map(int, input().split()))
	max_pref = [0] * n
	max_suff = [0] * n
	min_pref = [0] * n
	min_suff = [0] * n
	for i in range(n):
		max_pref[i] = max(max_pref[i - 1] if i > 0 else -inf, a[i])
		min_pref[i] = min(min_pref[i - 1] if i > 0 else inf, a[i])
	for i in range(n - 1, 0, -1):
		max_suff[i] = max(max_suff[i + 1] if i < n - 1 else -inf, a[i])
		min_suff[i] = min(min_suff[i + 1] if i < n - 1 else inf, a[i])
	ans = inf
	for i in range(n):
		mx = max(max_pref[i - 1] if i > 0 else -inf, max_suff[i + 1] if i < n - 1 else -inf)
		mi = min(min_pref[i - 1] if i > 0 else inf, min_suff[i + 1] if i < n - 1 else inf)
		ans = min(ans, mx - mi)
	print(ans)

def main():
	solve()
main()


n = int(input())
a = [int(x) for x in input().split(' ')]
if len(a) == 1:
	print(0)
else:
	a.sort()
	in1 = a[len(a) - 2] - a[0]
	in2 = a[len(a) - 1] - a[1]
	print(min(in1, in2))


maara = int(input())
x = input()
numerot = x.split()
numerolista = []
for numero in numerot:
	numerolista.append(int(numero))
nrolista = sorted(numerolista)
erotus1 = int(nrolista[1] - nrolista[0])
erotus2 = int(nrolista[-1] - nrolista[-2])
if erotus1 < erotus2:
	del nrolista[-1]
else:
	del nrolista[0]
erotus3 = nrolista[-1] - nrolista[0]
print(erotus3)


n = int(input())
arr = [int(k) for k in input().split()]
dic = {}
for i in range(n):
	if arr[i] not in dic:
		dic[arr[i]] = 1
	else:
		dic[arr[i]] += 1
mn = min(arr)
mx = max(arr)
arr.sort()
if dic[mx] == 1 and dic[mn] == 1:
	print(min(arr[n - 1] - arr[1], arr[n - 2] - arr[0]))
elif dic[mx] > 1 and dic[mn] == 1:
	print(arr[n - 1] - arr[1])
elif dic[mx] == 1 and dic[mn] > 1:
	print(arr[n - 2] - arr[0])
else:
	print(mx - mn)


n = int(input())
a = list(map(int, input().split()))
max12 = [-float('inf'), -float('inf')]
min12 = [float('inf'), float('inf')]
for u in a:
	if u > min(max12):
		max12[max12.index(min(max12))] = u
	if u < max(min12):
		min12[min12.index(max(min12))] = u
print(min(min(max12) - min(min12), max(max12) - max(min12)))


n = int(input())
A = sorted([int(a) for a in input().split()])
a = abs(A[1] - A[n - 1])
b = abs(A[0] - A[n - 1])
c = abs(A[0] - A[n - 2])
a = min(a, b)
print(min(a, c))


import sys
input = sys.stdin.readline

def swaparr(arr, a, b):
	temp = arr[a]
	arr[a] = arr[b]
	arr[b] = temp

def gcd(a, b):
	if b == 0:
		return a
	return gcd(b, a % b)

def nCr(n, k):
	if k > n - k:
		k = n - k
	res = 1
	for i in range(k):
		res = res * (n - i)
		res = res / (i + 1)
	return int(res)

def upper_bound(a, x, lo=0):
	hi = len(a)
	while lo < hi:
		mid = (lo + hi) // 2
		if a[mid] < x:
			lo = mid + 1
		else:
			hi = mid
	return lo

def primefs(n):
	primes = {}
	while n % 2 == 0 and n > 0:
		primes[2] = primes.get(2, 0) + 1
		n = n // 2
	for i in range(3, int(n ** 0.5) + 2, 2):
		while n % i == 0 and n > 0:
			primes[i] = primes.get(i, 0) + 1
			n = n // i
	if n > 2:
		primes[n] = primes.get(n, 0) + 1
	return primes

def power(x, y, p):
	res = 1
	x = x % p
	if x == 0:
		return 0
	while y > 0:
		if y & 1 == 1:
			res = res * x % p
		y = y >> 1
		x = x * x % p
	return res

def swap(a, b):
	temp = a
	a = b
	b = temp
	return (a, b)

def find(x, link):
	p = x
	while p != link[p]:
		p = link[p]
	while x != p:
		nex = link[x]
		link[x] = p
		x = nex
	return p

def union(x, y, link, size):
	x = find(x, link)
	y = find(y, link)
	if size[x] < size[y]:
		(x, y) = swap(x, y)
	if x != y:
		size[x] += size[y]
		link[y] = x

def sieve(n):
	prime = [True for i in range(n + 1)]
	p = 2
	while p * p <= n:
		if prime[p] == True:
			for i in range(p * p, n + 1, p):
				prime[i] = False
		p += 1
	return prime
MAXN = int(1000000.0 + 5)

def spf_sieve():
	spf[1] = 1
	for i in range(2, MAXN):
		spf[i] = i
	for i in range(4, MAXN, 2):
		spf[i] = 2
	for i in range(3, ceil(MAXN ** 0.5), 2):
		if spf[i] == i:
			for j in range(i * i, MAXN, i):
				if spf[j] == j:
					spf[j] = i

def factoriazation(x):
	ret = {}
	while x != 1:
		ret[spf[x]] = ret.get(spf[x], 0) + 1
		x = x // spf[x]
	return ret

def int_array():
	return list(map(int, input().strip().split()))

def str_array():
	return input().strip().split()
MOD = int(1000000000.0) + 7
CMOD = 998244353
INF = float('inf')
NINF = -float('inf')
n = int(input())
a = sorted(int_array())
if n == 2:
	print(0)
else:
	(x, y) = (a[0], a[-1])
	(c, d) = (a[1], a[-2])
	ans = min(abs(c - y), abs(x - y), abs(d - x))
	print(ans)


n = int(input())
a = list(map(int, input().split()))
l1 = max(a)
a.remove(l1)
k1 = max(a) - min(a)
a.append(l1)
l1 = min(a)
a.remove(l1)
k2 = max(a) - min(a)
if k2 <= k1:
	print(k2)
else:
	print(k1)


n = int(input())
A = [int(i) for i in input().split()]
A.sort()
print(min(A[-2] - A[0], A[-1] - A[1]))


n = int(input())
s = [int(i) for i in input().split()]
s1 = s[:]
s2 = s[:]
s1.remove(max(s1))
s2.remove(min(s2))
dif_s1 = max(s1) - min(s1)
dif_s2 = max(s2) - min(s2)
if dif_s1 > dif_s2:
	print(dif_s2)
else:
	print(dif_s1)


n = int(input())
a = sorted([int(s) for s in input().split()])
print(min(a[n - 1] - a[1], a[n - 2] - a[0]))


n = int(input())
a = list(map(int, input().split()))
a.sort()
x = a[0]
y = a[-1]
cx = a.count(x)
cy = a.count(y)
if cx > 1 and cy > 1:
	print(y - x)
elif cx > 1:
	print(a[-2] - x)
elif cy > 1:
	print(y - a[1])
else:
	print(min(a[-2] - x, y - a[1]))


def ans(arr):
	arr.sort()
	return min(arr[-2] - arr[0], arr[-1] - arr[1])
n = int(input())
a = list(map(int, input().strip().split()))
print(ans(a))


import sys
inf = float('inf')
mod = 1000000007

def get_array():
	return list(map(int, sys.stdin.readline().split()))

def get_ints():
	return map(int, sys.stdin.readline().split())

def input():
	return sys.stdin.readline()

def main():
	n = int(input())
	arr = get_array()
	arr.sort()
	print(min(arr[-2] - arr[0], arr[-1] - arr[1]))
main()


x = input()
y = list(map(int, input().split()))
y.sort()
print(min(y[len(y) - 1] - y[1], y[len(y) - 2] - y[0]))


from collections import deque, Counter, OrderedDict
from heapq import nsmallest, nlargest

def binNumber(n, size):
	return bin(n)[2:].zfill(size)

def gcd(a, b):
	if a == 0:
		return b
	return gcd(b % a, a)

def iar():
	return list(map(int, input().split()))

def ini():
	return int(input())

def isp():
	return map(int, input().split())

def sti():
	return str(input())
n = ini()
if n == 2:
	print(0)
	quit()
a = iar()
a.sort()
d1 = a[1] - a[0]
d2 = a[n - 1] - a[n - 2]
if d1 > d2:
	print(a[n - 1] - a[1])
else:
	print(a[n - 2] - a[0])


n = int(input())
l = [int(x) for x in input().split()]
if n == 2 or n == 1:
	print('0')
else:
	m1 = max(l)
	m2 = min(l)
	l.remove(m1)
	m3 = max(l)
	l.remove(m2)
	m4 = min(l)
	print(min(m1 - m4, m3 - m2))


n = int(input())
l = list(map(int, input().split()))
b = l[:]
c = l[:]
m = max(l)
n = min(l)
b.remove(m)
c.remove(n)
x = max(b) - min(b)
y = max(c) - min(c)
if x > y:
	print(y)
elif len(l) == 2:
	print('0')
else:
	print(x)


import math

def stabilize(arr):
	arr.sort()
	if len(arr) < 3:
		return 0
	if arr[1] - arr[0] > arr[-1] - arr[-2]:
		return arr[-1] - arr[1]
	else:
		return arr[-2] - arr[0]
input()
arr = list(map(int, input().strip().split()))
print(stabilize(arr))


n = int(input())
a = [int(i) for i in input().split()]
b = a.copy()
a[a.index(max(a))] = min(a)
b[b.index(min(b))] = max(b)
if max(a) - min(a) > max(b) - min(b):
	print(max(b) - min(b))
else:
	print(max(a) - min(a))


t = int(input())
x = [int(q) for q in input().split()]
x = sorted(x)
a1 = x[-2] - x[0]
a2 = x[-1] - x[1]
print(min(a1, a2))


n = int(input())
a = input().split()
a = [int(x) for x in a]
a.sort()
l = len(a)
print(min(a[l - 2] - a[0], a[l - 1] - a[1]))


n = int(input())
a = list(map(int, input().split()))

def solution():
	if n == 2:
		return 0
	mx = max(a)
	mn = min(a)
	a.remove(mx)
	d1 = max(a) - min(a)
	a.append(mx)
	a.remove(mn)
	d2 = max(a) - min(a)
	return min(d1, d2)
print(solution())


n = int(input())
a = []
a = [int(x) for x in input().split()]
a.sort()
a1 = a[n - 2] - a[0]
a2 = a[n - 1] - a[1]
if a1 > a2:
	print(a2)
else:
	print(a1)


n = int(input())
a = list(map(int, input().split()))
a.sort(reverse=True)
x = a[0] - a[n - 2]
y = a[1] - a[n - 1]
if n < 3:
	print('0')
if n > 2:
	if x > y:
		print(y)
	else:
		print(x)


n = int(input())
arr = list(input().split(' '))
arr = [int(arr_element) for arr_element in arr]
arr.sort()
if n == 2:
	print(0)
else:
	first_min = arr[0]
	second_min = first_min
	first_max = first_min
	second_max = first_max
	initial_instability = max(arr) - min(arr[1:])
	for num in arr:
		if num <= first_min:
			first_min = num
		elif num >= first_max:
			second_max = first_max
			first_max = num
	if second_max - first_min < initial_instability:
		initial_instability = second_max - first_min
	print(initial_instability)


input()
n = sorted(map(int, input().split()))
if len(n) <= 2:
	print(0)
else:
	k = n[1:]
	t = n[:len(n) - 1]
	if max(k) - min(k) <= max(t) - min(t):
		print(max(k) - min(k))
	else:
		print(max(t) - min(t))


import math

def modi(a, m):
	return pow(a, m - 2, m)
n = int(input())
arr = [int(x) for x in input().split()]
arr.sort()
cur = arr[-1] - arr[0]
if n > 2:
	cur = min(cur, arr[-1] - arr[1])
	cur = min(cur, arr[-2] - arr[0])
if n == 2:
	cur = 0
print(cur)


n = int(input())
array = input()
array = array.split(' ')
array = [int(x) for x in array]
array.sort()
if len(array) > 2:
	greatest = max(array)
	array.pop(-1)
	secondGreat = max(array)
	smallest = min(array)
	array.pop(0)
	secondSmallest = min(array)
	inst1 = secondGreat - smallest
	inst2 = greatest - secondSmallest
	print(min([inst1, inst2]))
else:
	print(0)


n = int(input())
lis = list(map(int, input().split()))
maxi = max(lis)
mini = min(lis)
lis.remove(maxi)
pmaxi = max(lis)
lis.append(maxi)
lis.remove(mini)
pmini = min(lis)
print(min(pmaxi - mini, maxi - pmini))


n = int(input())
arr = input().split()
(min1, min2, max1, max2) = (float('inf'), float('inf'), 0, 0)
for n in arr:
	n = int(n)
	if n > max1:
		(max1, max2) = (n, max1)
	elif n > max2:
		max2 = n
	if n < min1:
		(min1, min2) = (n, min1)
	elif n < min2:
		min2 = n
print(min(max2 - min1, max1 - min2))


n = int(input())
lst = list(map(int, input().strip().split(' ')))
lst = sorted(lst)
ma = lst[-1]
ma2 = lst[-2]
mi = lst[0]
mi2 = lst[1]
d = ma - mi
d1 = ma - mi2
d2 = ma2 - mi
if d >= d1 and d2 >= d1:
	print(d1)
elif d >= d2 and d1 >= d2:
	print(d2)


n = int(input())
l = list(map(int, input().split()))
b = []
for i in l:
	b.append(i)
ans = max(l) - min(l)
l.remove(max(l))
ans1 = max(l) - min(l)
b.remove(min(b))
ans2 = max(b) - min(b)
print(min(ans, ans1, ans2))


n = int(input())
arr = list(map(int, input().split()))
arr.sort()
left = arr[1] - arr[0]
right = arr[-1] - arr[-2]
if right > left:
	print(arr[-2] - arr[0])
elif left > right:
	print(arr[-1] - arr[1])
elif left == right:
	print(arr[-1] - arr[1])


x = int(input())
numbers = list(map(int, input().split()))
setter = list(numbers)
setter.sort()
if len(setter) <= 2:
	print(0)
else:
	ans1 = max(setter[:-1])
	ans1 = ans1 - min(setter)
	ans2 = min(setter[1:])
	ans2 = setter[-1] - ans2
	print(min(ans1, ans2))


n = int(input())
a = list(map(int, input().split()))
a.sort()
ans = min(a[-2] - a[0], a[-1] - a[1])
print(str(ans))


n = int(input())
nums = list(map(int, input().split()))
nums.sort()
left = 0
right = n - 1
ins = nums[-1] - nums[0]
ins1 = nums[right - 1] - nums[left]
ins2 = nums[right] - nums[left + 1]
if ins1 < ins2:
	ins = ins1
	right -= 1
else:
	ins = ins2
	left += 1
print(ins)


n = int(input())
s = sorted(list([int(i) for i in input().split(' ')]))
if len(s) < 3:
	print(0)
else:
	print(min(s[-1] - s[1], s[-2] - s[0]))


def instability(vec):
	(indMax, valMax) = (vec.index(max(vec)), max(vec))
	(indMin, valMin) = (vec.index(min(vec)), min(vec))
	return (valMax - valMin, indMax, indMin, valMax, valMin)

def main():
	n = int(input())
	vec = [int(x) for x in input().split()]
	(_, indMax, indMin, _, _) = instability(vec)
	vecMax = []
	vecMin = []
	for i in range(n):
		if i != indMax:
			vecMax.append(vec[i])
		if i != indMin:
			vecMin.append(vec[i])
	(instMax, _, _, _, _) = instability(vecMax)
	(instMin, _, _, _, _) = instability(vecMin)
	print(min(instMax, instMin))
main()


def instability(y):
	return max(y) - min(y)
n = int(input())
numbers = [int(i) for i in input().split()]
numbers.sort()
if n == 2:
	print(0)
else:
	a = numbers[0:n - 1]
	b = numbers[1:n]
	if instability(a) > instability(b):
		print(instability(b))
	else:
		print(instability(a))


c = int(input())
a = list(map(int, input().split()))[:]
a = sorted(a)
m = a[c - 1] - a[1]
n = a[c - 2] - a[0]
if m > n:
	print(n)
else:
	print(m)


x = input()
y = list(map(int, input().strip().split()))
(ma, mi) = (max(y), min(y))
set1 = tuple(y)
for i in range(2):
	if i == 0:
		a = list(y)
		a.remove(ma)
		nma = max(a)
		z = nma - mi
	else:
		y.remove(mi)
		nmi = min(y)
		w = ma - nmi
print(min(z, w))


n = int(input())
a = list(map(int, input().split()))
b = max(a)
c = min(a)
a.remove(b)
a.remove(c)
if len(a) == 0:
	print(0)
elif max(a) - c < b - min(a):
	print(max(a) - c)
else:
	print(b - min(a))


n = int(input())
a = map(int, input().split())
a = list(a)
a.sort()
p = a[n - 2] - a[0]
q = a[n - 1] - a[1]
if p > q:
	print(q)
else:
	print(p)


import math
import os
import random
import re
import sys

def main():
	n = int(input())
	a = [int(x) for x in input().split()]
	a.sort()
	if n == 2:
		print(0)
	elif n == 3:
		print(min(a[2] - a[1], min(a[1] - a[0], a[2] - a[0])))
	else:
		print(min(a[n - 1] - a[1], a[n - 2] - a[0]))
main()


n = int(input())
vector = input().split()
for i in range(n):
	vector[i] = int(vector[i])
vector.sort()
if n == 2:
	print(0)
else:
	ans = vector[-1] - vector[1]
	if vector[-2] - vector[0] < ans:
		ans = vector[-2] - vector[0]
	print(ans)


kol = int(input())
numbers = input().split(' ')
for i in range(0, kol):
	numbers[i] = int(numbers[i])
numbers = sorted(numbers)
if numbers[-1] - numbers[-2] > numbers[1] - numbers[0]:
	print(numbers[-2] - numbers[0])
else:
	print(numbers[-1] - numbers[1])


input()
a = list(map(int, input().split()))
ma = max(a)
mi = min(a)
orig = ma - mi
b = a.copy()
b.remove(mi)
ans = ma - min(b)
b = a.copy()
b.remove(ma)
if max(b) - mi < ans:
	ans = max(b) - mi
print(ans)


a = int(input())
list1 = list(map(int, input().split()))
list2 = list1[:]
list3 = list1[:]
s1 = min(list1)
s2 = max(list1)
list2.remove(s1)
list3.remove(s2)
ans = s2 - s1
s3 = min(list2)
s4 = max(list2)
s5 = min(list3)
s6 = max(list3)
ans1 = s4 - s3
ans2 = s6 - s5
a2 = min(ans, ans1, ans2)
print(a2)


n = int(input())
a = [int(v) for v in input().split()]
b = a.copy()
(init_max, init_min) = (max(a), min(a))
for (j, i) in enumerate(a):
	if i == init_max:
		a[j] = init_min
		break
init_max = max(a)
(_max, _min) = (max(b), min(b))
for (j, i) in enumerate(b):
	if i == _min:
		b[j] = _max
		break
_min = min(b)
print(min(init_max - init_min, _max - _min))


f = lambda : map(int, input().split())
n = int(input())
l = sorted(f())
print(0 if n == 2 else min(l[-2] - l[0], l[-1] - l[1]))


x = int(input())
l = list(map(int, input().split()))
l.sort()
print(min(l[x - 1] - l[1], l[x - 2] - l[0]))


n = int(input())
a = [int(x) for x in input().split()]
mx = max(a)
mn = min(a)
a.remove(mx)
one = max(a) - min(a)
a.append(mx)
a.remove(mn)
two = max(a) - min(a)
print(min(one, two))


n = int(input())
a = list(map(int, input().split()))
min_1 = max_2 = min(a[0], a[1])
min_2 = max_1 = max(a[0], a[1])
for i in range(2, n):
	if a[i] <= min_1:
		min_2 = min_1
		min_1 = a[i]
	elif a[i] < min_2:
		min_2 = a[i]
	if a[i] >= max_1:
		max_2 = max_1
		max_1 = a[i]
	elif a[i] > max_2:
		max_2 = a[i]
print(min(max_2 - min_1, max_1 - min_2))


n = int(input())
a = list(map(int, input().split()))
a.sort()
left = a[-1] - a[1]
right = a[-2] - a[0]
if left > right:
	print(right)
else:
	print(left)


l = input()
a = [int(v) for v in input().split(' ')]
a.sort()
print(min(a[-2] - a[0], a[-1] - a[1]))


def mi():
	return map(int, input().split(' '))
n = int(input())
li = list(mi())
li.sort()
if n == 2:
	print(0)
else:
	min1 = min(li[1:])
	max1 = max(li[1:])
	max2 = max(li[:len(li) - 1])
	min2 = min(li[:len(li) - 1])
	ans = min(max2 - min2, max1 - min1)
	print(ans)


input()
a = list(map(int, input().split()))
a.sort()

def calc(x):
	return max(x) - min(x)
print(min(calc(a[:-1]), calc(a[1:])))


n = int(input())
t = [int(x) for x in input().split()]

def min_max(t, a):
	joker = t.copy()
	joker.remove(a)
	return max(joker) - min(joker)
a = max(t)
b = min(t)
if min_max(t, a) < min_max(t, b):
	print(min_max(t, a))
else:
	print(min_max(t, b))


n = int(input())
arr = list(map(int, input().split()))
arr = list(sorted(arr))
if n >= 4:
	if arr[1] - arr[0] >= arr[n - 1] - arr[n - 2]:
		print(arr[n - 1] - arr[1])
	else:
		print(arr[n - 2] - arr[0])
elif n == 2:
	print(0)
else:
	print(min(arr[1] - arr[0], arr[2] - arr[1]))


z = input
from math import *
n = int(z())
l = sorted(list(map(int, z().split())))
(t, t1, t3) = (0, 0, 0)
t = l[-1] - l[0]
try:
	t1 = l[-2] - l[0]
except:
	pass
try:
	t3 = l[-1] - l[1]
except:
	pass
print(min(t, t1, t3))


n = int(input())
arr = list(map(int, input().split()))
arr.sort()
t1 = arr[:n - 1]
min1 = min(t1)
max1 = max(t1)
diff1 = max1 - min1
t2 = arr[1:]
min2 = min(t2)
max2 = max(t2)
diff2 = max2 - min2
print(min(diff1, diff2))


n = int(input())
l = list(map(int, input().split()))
mx = max(l)
mn = min(l)
if l.count(mx) >= 2:
	l.remove(mn)
	print(max(l) - min(l))
elif l.count(mn) >= 2:
	l.remove(mx)
	print(max(l) - min(l))
else:
	wo_max = list(filter(lambda a: a != mx, l))
	x = max(wo_max) - min(wo_max)
	wo_min = list(filter(lambda a: a != mn, l))
	y = max(wo_min) - min(wo_min)
	if x > y:
		print(y)
	else:
		print(x)


import sys
sys.setrecursionlimit(10 ** 6)

def main():
	nbEntrees = int(input())
	nb = list(map(int, input().split()))
	maximum = max(nb)
	nb.remove(maximum)
	etendue = max(nb) - min(nb)
	nb.append(maximum)
	maximum = min(nb)
	nb.remove(maximum)
	etendue = min(etendue, max(nb) - min(nb))
	print(etendue)
main()


from sys import stdin, stdout
from math import *
from heapq import *
from collections import *

def main():
	n = int(stdin.readline())
	a = [int(x) for x in stdin.readline().split()]
	a.sort()
	res = a[n - 1] - a[0]
	res = min(res, a[n - 1] - a[1], a[n - 2] - a[0])
	stdout.write(str(res))
	return 0
main()


n = input()
string = list(input().split(' '))
string = list(map(lambda x: int(x), string))
string.sort()
string1 = string[1:]
string2 = string[:-1]
inst1 = string1[-1] - string1[0]
inst2 = string2[-1] - string2[0]
if inst1 < inst2:
	print(inst1)
else:
	print(inst2)


n = int(input())
m = list(map(int, input().split()))
m = sorted(m)
m1 = sorted(m, reverse=True)
m.pop()
m1.pop()
print(min(max(m) - min(m), max(m1) - min(m1)))


n = int(input())
l = list(map(int, input().split()))
p = l[:]
mi = max(l)
mn = min(p)
l.pop(l.index(mi))
p.pop(p.index(mn))
print(min(max(l) - min(l), max(p) - min(p)))


from __future__ import division, print_function
from fractions import Fraction
import sys
import os
from io import BytesIO, IOBase
from itertools import *
import bisect
from heapq import *
from math import *
from copy import *
from collections import deque
from collections import Counter as counter
from itertools import combinations as comb
from itertools import permutations as permutate
from bisect import bisect_left as bl
from bisect import bisect_right as br
from bisect import bisect
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

def print(*args, **kwargs):
	(sep, file) = (kwargs.pop('sep', ' '), kwargs.pop('file', sys.stdout))
	at_start = True
	for x in args:
		if not at_start:
			file.write(sep)
		file.write(str(x))
		at_start = False
	file.write(kwargs.pop('end', '\n'))
	if kwargs.pop('flush', False):
		file.flush()
if sys.version_info[0] < 3:
	(sys.stdin, sys.stdout) = (FastIO(sys.stdin), FastIO(sys.stdout))
else:
	(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
from types import GeneratorType

def iterative(f, stack=[]):

	def wrapped_func(*args, **kwargs):
		if stack:
			return f(*args, **kwargs)
		to = f(*args, **kwargs)
		while True:
			if type(to) is GeneratorType:
				stack.append(to)
				to = next(to)
				continue
			stack.pop()
			if not stack:
				break
			to = stack[-1].send(to)
		return to
	return wrapped_func
mod = 1000000007

def inp():
	return sys.stdin.readline().rstrip('\r\n')

def out(var):
	sys.stdout.write(str(var))

def lis():
	return list(map(int, inp().split()))

def stringlis():
	return list(map(str, inp().split()))

def sep():
	return map(int, inp().split())

def strsep():
	return map(str, inp().split())

def zerolist(n):
	return [0] * n

def nextline():
	out('\n')

def testcase(t):
	for p in range(t):
		solve()

def printlist(a):
	for p in range(0, len(a)):
		out(str(a[p]) + ' ')

def solve():
	n = int(inp())
	ar = lis()
	ar.sort()
	k1 = ar[-1] - ar[1]
	k2 = ar[-2] - ar[0]
	print(min(k1, k2))
solve()


t = int(input())
a = list(map(int, input().split()))
t = 0
a.sort()
x = a[len(a) - 2]
y = a[0]
z = a[1]
w = a[len(a) - 1]
if x - y > w - z:
	print(w - z)
else:
	print(x - y)


n = int(input())
g = list(map(int, input().split()))
g.sort()
sumi = sum(g)
if n - 2 < 0:
	my = g[0]
else:
	my = g[n - 2]
mx = g[0]
a = g[n - 1] - g[1]
b = my - mx
if a > b:
	print(b)
else:
	print(a)


n = int(input())
a = sorted([int(x) for x in input().split()])
if n == 2:
	print(0)
else:
	mn = abs(a[0] - a[n - 2])
	mx = abs(a[1] - a[n - 1])
	print(min(mn, mx))


n = int(input())
lst = list(map(int, input().split()))
lst.sort()
ans = 0
if n > 2:
	ans = min(lst[-2] - lst[0], lst[-1] - lst[1])
print(ans)


from sys import stdin
n = int(stdin.readline())
arr = sorted(map(int, stdin.readline().split()))
v = arr.pop(-1)
ins1 = max(arr) - min(arr)
arr.append(v)
v = arr.pop(0)
ins2 = max(arr) - min(arr)
print(min(ins1, ins2))


length = input('')
numbers = input('')
length = int(length)
numbers = sorted([int(i) for i in numbers.split(' ')])
print(min(abs(numbers[0] - numbers[-2]), abs(numbers[1] - numbers[-1])))


import sys

def solve(io):
	N = io.read_int()
	A = io.read_int_array(N)
	A = list(sorted(A))
	io.println(min(A[-2] - A[0], A[-1] - A[1]))

class IO:
	in_stream = None
	out_stream = None
	raw = ''
	buf = []
	pos = 0

	def __init__(self, input_stream, output_stream):
		self.in_stream = input_stream
		self.out_stream = output_stream

	def read_to_buffer(self):
		self.raw = self.in_stream.readline().rstrip('\n')
		self.buf = self.raw.split()
		self.pos = 0

	def read_string(self):
		while self.pos == len(self.buf):
			self.read_to_buffer()
		ans = self.buf[self.pos]
		self.pos += 1
		return ans

	def read_int(self):
		return int(self.read_string())

	def read_float(self):
		return float(self.read_string())

	def read_string_array(self, N, offset=0):
		arr = [None] * offset
		for _ in range(0, N):
			arr.append(self.read_string())
		return arr

	def read_int_array(self, N, offset=0):
		arr = [None] * offset
		for _ in range(0, N):
			arr.append(self.read_int())
		return arr

	def read_float_array(self, N, offset=0):
		arr = [None] * offset
		for _ in range(0, N):
			arr.append(self.read_float())
		return arr

	def read_line(self):
		while self.pos == len(self.buf):
			self.read_to_buffer()
		if self.pos > 0:
			raise ValueError('Cannot call read_line in the middle of a line.')
		self.pos = len(self.buf)
		return self.raw

	def print(self, *args):
		self.out_stream.write(' '.join([str(x) for x in args]))

	def println(self, *args):
		self.print(*args)
		self.print('\n')

	def println_array(self, arr, sep=' '):
		self.println(sep.join((str(x) for x in arr)))

	def flush_output(self):
		self.out_stream.flush()
pythonIO = IO(sys.stdin, sys.stdout)
solve(pythonIO)
pythonIO.flush_output()


x = int(input())
lcopy = []
l = list(map(int, input().split()))
lcopy += l
lcopy.remove(max(lcopy))
l.remove(min(l))
minns = max(lcopy) - min(lcopy)
minns = min(minns, max(l) - min(l))
print(minns)


n = int(input())
array = []
array = input().split()
for i in range(n):
	array[i] = int(array[i])
array.sort()
menor = 0
menor = array[n - 1] - array[1]
if array[n - 2] - array[0] < menor:
	menor = array[n - 2] - array[0]
print(menor)


import sys
n = int(input())
list = [int(i) for i in input().split()]
if n == 2:
	print(0)
	sys.exit()
a = min(list)
b = max(list)
list.remove(a)
list.remove(b)
print(min(b - min(list), max(list) - a))


number_of_elements = int(input())
array_strings = input().split(' ')
array = []
for j in array_strings:
	array.append(int(j))
array.sort()
answer_1 = 0
answer_2 = 0
if len(array) - 2 >= 0:
	answer_1 = int(array[len(array) - 2]) - int(array[0])
else:
	answer_1 = int(array[len(array) - 1]) - int(array[0])
if len(array) > 1:
	answer_2 = int(array[len(array) - 1]) - int(array[1])
else:
	answer_2 = int(array[len(array) - 1]) - int(array[0])
if answer_2 < answer_1:
	print(answer_2)
else:
	print(answer_1)


n = int(input())
a = list(map(int, input().split()))
a.sort()
tempa = a[-1] - a[1]
tempb = a[-2] - a[0]
print(min(tempa, tempb))


n = int(input())
l = list(map(int, input().split()))
if n < 3:
	print(0)
else:
	l.sort()
	a = l[-2] - l[0]
	b = l[-1] - l[1]
	print(min(a, b))


def read_input():
	n = int(input())
	array = input()
	array = [int(number) for number in array.split()]
	return (n, array)

def compute_stability(array):
	return max(array) - min(array)

def array_stabilization(n, array):
	max_element = max(array)
	min_element = min(array)
	copy_1 = array[:]
	copy_2 = array[:]
	copy_1.remove(max_element)
	copy_2.remove(min_element)
	stability_max_element = compute_stability(copy_1)
	stability_min_element = compute_stability(copy_2)
	if stability_max_element < stability_min_element:
		return stability_max_element
	else:
		return stability_min_element
(n, array) = read_input()
res = array_stabilization(n, array)
print(res)


n = int(input())
a = list(map(int, input().split()))
if n == 2:
	print(0)
else:
	a.sort()
	mi1 = a[0]
	mi2 = a[1]
	ma1 = a[n - 1]
	ma2 = a[n - 2]
	ans = min(ma1 - mi2, ma2 - mi1)
	print(ans)


import sys

class Main:

	def __init__(self):
		self.buff = None
		self.index = 0

	def next(self):
		if self.buff is None or self.index == len(self.buff):
			self.buff = sys.stdin.readline().split()
			self.index = 0
		val = self.buff[self.index]
		self.index += 1
		return val

	def next_int(self):
		return int(self.next())

	def solve(self):
		n = self.next_int()
		x = sorted([self.next_int() for _ in range(0, n)])
		print(min(x[-1] - x[1], x[-2] - x[0]))
Main().solve()


t = int(input())
l = sorted(list(map(int, input().split())))
print(min(l[len(l) - 1] - l[1], l[len(l) - 2] - l[0]))


n = int(input())
l1 = input().split()
l3 = []
for i in l1:
	l3.append(int(i))
l1 = l3[:]
if n == 2:
	print(0)
else:
	x = l1.index(min(l1))
	y = l1.index(max(l1))
	l1.pop(x)
	l3.pop(y)
	i = max(l1) - min(l1)
	j = max(l3) - min(l3)
	if i > j:
		print(j)
	else:
		print(i)


from sys import stdin

def cast_to_int(arr):
	new_arr = []
	for value in arr:
		new_arr.append(int(value))
	return new_arr

def find_max(arr):
	max = arr[0]
	for value in arr:
		if value > max:
			max = value
	return max

def find_min(arr):
	min = arr[0]
	for value in arr:
		if value < min:
			min = value
	return min

def instability(arr):
	return find_max(arr) - find_min(arr)

def remove_and_find_instab(value, arr):
	new_arr = arr.copy()
	new_arr.remove(value)
	return instability(new_arr)

def solve_problem(length, arr):
	arr = cast_to_int(arr)
	try1 = remove_and_find_instab(find_max(arr), arr)
	try2 = remove_and_find_instab(find_min(arr), arr)
	if try1 > try2:
		return try2
	return try1
data = stdin.read()
data = data.split()
data = solve_problem(data[0], data[1:])
print(data)


R = lambda : map(int, input().split())
(n,) = R()
a = list(R())
a.sort()
print(min(a[-1] - a[1], a[-2] - a[0]))


import sys

def inpt():
	return sys.stdin.readline().strip()
n = int(inpt())
x = [int(i) for i in inpt().split()]
x = sorted(x)
print(min(x[n - 1] - x[1], x[n - 2] - x[0]))


n = int(input())
a = list(map(int, input().split()))
a.sort()
min1 = a[0]
min2 = a[1]
max1 = a[n - 1]
max2 = a[n - 2]
ans = min(max2 - min1, max1 - min2)
print(ans)


n = int(input())
x = list(map(int, input().split()))

def astab(x):
	if len(x) == 2:
		return 0
	d1 = max(x)
	x.remove(max(x))
	a = max(x) - min(x)
	x.append(d1)
	d2 = min(x)
	x.remove(min(x))
	b = max(x) - min(x)
	if a > b:
		return b
	else:
		return a
print(astab(x))


def B():
	n = int(input())
	array = input().split()
	array1 = [int(x) for x in array]
	array2 = [int(x) for x in array]
	stability = lambda x: max(x) - min(x)
	max1 = max(array1)
	min2 = min(array2)
	array1.remove(max1)
	array2.remove(min2)
	output = min(stability(array1), stability(array2))
	print(output)
B()


n = int(input())
l = list(map(int, input().split()))
x = l[:]
x.sort()
l.sort()
del x[-1]
ans1 = max(x) - min(x)
del l[0]
ans2 = max(l) - min(l)
print(min(ans1, ans2))


n = int(input())
arr = [int(x) for x in input().split()]
arr.sort()
mx = arr[-1]
smx = arr[-2]
smn = arr[1]
mn = arr[0]
print(min(smx - mn, mx - smn))


def solver(n, a):
	a = sorted(a)
	return min(a[-1] - a[1], a[-2] - a[0])
(n, a) = (int(input()), map(int, input().split()))
print(solver(n, a))


n = input()
A = input()
A = [int(i) for i in A.split()]
A = sorted(A)
m1 = A[-1] - A[1]
m2 = A[-2] - A[0]
print(min(m1, m2))


a = int(input())
b = list(map(int, input().split()))
b.sort()
if a == 2:
	print(0)
else:
	print(min(b[-2] - b[0], b[-1] - b[1]))


n = input()
nums = map(int, input().split())
sortn = sorted(nums)
first = sortn[1] - sortn[0]
last = sortn[-1] - sortn[-2]
if n == 2:
	print('0')
elif first <= last:
	print(sortn[-2] - sortn[0])
else:
	print(sortn[-1] - sortn[1])


import sys
a = input()
b = list(map(int, input().split()))
if len(b) == 1 or len(b) == 2:
	print('0')
	sys.exit(0)
b.sort()
Max = b[-1]
Nextmax = b[-2]
Min = b[0]
Nextmin = b[1]
print(min(Nextmax - Min, Max - Nextmin))


from sys import stdin
n = int(stdin.readline().strip())
s = list(map(int, stdin.readline().strip().split()))
s.sort()
if len(s) == 2:
	print(0)
	exit(0)
print(min(s[n - 2] - s[0], s[n - 1] - s[1]))


l = int(input())
a = list(map(int, input().split()))
if l == 2 or max(a) == min(a):
	print(0)
	exit()
else:
	min1 = min(a)
	a.remove(min(a))
	x = max(a) - min(a)
	a.append(min1)
	a.remove(max(a))
	y = max(a) - min(a)
	print(min(x, y))


n = int(input())
a = [int(i) for i in input().strip().split(' ')]
a1 = a.copy()
a1.remove(max(a1))
t1 = max(a1) - min(a1)
a.remove(min(a))
t = max(a) - min(a)
if t > t1:
	print(t1)
else:
	print(t)


n = int(input())
t = list(map(int, input().split()))
t = sorted(t)
b = sorted(t, reverse=True)
del b[n - 1]
del t[n - 1]
print(min(max(t) - min(t), max(b) - min(b)))


n = int(input())
L = sorted(map(int, input().split()))
if n == 2:
	print(0)
else:
	print(min(L[n - 2] - L[0], L[n - 1] - L[1]))


a = int(input())
x = [int(x) for x in input().split()]
x.sort()
if a <= 2:
	print(0)
else:
	print(min(abs(x[0] - x[-2]), abs(x[1] - x[-1])))


def process(A, n):
	B = sorted(A)
	return min(B[-1] - B[1], B[-2] - B[0])
n = int(input())
A = [int(x) for x in input().split()]
print(process(A, n))


n = int(input())
a = [int(_) for _ in input().split()]
a.sort()
mx = max(a)
mi = min(a)
z = [mx - mi, mx - min(a[1:]), max(a[:n - 1]) - mi]
print(min(z))


def main():
	n = int(input())
	a = list(map(int, input().split()))
	a.sort()
	print(min(a[-1] - a[1], a[-2] - a[0]))
	return
main()


n = int(input())
arr = list(map(int, input().split()))
insta1 = insta2 = 0
arr.sort()
insta = arr[-1] - arr[0]
insta1 = arr[-2] - arr[0]
insta2 = arr[-1] - arr[1]
ans = [insta, insta1, insta2]
if n == 2:
	print(0)
else:
	ans.sort()
	print(ans[0])


n = int(input())
a = sorted(list(map(int, input().split())))
k1 = a[-2] - a[0]
k2 = a[-1] - a[1]
print(min(k1, k2))


n = int(input())
s = list(map(int, input().split()))
M = max(s)
m = min(s)
i = M - m
s.remove(M)
M1 = max(s)
i1 = M1 - m
s.append(M)
s.remove(m)
m1 = min(s)
i2 = M - m1
print(min(i, i1, i2))


n = int(input())
l = list(map(int, input().split()))
mini = min(l)
maxi = max(l)
l.sort()
res = min(l[-2] - mini, maxi - l[1])
print(res)


def stab(arr):
	if len(arr) < 3:
		return 0
	arr.sort()
	if arr[1] - arr[0] > arr[-1] - arr[-2]:
		return arr[-1] - arr[1]
	else:
		return arr[-2] - arr[0]
n = int(input())
arr = list(map(int, input().strip().split()))
print(stab(arr))


input()
ls = sorted(list(map(int, input().split())))
print(ls[len(ls) - 2] - ls[0] if ls[len(ls) - 2] - ls[0] < ls[len(ls) - 1] - ls[1] else ls[len(ls) - 1] - ls[1])


n = int(input())
a = list(map(lambda e: int(e), input().split()))
new_instability = []
b = a.copy()
b.remove(max(a))
new_instability.append(max(b) - min(b))
b = a.copy()
b.remove(min(a))
new_instability.append(max(b) - min(b))
print(min(new_instability))


amount = int(input())
array = [int(s) for s in input().split()]
sorted_array = sorted(array)
diff = sorted_array[-1] - sorted_array[0]
if sorted_array[-2] - sorted_array[0] <= sorted_array[-1] - sorted_array[1]:
	print(sorted_array[-2] - sorted_array[0])
else:
	print(sorted_array[-1] - sorted_array[1])


input()

def mergeSort(arr):
	if len(arr) > 1:
		mid = len(arr) // 2
		L = arr[:mid]
		R = arr[mid:]
		mergeSort(L)
		mergeSort(R)
		i = j = k = 0
		while i < len(L) and j < len(R):
			if L[i] < R[j]:
				arr[k] = L[i]
				i += 1
			else:
				arr[k] = R[j]
				j += 1
			k += 1
		while i < len(L):
			arr[k] = L[i]
			i += 1
			k += 1
		while j < len(R):
			arr[k] = R[j]
			j += 1
			k += 1
nums = list(map(lambda x: int(x), input().split()))
if len(nums) < 3:
	ans = 0
else:
	mergeSort(nums)
	firstdif = nums[1] - nums[0]
	lastdif = nums[-1] - nums[-2]
	if firstdif > lastdif:
		ans = nums[-1] - nums[1]
	else:
		ans = nums[-2] - nums[0]
print(ans)


n = int(input())
L = list((int(x) for x in input().split()))
m = len(L)
if L.count(L[0]) == len(L):
	print(0)
elif m <= 2:
	print(0)
else:
	L.sort()
	max1 = L[n - 1]
	max2 = L[n - 2]
	min1 = L[0]
	min2 = L[1]
	if max1 - max2 >= min2 - min1:
		print(max2 - min1)
	else:
		print(max1 - min2)


n = int(input())
x = [int(i) for i in input().split()]
mx = max(x)
mn = min(x)
res = mx - mn
if x.count(mx) == 1:
	res = min(res, max(x[:x.index(mx)] + x[x.index(mx) + 1:]) - mn)
if x.count(mn) == 1:
	res = min(res, mx - min(x[:x.index(mn)] + x[x.index(mn) + 1:]))
print(res)


def instability(ar_num):
	ar_num.sort()
	max_diff = ar_num[len(ar_num) - 2] - min(ar_num)
	min_diff = max(ar_num) - ar_num[1]
	if max(ar_num) - min(ar_num) != 0:
		if max_diff < min_diff:
			return max_diff
		return min_diff
	return max(ar_num) - min(ar_num)
inp = int(input())
ar_num = list(map(int, input().split(' ')))
print(instability(ar_num))


n = int(input())
li = input().split()
for i in range(n):
	li[i] = int(li[i])
li.sort()
d1 = li[n - 1] - li[1]
d2 = li[n - 2] - li[0]
min = d1
if d2 < min:
	min = d2
print(min)


n = int(input())
s = input().split(' ')
s = [int(i) for i in s]
s.sort()
max = s[-1]
temp = max
s.remove(max)
s.sort()
max = s[-1]
min = s[0]
d1 = max - min
s.append(temp)
s.sort()
max = s[-1]
min = s[0]
s.remove(min)
min = s[0]
d2 = max - min
if d1 >= d2:
	print(d2)
else:
	print(d1)


input()
a = list(sorted(map(int, input().split())))
if len(a) == 1:
	print(a[0])
else:
	print(min(a[-1] - a[1], a[-2] - a[0]))


n = int(input())
x = [int(i) for i in input().split()]
x = sorted(x)
x1 = x[:len(x) - 1]
x2 = x[1:]
if max(x1) - min(x1) >= max(x2) - min(x2):
	print(max(x2) - min(x2))
else:
	print(max(x1) - min(x1))


(z, zz) = (input, lambda : list(map(int, z().split())))
zzz = lambda : [int(i) for i in stdin.readline().split()]
(szz, graph, mod, szzz) = (lambda : sorted(zz()), {}, 10 ** 9 + 7, lambda : sorted(zzz()))
from string import *
from re import *
from collections import *
from queue import *
from sys import *
from collections import *
from math import *
from heapq import *
from itertools import *
from bisect import *
from collections import Counter as cc
from math import factorial as f
from bisect import bisect as bs
from bisect import bisect_left as bsl
from itertools import accumulate as ac

def lcd(xnum1, xnum2):
	return xnum1 * xnum2 // gcd(xnum1, xnum2)

def prime(x):
	p = ceil(x ** 0.5) + 1
	for i in range(2, p):
		if x % i == 0 and x != 2 or x == 0:
			return 0
	return 1

def dfs(u, visit, graph):
	visit[u] = True
	for i in graph[u]:
		if not visit[i]:
			dfs(i, visit, graph)
num = int(z())
lst = szzz()
l1 = lst[-2] - lst[0]
l2 = lst[-1] - lst[1]
print(min(l1, l2))


size = int(input())
data = input()
data = list(map(int, data.split()))
data.sort()
min1 = data[0]
min2 = data[1]
max1 = data[-1]
max2 = data[-2]
print(min(min(max2 - min1, max1 - min2), max1 - min1))


n = int(input())
a = [int(i) for i in input().split()]
min_v1 = 10 ** 6
min_v2 = 10 ** 6
max_v1 = 0
max_v2 = 0
min_i1 = 0
min_i2 = 0
max_i1 = 0
max_i2 = 0
for i in range(n):
	if a[i] <= min_v1:
		(min_v1, min_v2) = (a[i], min_v1)
	elif a[i] < min_v2:
		min_v2 = a[i]
	if a[i] >= max_v1:
		(max_v1, max_v2) = (a[i], max_v1)
	elif a[i] > max_v2:
		max_v2 = a[i]
print(min(max_v1 - min_v2, max_v2 - min_v1))


from collections import Counter
n = int(input().strip())
a = [int(x) for x in input().strip().split(' ')]
if n <= 2:
	print(0)
else:
	a.sort(reverse=True)
	u = a[1] - a[n - 1]
	v = a[0] - a[n - 2]
	if u < v:
		print(u)
	else:
		print(v)


n = int(input())
l = list(map(int, input().split()))
l.sort()
d = l[n - 1] - l[1]
e = l[n - 2] - l[0]
f = min(d, e)
print(f)


t = int(input())
s = [int(x) for x in input().split()]
maxv = max(s)
minv = min(s)
s.remove(maxv)
m = max(s)
a = m - minv
s.append(maxv)
s.remove(minv)
n = min(s)
b = maxv - n
print(min(a, b))


n = int(input())
arr = list(map(int, input().split()))
arr.sort()
d1 = arr[1] - arr[0]
d2 = arr[n - 1] - arr[n - 2]
if d1 > d2:
	print(arr[n - 1] - arr[1])
else:
	print(arr[n - 2] - arr[0])


n = int(input())
lst = list(map(int, input().split()))
max_lst = sorted(lst, reverse=True)
max_lst.pop(0)
max_low = min(max_lst)
max_high = max(max_lst)
max_inst = max_high - max_low
min_lst = sorted(lst, reverse=False)
min_lst.pop(0)
min_low = min(min_lst)
min_high = max(min_lst)
min_inst = min_high - min_low
final_lst = [max_inst, min_inst]
print(min(final_lst))


n = int(input())
seq = list(map(int, input().split()))
if n == 2:
	print(0)
else:
	mi = min(seq)
	ma = max(seq)
	new_seq1 = seq.copy()
	new_seq1.pop(seq.index(mi))
	mi1 = min(new_seq1)
	ma1 = max(new_seq1)
	unst1 = ma1 - mi1
	new_seq2 = seq.copy()
	new_seq2.pop(seq.index(ma))
	mi2 = min(new_seq2)
	ma2 = max(new_seq2)
	unst2 = ma2 - mi2
	print(min(unst1, unst2))


import sys
n = int(sys.stdin.readline().strip())
arr = [int(elem) for elem in sys.stdin.readline().strip().split()]
if n == 2:
	print(0)
	exit()
else:
	minimum = min(arr)
	maximum = max(arr)
	min_removed = arr.copy()
	max_removed = arr.copy()
	min_removed.remove(minimum)
	max_removed.remove(maximum)
	instab1 = max(min_removed) - min(min_removed)
	instab2 = max(max_removed) - min(max_removed)
	print(min(instab1, instab2))


n = int(input())
arr = list(map(int, input().split()))
t = arr.copy()
t.remove(max(arr))
x = max(t) - min(t)
arr.remove(min(arr))
y = max(arr) - min(arr)
print(min(x, y))


n = int(input())
nums = input().split()
for i in range(len(nums)):
	nums[i] = int(nums[i])
nums.sort()
min_stab = min(nums[n - 1] - nums[min(1, n - 1)], nums[max(n - 2, 0)] - nums[0])
print(min_stab)


import random

def quick_sort(array):
	if len(array) < 2:
		return array
	else:
		middle = random.choice(array)
		(smaller, equal, bigger) = ([], [], [])
		for item in array:
			if item < middle:
				smaller.append(item)
			elif item > middle:
				bigger.append(item)
			else:
				equal.append(item)
		return quick_sort(smaller) + equal + quick_sort(bigger)
n = int(input())
a = [int(i) for i in input().split()]
a = quick_sort(a)
print(min(a[-1] - a[1], a[-2] - a[0]))


n = int(input())
l = list(map(int, input().split()))

def f(l, n):
	if n == 2:
		return 0
	l.sort()
	x = min(l[n - 2] - l[0], l[n - 1] - l[1])
	return x
print(f(l, n))


n = int(input())
a = list(map(int, input().rstrip().split()))
i0 = max(a) - min(a)
amax = a.copy()
amax.remove(max(a))
amin = a.copy()
amin.remove(min(a))
print(min(max(amax) - min(a), max(a) - min(amin)))


from sys import stdin
from collections import Counter
from math import sqrt, factorial, log10, log, floor, ceil
from itertools import permutations, combinations, combinations_with_replacement
input = stdin.readline

def li():
	return list(map(int, input().split()))

def lis():
	return list(map(str, input().split()))

def mp():
	return map(int, input().split())

def inp():
	return int(input())

def inps():
	return str(input().strip())

def pr(n):
	return stdout.write(str(n) + '\n')
INF = float('inf')

def solve():
	n = inp()
	a = li()
	if n == 2:
		print(0)
		return
	a.sort()
	print(min(a[-2] - a[0], a[-1] - a[1]))
t = 1
for i in range(t):
	solve()


n = int(input())
a = list(map(int, input().split()))
a.sort()
x = a[:n - 1]
y = a[1:]
b = max(x) - min(x)
c = max(y) - min(y)
if b <= c:
	print(b)
else:
	print(c)


from copy import copy
n = int(input())

def secondmax(dd):
	l = copy(dd)
	l.remove(max(l))
	return max(l)

def secondmin(dd):
	l = copy(dd)
	l.remove(min(l))
	return min(l)
d = list(map(int, input().split(' ')))
print(min(secondmax(d) - min(d), max(d) - secondmin(d)))


n = int(input())
tmp = input().split()
arr = []
for i in range(n):
	arr.append(int(tmp[i]))
arr.sort()
if n == 2:
	print('0')
else:
	print(min(arr[n - 1] - arr[0], min(arr[n - 1] - arr[1], arr[n - 2] - arr[0])))


lens = int(input())
arrs = sorted([int(x) for x in input().split()])
print(min(arrs[-1] - arrs[1], arrs[-2] - arrs[0]))


a = int(input())
b = list(input().split())
for x in range(0, len(b)):
	b[x] = int(b[x])
b.sort()
cop1 = b.copy()
cop2 = b.copy()
cop1.pop(a - 1)
cop2.pop(0)
maxy = max(b)
miny = min(b)
diff = maxy - miny
maxy1 = max(cop1)
miny1 = min(cop2)
rem_max = maxy1 - b[0]
rem_min = b[a - 1] - miny1
if rem_max < rem_min and rem_max < diff:
	print(rem_max)
else:
	print(rem_min)


n = int(input())
l = list(map(int, input().split()))
if n == 2:
	print(0)
else:
	x = max(l)
	m = min(l)
	l.remove(x)
	x1 = max(l)
	l.remove(m)
	m1 = min(l)
	q = x1 - m
	w = x - m1
	print(min(q, w))


import sys
import math
data = sys.stdin.readlines()
a = list(map(int, data[1].split(' ')))
a.sort()
sinMayor = a[len(a) - 2] - a[0]
sinMenor = a[len(a) - 1] - a[1]
if sinMayor < sinMenor:
	print(sinMayor)
else:
	print(sinMenor)


n = int(input())
t = list(map(int, input().split()))
t.sort()
v = 100001
for i in range(n):
	if i == 0:
		v = min(v, t[n - 1] - t[1])
	elif i == n - 1:
		v = min(v, t[n - 2] - t[0])
	else:
		v = min(v, t[n - 1] - t[0])
print(v)


import sys
import math
n = int(input())
nama = [int(x) for x in input().strip().split()]
kama = sorted(nama)
if len(nama) == 2:
	print('0')
else:
	amin = kama[0]
	bmin = kama[1]
	amax = kama[-1]
	bmax = kama[-2]
	print(min(amax - bmin, bmax - amin))


m1 = 0
m2 = 0
mi1 = 1000000
mi2 = 1000000
n = int(input())
z = input().split()
for y in z:
	x = int(y)
	if x >= m1:
		m2 = m1
		m1 = x
	elif x > m2:
		m2 = x
	if x <= mi1:
		mi2 = mi1
		mi1 = x
	elif x < mi2:
		mi2 = x
if m2 == 0:
	m2 = m1
if mi2 == 0:
	mi2 = mi1
print(min(m2 - mi1, m1 - mi2))


n = int(input())
a = list(map(int, input().split()))
if n == 2:
	print(0)
else:
	min1 = min2 = 1000000
	max1 = max2 = 0
	for i in range(n):
		if a[i] <= min1:
			min2 = min1
			min1 = a[i]
		elif a[i] < min2:
			min2 = a[i]
		if a[i] >= max1:
			max2 = max1
			max1 = a[i]
		elif a[i] > max2:
			max2 = a[i]
	if min1 == min2:
		print(max2 - min1)
	elif max1 == max2:
		print(max1 - min2)
	else:
		print(min(max1 - min2, max2 - min1))


n = int(input())
array = [int(x) for x in input().split()]
array.sort()
case1 = array[n - 1] - array[1]
case2 = array[n - 2] - array[0]
if case1 < case2:
	print(case1)
else:
	print(case2)


import copy

def calculate_stable_value_after_remove_item(target_array, remove_index):
	new_target_array = copy.deepcopy(target_array)
	new_target_array.pop(remove_index)
	return max(new_target_array) - min(new_target_array)

def main():
	n = int(input())
	target_array = [int(i) for i in input().split()]
	index_of_max = target_array.index(max(target_array))
	index_of_min = target_array.index(min(target_array))
	new_value_after_remove_max_value = calculate_stable_value_after_remove_item(target_array, index_of_max)
	new_value_after_remove_min_value = calculate_stable_value_after_remove_item(target_array, index_of_min)
	if new_value_after_remove_max_value < new_value_after_remove_min_value:
		print(new_value_after_remove_max_value)
	else:
		print(new_value_after_remove_min_value)
main()


n = int(input())
ln = input().split()
for i in range(len(ln)):
	ln[i] = int(ln[i])
ln.sort()
print(min(ln[-1] - ln[1], ln[-2] - ln[0]))


n = int(input())
A = []
for i in input().split():
	A.append(int(i))
A.sort()
if n == 2:
	print(0)
else:
	Max1 = A[n - 1]
	Max2 = A[n - 2]
	Min1 = A[0]
	Min2 = A[1]
	Insta1 = Max1 - Min2
	Insta2 = Max2 - Min1
	print(min(Insta1, Insta2))


t = int(input())
ar = list(map(int, input().split()))
c = 0
ar.sort()
c = ar[t - 2] - ar[0]
d = ar[t - 1] - ar[1]
print(min(c, d))


n = int(input())
l = list(map(int, input().split()))
if n == 2:
	print(0)
else:
	ma1 = max(l)
	mi1 = min(l)
	l.remove(ma1)
	l.remove(mi1)
	ma2 = max(l)
	mi2 = min(l)
	if ma1 - ma2 > mi2 - mi1:
		print(ma2 - mi1)
	else:
		print(ma1 - mi2)


a = int(input())
sum = 0
diff = 10 ** 6
lista = list(map(int, input().split()))
lista.sort()
if a == 2:
	print(0)
elif lista[1] - lista[0] > lista[a - 1] - lista[a - 2]:
	print(lista[a - 1] - lista[1])
else:
	print(lista[a - 2] - lista[0])


ar_count = int(input())
ar = list(map(int, input().rstrip().split()))
ar.sort()
temp = ar[ar_count - 1]
ar.pop(ar_count - 1)
m1 = max(ar) - min(ar)
ar.append(temp)
ar.pop(0)
m2 = max(ar) - min(ar)
print(min(m1, m2))


def ii():
	return int(input())

def mi():
	return map(int, input().split())

def li():
	return list(mi())
n = ii()
a = li()
a.sort()
ans = min(a[-1] - a[1], a[-2] - a[0])
print(ans)


n = int(input())
arr = list(map(int, input().split()))
arr.sort()
ans = 999999999999999999
if len(arr) < 3:
	ans = 0
if len(arr) >= 3:
	ans = min(ans, arr[-2] - arr[0])
	ans = min(ans, arr[-1] - arr[1])
print(ans)


alph = 'abcdefghijklmnopqrstuvwxyz'
n = int(input())
a = list(map(int, input().split()))
a.sort()
print(min(a[-2] - a[0], a[-1] - a[1]))


int(input())
l = list(map(int, input().split()))
(x, y) = (l[:], l[:])
x.remove(max(x))
y.remove(min(y))
print(min(max(x) - min(x), max(y) - min(y)))


import sys
n = int(input())
if n == 2:
	print(0)
	sys.exit(0)
arr = [int(x) for x in input().split()]
arr.sort()
x = arr[-1] - arr[1]
y = arr[-2] - arr[0]
print(min(x, y))


n = int(input())
l = [0 for i in range(0, n)]
l = input().split(' ')
for i in range(n):
	l[i] = int(l[i])
m1 = 1000000.0 + 10
m2 = 1000000.0 + 10
M1 = 0
M2 = 0
for i in range(n):
	if l[i] < m1:
		m2 = m1
		m1 = l[i]
	elif l[i] < m2:
		m2 = l[i]
	if l[i] > M1:
		M2 = M1
		M1 = l[i]
	elif l[i] > M2:
		M2 = l[i]
manji = M1 - m2
veci = M2 - m1
print(min(manji, veci))


_ = int(input())
nums = list(map(int, input().split()))
if len(nums) < 3:
	print(0)
else:
	max1 = max(nums)
	nums.remove(max1)
	max2 = max(nums)
	min1 = min(nums)
	nums.remove(min1)
	min2 = min(nums)
	nums.remove(min2)
	if max1 - max2 > min2 - min1:
		print(max2 - min1)
	else:
		print(max1 - min2)


def instable(n, a):
	if n == 2:
		print(0)
		return
	new_arr = sorted(a)
	print(min(new_arr[n - 1] - new_arr[1], new_arr[n - 2] - new_arr[0]))
	return
n = int(input())
a = [int(i) for i in input().split()]
instable(n, a)


n = int(input())
a = list(map(int, input().split()))
a.sort()
f = a[-1] - a[1]
l = a[-2] - a[0]
if f > l:
	print(l)
else:
	print(f)


def m():
	n = int(input())
	l = list(map(int, input().split()))
	l.sort()
	m = l[:]
	del l[-1]
	a = l[-1] - l[0]
	del m[0]
	b = m[-1] - m[0]
	if a >= b:
		print(b)
	else:
		print(a)
m()


n = int(input())
arr = [int(x) for x in input().strip().split()]
arr.sort()
a = arr[n - 1] - arr[1]
b = arr[n - 2] - arr[0]
print(min(a, b))


n = int(input())
a = []
a = list(map(int, input().split()))
a.sort()
a1 = abs(a[1] - a[n - 1])
a2 = abs(a[0] - a[n - 2])
if a1 > a2:
	print(a2)
else:
	print(a1)


n = int(input())
ans = list(map(int, input().split()))
ans.sort()
flip = ans.copy()
ans.pop(len(ans) - 1)
k1 = ans[len(ans) - 1] - ans[0]
flip.pop(0)
k2 = flip[len(flip) - 1] - flip[0]
print(min(k1, k2))


n = int(input())
a = [int(i) for i in input().split()]
MAX = max(a)
MIN = min(a)
id_min = 0
if_max = 0
for j in range(len(a)):
	if a[j] == MIN:
		id_min = j
	if a[j] == MAX:
		id_max = j
a_new_min = a[:id_min] + a[id_min + 1:]
a_new_max = a[:id_max] + a[id_max + 1:]
print(min(max(a_new_min) - min(a_new_min), max(a_new_max) - min(a_new_max)))


input()
b = sorted(list(map(int, input().split())))
print(min(b[-1] - b[1], b[-2] - b[0]))


def remove_values_from_list(the_list, val):
	return [value for value in the_list if value != val]
n = int(input())
a = list(map(int, input().split()))
a = sorted(a)
print(min(a[n - 2] - a[0], a[n - 1] - a[1]))


input()
a = list(map(int, input().split()))
b = a.copy()
c = a.copy()
d = a.index(max(a))
e = a.index(min(a))
del a[d]
f = max(a) - min(a)
del b[e]
g = max(b) - min(b)
print(min(f, g))


n = int(input())
l = sorted(list(map(int, input().split())))

def inst(l):
	return max(l) - min(l)
l1 = l[:-1]
l2 = l[1:]
ln = l1 if inst(l1) <= inst(l2) else l2
print(inst(ln))


from sys import stdin

def main():
	stdin.readline()
	arr = [int(x) for x in stdin.readline().strip().split(' ')]
	if len(arr) <= 2:
		print(0)
	else:
		res = 0
		maxi = max(arr)
		mini = min(arr)
		arr.remove(maxi)
		res = max(arr) - mini
		arr.append(maxi)
		arr.remove(mini)
		res = min(maxi - min(arr), res)
		print(res)
main()


n_elements = int(input())
array = [int(i) for i in input().split()]
array.sort()
remove_first = array[n_elements - 1] - array[1]
remove_last = array[n_elements - 2] - array[0]
if remove_first < remove_last:
	print(remove_first)
else:
	print(remove_last)


n = str(input())
a = str(input())
a = [int(val) for val in a.split(' ')]
a.sort()
if a[len(a) - 2] - a[0] < a[len(a) - 1] - a[1]:
	print(a[len(a) - 2] - a[0])
else:
	print(a[len(a) - 1] - a[1])


n = int(input())
l = list(map(int, input().split()))
l.sort()
ans1 = l[-2] - l[0]
ans2 = l[-1] - l[1]
print(min(ans1, ans2))


from typing import *

def main():
	n = int(input())
	a = map(int, input().strip().split(' '))
	answer = solve(n, a)
	print(answer)

def solve(n: int, a: List[int]):
	sorted_a = sorted(a)
	return min(sorted_a[-2] - sorted_a[0], sorted_a[-1] - sorted_a[1])
main()


n = int(input())
arr = list(map(int, input().split()))
if len(arr) == 1:
	print(0)
else:
	secMax = secMin = 0
	arr.sort()
	secMax = arr[len(arr) - 2]
	secMin = arr[1]
	print(min(max(arr) - secMin, secMax - min(arr)))


n = int(input())
a = sorted(list(map(int, input().split())))
print(a[n - 1] - a[0] - max(a[1] - a[0], a[n - 1] - a[n - 2]))


n = int(input())
k = [int(x) for x in input().split()]
f = k.copy()
f.remove(max(k))
r1 = max(f) - min(f)
f = k.copy()
f.remove(min(k))
r2 = max(f) - min(f)
print(min(r1, r2))


n = map(int, input())
lista = list(map(int, input().split(' ')))
lista.sort()
length = len(lista)
if length <= 2:
	print('0')
else:
	print(min(lista[-1] - lista[1], lista[length - 2] - lista[0]))


from sys import stdin
n = int(stdin.readline())
arr = sorted(map(int, stdin.readline().split()))

def minMax(arr):
	(mi, ma) = (arr[0], arr[0])
	for e in arr:
		if e < mi:
			mi = e
		if e > ma:
			ma = e
	return ma - mi
v = arr.pop(-1)
ins1 = minMax(arr)
arr.append(v)
v = arr.pop(0)
ins2 = minMax(arr)
print(min(ins1, ins2))


x = int(input())
zerocounter = 0
counter = 1000000
ar = list(map(int, input().split()))
ar.sort()
x = min(ar[x - 1] - ar[1], ar[x - 2] - ar[0])
print(x)


n = int(input())
a = [int(i) for i in input().split()]
(mn1, mn2, mx1, mx2) = (100001, 100001, -1, -1)
for i in range(n):
	if a[i] > mx1:
		mx2 = mx1
		mx1 = a[i]
	elif a[i] > mx2:
		mx2 = a[i]
	if a[i] < mn1:
		mn2 = mn1
		mn1 = a[i]
	elif a[i] < mn2:
		mn2 = a[i]
print(min(mx1 - mn2, mx2 - mn1))


n = int(input())
l = [int(i) for i in input().split()]
l2 = [i for i in l]
if len(l) == 2:
	print(0)
else:
	min1 = min(l2)
	l2.remove(min1)
	n1 = max(l2) - min(l2)
	m1 = max(l)
	l.remove(m1)
	n2 = max(l) - min(l)
	if n1 <= n2:
		print(n1)
	else:
		print(n2)


n = int(input())
lis = list(map(int, input().split()))
maxim = lis.index(max(lis))
minim = lis.index(min(lis))
l1 = lis[:maxim] + lis[maxim + 1:]
l2 = lis[:minim] + lis[minim + 1:]
if max(l1) - min(l1) > max(l2) - min(l2):
	print(max(l2) - min(l2))
else:
	print(max(l1) - min(l1))


n = int(input())
arr = list(map(int, input().split()))

def array(arr, n):
	if n <= 2:
		return 0
	else:
		arr.sort()
		x = arr[-1] - arr[1]
		y = arr[-2] - arr[0]
		return min(x, y)
print(array(arr, n))


n = int(input())
A = sorted(map(int, input().split()))
print(0 if n < 3 else min(A[-1] - A[1], A[-2] - A[0]))


import re
bs = input()

def numberGatherer():
	generalArray = re.findall('[+-]?\\d+', input(''))
	for index in range(0, len(generalArray)):
		generalArray[index] = int(generalArray[index])
	return generalArray
arrayRough = numberGatherer()
array = []
for element in arrayRough:
	array.append(int(element))
maximum = array[0]
maxIndex = 0
minimum = array[0]
minIndex = 0
for index in range(0, len(array)):
	if array[index] > maximum:
		maximum = array[index]
		maxIndex = index
	elif array[index] < minimum:
		minimum = array[index]
		minIndex = index
pretendArray = array.copy()
maximumOne = pretendArray[0]
maxIndexOne = 0
del pretendArray[minIndex]
minimumOne = pretendArray[0]
minIndexOne = 0
for index in range(0, len(pretendArray)):
	if pretendArray[index] > maximumOne:
		maximumOne = pretendArray[index]
		maxIndexOne = index
	elif pretendArray[index] < minimumOne:
		minimumOne = pretendArray[index]
		minIndexOne = index
answer = maximumOne - minimumOne
pretendArray = array.copy()
minimumOne = pretendArray[0]
minIndexOne = 0
del pretendArray[maxIndex]
maximumOne = pretendArray[0]
maxIndexOne = 0
for index in range(0, len(pretendArray)):
	if pretendArray[index] > maximumOne:
		maximumOne = pretendArray[index]
		maxIndexOne = index
	elif pretendArray[index] < minimumOne:
		minimumOne = pretendArray[index]
		minIndexOne = index
if maximumOne - minimumOne < answer:
	answer = maximumOne - minimumOne
print(answer)


n = int(input())
l = list(map(int, input().strip().split()))
min1 = 10 ** 9
min2 = 10 ** 9
max1 = 0
max2 = 0
for i in l:
	if i < min1:
		min2 = min1
		min1 = i
	elif i < min2:
		min2 = i
	if i >= max1:
		max2 = max1
		max1 = i
	elif i >= max2:
		max2 = i
print(min(max1 - min2, max2 - min1))


number = int(input())
string = input()
list = []
string = string.split(' ')
for i in string:
	list.append(int(i))
if number == 2:
	print(0)
else:
	list.sort()
	difference = list[1] - list[0]
	differenceLast = list[number - 1] - list[number - 2]
	if difference > differenceLast:
		print(list[number - 1] - list[1])
	else:
		print(list[number - 2] - list[0])


n = int(input())
a = list(map(int, input().split()))
(ma1, ma2) = (max(a[0:2]), min(a[0:2]))
(mi1, mi2) = (min(a[0:2]), max(a[0:2]))
for el in a[2:]:
	if el > ma1:
		ma2 = ma1
		ma1 = el
	elif el > ma2:
		ma2 = el
	if el < mi1:
		mi2 = mi1
		mi1 = el
	elif el < mi2:
		mi2 = el
print(min(ma1 - mi2, ma2 - mi1))


n = int(input().strip())
la = [int(x) for x in input().split()]
la.sort()
print(min(la[n - 1] - la[1], la[n - 2] - la[0]))


n = int(input())
ch = input()
l = ch.split()
l1 = ch.split()
for i in range(len(l)):
	l[i] = int(l[i])
for i in range(len(l1)):
	l1[i] = int(l1[i])
l.remove(max(l))
a = int(max(l)) - int(min(l))
l1.remove(min(l1))
b = int(max(l1)) - int(min(l1))
if b < a:
	print(b)
else:
	print(a)


n = int(input())
a = [int(x) for x in input().split()]
b = a.copy()
a.remove(max(a))
b.remove(min(a))
if max(a) - min(a) < max(b) - min(b):
	print(max(a) - min(a))
else:
	print(max(b) - min(b))


size = input()
array = sorted(map(int, input().split()))
a = array[-1] - array[1]
b = array[-2] - array[0]
print(min(a, b))


n = int(input())
s = list(map(int, input().split()))
s1 = s.copy()
s2 = s.copy()
s1.remove(min(s1))
s2.remove(max(s2))
print(min(max(s1) - min(s1), max(s2) - min(s2)))


n = int(input())
a = list(map(int, input().split()))
a.sort()
m1 = a[-2] - a[0]
m2 = a[-1] - a[1]
print(min(m1, m2))


n = int(input())
a = list(map(int, input().split()))
if n == 2:
	print(0)
	exit()
a = sorted(a)
fi = a[0]
la = a[n - 1]
fi_2 = a[1]
la_2 = a[n - 2]
print(min(la - fi_2, la_2 - fi))


n = input()
arr = [int(i) for i in input().split()]
if len(arr) < 3:
	print(0)
else:
	MAX = 9999999
	MIN = -1
	min = MAX
	max = MIN
	prev_min = MAX
	prev_max = MIN
	for i in arr:
		if i < min:
			prev_min = min
			min = i
		elif i < prev_min:
			prev_min = i
		if i > max:
			prev_max = max
			max = i
		elif i > prev_max:
			prev_max = i
	if prev_max == MIN:
		prev_max == max
	if prev_min == MAX:
		prev_min = min
	res = prev_max - min if prev_max - min < max - prev_min else max - prev_min
	print(res)


(inp, s) = (int(input()), sorted(list(map(int, input().split()))))
print(min(s[inp - 2] - s[0], s[inp - 1] - s[1]))


n = int(input())
a = input().split()
a = [int(i) for i in a]
if n == 2:
	print(0)
else:
	b1 = max(a)
	a.remove(b1)
	c1 = max(a) - min(a)
	b2 = min(a)
	a.append(b1)
	a.remove(b2)
	c2 = max(a) - min(a)
	print(min(c1, c2))


n = int(input())
a = [int(x) for x in input().split()]
a.sort()
c = []
c.append(a[n - 2] - a[0])
c.append(a[n - 1] - a[1])
d = min(c)
print(d)


n = int(input())
a = [*map(int, input().split())]
max1 = max(a)
min1 = min(a)
a.remove(max1)
max2 = max(a)
a.append(max1)
a.remove(min1)
min2 = min(a)
print(min(max1 - min2, max2 - min1))


from collections import defaultdict
n = int(input())
items = [int(i) for i in input().split()]
items.sort()
if len(items) == 2:
	print(0)
	exit(0)
candidates = [items[0], items[1], items[-1], items[-2]]
if items[-2] - items[0] < items[-1] - items[1]:
	print(items[-2] - items[0])
else:
	print(items[-1] - items[1])