Sorting is useful as the first step in many different tasks. The most common task is to make finding things easier, but there are other uses as well.  In this case, it will make it easier to determine which pair or pairs of elements have the smallest absolute difference between them.

Example 

$arr=[5,2,3,4,1]$   

Sorted, $ar r'=[1,2,3,4,5]$.  Several pairs have the minimum difference of $1$: $[(1,2),(2,3),(3,4),(4,5)]$.  Return the array $[1,2,2,3,3,4,4,5]$.

Note 

As shown in the example, pairs may overlap.  

Given a list of unsorted integers, $\textbf{arr}$, find the pair of elements that have the smallest absolute difference between them. If there are multiple pairs, find them all.

Function Description  

Complete the closestNumbers function in the editor below.   

closestNumbers has the following parameter(s):  

int arr[n]: an array of integers   

Returns 

- int[]: an array of integers as described   

Input Format

The first line contains a single integer $n$, the length of $\textbf{arr}$. 

The second line contains $n$ space-separated integers, $arr\left[i\right]$.

Constraints

$2\leq n\leq200000$
$-10^7\leq arr[i]\leq10^7$
All $a[i]$ are unique in $\textbf{arr}$.

Output Format

Sample Input 0

10
-20 -3916237 -357920 -3620601 7374819 -7330761 30 6246457 -6461594 266854 

Sample Output 0

-20 30

Explanation 0 

(30) - (-20) = 50, which is the smallest difference.  

Sample Input 1

12
-20 -3916237 -357920 -3620601 7374819 -7330761 30 6246457 -6461594 266854 -520 -470 

Sample Output 1

-520 -470 -20 30

Explanation 1 

(-470) - (-520) = 30 - (-20) = 50, which is the smallest difference. 

Sample Input 2

4
5 4 3 2

Sample Output 2

2 3 3 4 4 5

Explanation 2 

Here, the minimum difference is 1. Valid pairs are (2, 3), (3, 4), and (4, 5).

Source: hackerrank | Difficulty: EASY
Tags: Sorting


Solutions:
def main():
	N = int(input())
	ar = sorted(list(map(int, str(input()).split())))
	prs = ''
	mini = pow(10, 7) + 1
	for i in range(1, N):
		diff = abs(ar[i - 1] - ar[i])
		if diff <= mini:
			if diff < mini:
				prs = ''
			mini = diff
			prs += str(ar[i - 1]) + ' ' + str(ar[i]) + ' '
	print(prs)
main()


testcase = int(input())
arr = [int(i) for i in input().split()]
arr.sort()
out = ''
index1 = []
index2 = []
min = 10000000
for x in range(1, testcase):
	if arr[x] - arr[x - 1] < min:
		min = arr[x] - arr[x - 1]
		index1 = [arr[x - 1]]
		index2 = [arr[x]]
	elif arr[x] - arr[x - 1] == min:
		index1.append(arr[x - 1])
		index2.append(arr[x])
final = index1 + index2
final.sort()
for i in final:
	out += str(i) + ' '
print(out)


import sys
input()
array = sorted(map(int, input().split()))
smallest = sys.maxsize
pairs = []
for i in range(1, len(array)):
	diff = abs(array[i - 1] - array[i])
	if diff < smallest:
		smallest = diff
		pairs = []
		pairs.append(array[i - 1])
		pairs.append(array[i])
	elif diff == smallest:
		pairs.append(array[i - 1])
		pairs.append(array[i])
print(' '.join(map(str, pairs)))


N = int(input())
ar = [int(x) for x in input().split()]
ar.sort()
min = ar[1] - ar[0]
for i in range(1, N - 1):
	if ar[i + 1] - ar[i] < min:
		min = ar[i + 1] - ar[i]
ans = ''
for j in range(N - 1):
	if ar[j + 1] - ar[j] == min:
		if len(ans) > 0:
			ans = ans + ' '
		ans = ans + str(ar[j]) + ' ' + str(ar[j + 1])
print(ans)


import math

def main():
	n = input()
	numbers = sorted([int(num) for num in input().split(' ') if num != ''])
	pairs = []
	previous = numbers[0]
	diff = math.fabs(numbers[1] - numbers[0])
	for num in numbers[1:]:
		current_diff = math.fabs(previous - num)
		if diff > current_diff:
			diff = current_diff
			pairs = []
		if current_diff == diff:
			pairs.append((previous, num))
		previous = num
	result = []
	for pair in pairs:
		result.append(str(pair[0]) + ' ' + str(pair[1]))
	print(' '.join(result))
main()


n = int(input())
ar = input().rstrip().split(' ')
ar = [int(i) for i in ar]
ar.sort()
mins = [ar[0], ar[1]]
for i in range(1, len(ar)):
	if abs(ar[i] - ar[i - 1]) < abs(mins[1] - mins[0]):
		mins = [ar[i - 1], ar[i]]
	elif abs(ar[i] - ar[i - 1]) == abs(mins[1] - mins[0]):
		mins += [ar[i - 1], ar[i]]
print(*mins)


n = int(input())
ar = input().split()
ar = sorted(list(map(int, ar)))
initial_diff = ar[1] - ar[0]
final = str(ar[0]) + ' ' + str(ar[1]) + ' '
for i in range(2, len(ar)):
	diff = ar[i] - ar[i - 1]
	if diff < initial_diff:
		final = ''
		final = str(ar[i - 1]) + ' ' + str(ar[i]) + ' '
		initial_diff = diff
	elif diff == initial_diff:
		final = final + str(ar[i - 1]) + ' ' + str(ar[i]) + ' '
print(final)


m = int(input())
br = input().split()
br = sorted(list(map(int, br)))
initial_diff = br[1] - br[0]
final = str(br[0]) + ' ' + str(br[1]) + ' '
for i in range(2, len(br)):
	diff = br[i] - br[i - 1]
	if diff < initial_diff:
		final = ''
		final = str(br[i - 1]) + ' ' + str(br[i]) + ' '
		initial_diff = diff
	elif diff == initial_diff:
		final = final + str(br[i - 1]) + ' ' + str(br[i]) + ' '
print(final)


import sys
N = int(sys.stdin.readline())
ar = sorted([int(x) for x in sys.stdin.readline().split()])
min_diff = 1000000.0
pairs = []
for i in range(0, len(ar) - 1):
	if ar[i + 1] - ar[i] == min_diff:
		pairs.append((ar[i], ar[i + 1]))
	elif ar[i + 1] - ar[i] < min_diff:
		pairs = [(ar[i], ar[i + 1])]
		min_diff = ar[i + 1] - ar[i]
print(' '.join([' '.join([str(t[0]), str(t[1])]) for t in pairs]))


n = int(input())
ar = [int(i) for i in input().split()]
ar.sort()
mindiff = abs(ar[0] - ar[1])
minar = []
for i in range(n - 1):
	if mindiff == abs(ar[i] - ar[i + 1]):
		minar.append(sorted([ar[i], ar[i + 1]]))
	elif mindiff > abs(ar[i] - ar[i + 1]):
		minar = [sorted([ar[i], ar[i + 1]])]
		mindiff = abs(ar[i] - ar[i + 1])
minar.sort()
for i in minar:
	print(i[0], i[1], end=' ')


n = int(input())
numbers = input().split()
numbers = [int(x) for x in numbers]
numbers = sorted(numbers)
min_diff = numbers[1] - numbers[0]
ret_list = []
for i in range(1, n):
	if numbers[i] - numbers[i - 1] == min_diff:
		ret_list.append(numbers[i - 1])
		ret_list.append(numbers[i])
	elif numbers[i] - numbers[i - 1] < min_diff:
		ret_list = []
		ret_list.append(numbers[i - 1])
		ret_list.append(numbers[i])
		min_diff = numbers[i] - numbers[i - 1]
print(' '.join(map(str, ret_list)))


n = int(input())
ar = input().split()
ar = [int(ar[i]) for i in range(n)]
ar.sort()
dif = []
for i in range(1, n):
	dif.append(ar[i] - ar[i - 1])
sdif = ndif = min(dif)
while sdif == ndif:
	print(ar[dif.index(sdif)], ar[dif.index(sdif) + 1], end=' ')
	dif[dif.index(sdif)] += 1
	ndif = min(dif)


n = int(input())
vals = [int(i) for i in input().split()]
vals.sort()
minDist = vals[-1] - vals[0]
dists = []
for i in range(len(vals) - 1):
	dists.append(vals[i + 1] - vals[i])
	if dists[i] < minDist:
		minDist = dists[i]
for i in range(len(vals) - 1):
	if dists[i] == minDist:
		print(vals[i], vals[i + 1], end=' ')
print()


def lonelyinteger(a):
	a = list(a)
	a.sort()
	diff = dict()
	for i in range(len(a) - 1):
		cur_diff = a[i + 1] - a[i]
		if cur_diff in diff:
			diff[cur_diff].append(a[i])
			diff[cur_diff].append(a[i + 1])
		else:
			values = list()
			values.append(a[i])
			values.append(a[i + 1])
			diff[cur_diff] = values
	smallest = min(diff)
	return ' '.join(map(str, diff[smallest]))
a = int(input())
b = map(int, input().strip().split(' '))
print(lonelyinteger(b))


n = int(input())
raw_ar = input().strip().split()
ar = [int(i) for i in raw_ar]
ar.sort()
k = 100000000
lst = []
for i in range(1, n):
	if abs(ar[i] - ar[i - 1]) < k:
		k = abs(ar[i] - ar[i - 1])
		lst = []
		lst.append(ar[i - 1])
		lst.append(ar[i])
	elif abs(ar[i] - ar[i - 1]) == k:
		lst.append(ar[i - 1])
		lst.append(ar[i])
print(' '.join([str(i) for i in lst]))


a = input()
b = input()
b = b.split()
c = []
for i in b:
	c.append(float(i))
c.sort()
b = len(c)
i = 0
diffmin = 10000000000000000
min1 = []
min2 = []
while i < b - 1:
	diff = c[i + 1] - c[i]
	if diff == diffmin:
		min1.append(i)
		min2.append(i + 1)
	elif diff < diffmin:
		min1 = []
		min2 = []
		diffmin = diff
		min1.append(i)
		min2.append(i + 1)
	i = i + 1
b = len(min1)
i = 0
while i < b:
	print('%d %d' % (c[min1[i]], c[min2[i]]), end='')
	if i < b - 1:
		print(' ', end='')
	i = i + 1


def closest_numbers(n, A):
	min_diff = float('inf')
	number_set = []
	A.sort()
	for i in range(n - 1):
		if A[i + 1] - A[i] < min_diff:
			min_diff = A[i + 1] - A[i]
			temp = (A[i], A[i + 1])
			number_set = [temp]
		elif A[i + 1] - A[i] == min_diff:
			temp = (A[i], A[i + 1])
			number_set.append(temp)
	for i in number_set:
		print(' '.join(map(str, i)), end=' ')
n = int(input())
A = list(map(int, input().split()))
closest_numbers(n, A)


import sys
sys.stdin.readline()
ar = [int(s) for s in sys.stdin.readline().strip().split(' ')]
ar.sort()
minD = None
for i in range(len(ar) - 1):
	d = ar[i + 1] - ar[i]
	if minD is None or d < minD:
		minD = d
first = True
for i in range(len(ar) - 1):
	d = ar[i + 1] - ar[i]
	if d == minD:
		if first:
			first = False
		else:
			print(' ', end='')
		print(ar[i], ar[i + 1], end='')
print()


def closest_numbers(list):
	list.sort()
	min = 0
	result = []
	for x in range(len(list) - 1):
		if abs(list[x] - list[x + 1]) < min or x == 0:
			min = abs(list[x] - list[x + 1])
			result[:] = []
			result.append(x)
		elif abs(list[x] - list[x + 1]) == min:
			result.append(x)
	min = ''
	for x in result:
		min += str(list[x]) + ' ' + str(list[x + 1]) + ' '
	print(min.strip())
	return
a = input()
b = input()
b = b.split(' ')
b = [int(x) for x in b if x != '']
closest_numbers(b)


n = int(input())
ar = [int(i) for i in input().strip().split()]
ar.sort()
d = [ar[i + 1] - ar[i] for i in range(n - 1)]
minim = min(d)
s = [[ar[i], ar[i + 1]] for i in range(n - 1) if d[i] == minim]
for i in s:
	print(*i, sep=' ', end=' ')


def printList(list):
	for x in list:
		print(x, end=' ')
	print()

def closest(numbers):
	numbers = sorted(numbers)
	mindiff = 20000002
	result = []
	N = len(numbers)
	for i in range(1, N):
		if numbers[i] - numbers[i - 1] < mindiff:
			mindiff = numbers[i] - numbers[i - 1]
			result[:] = []
			result.append(numbers[i - 1])
			result.append(numbers[i])
		elif numbers[i] - numbers[i - 1] == mindiff:
			result.append(numbers[i - 1])
			result.append(numbers[i])
	return result

def main():
	N = int(input())
	numbers = [int(x) for x in input().split()]
	printList(closest(numbers))
main()


s = int(input())
rs = input().strip()
r = list(map(int, rs.split(' ')))
r.sort()
d = dict()
for i in range(s - 1):
	j = i + 1
	diff = r[j] - r[i]
	lis = [r[i], r[j]]
	d[i] = diff
akh = list(d.values())
akh.sort()
z = akh[0]
for i in range(len(d)):
	if d[i] == z:
		print(r[i], r[i + 1], end=' ')


(_, nums) = (input(), list(map(int, input().split())))
nums.sort()
mindiff = float('inf')
diffs = []
for (a, b) in zip(nums, nums[1:]):
	if b - a < mindiff:
		(mindiff, diffs) = (b - a, [])
	if b - a == mindiff:
		diffs += [a, b]
print(' '.join(map(str, diffs)))


n = input()
arr = input()
arr = map(int, arr.split())
arr = sorted(arr)
output = ''
z = 0
diff = arr[1] - arr[0]
output = str(arr[0]) + ' ' + str(arr[1])
for x in range(1, len(arr) - 1):
	val1 = arr[x]
	val2 = arr[x + 1]
	z = val2 - val1
	if z > diff:
		continue
	elif z < diff:
		output = str(val1) + ' ' + str(val2)
		diff = z
	else:
		output = output + ' ' + str(val1) + ' ' + str(val2)
print(output)


import sys

def closest_nums(l, arr):
	arr = sorted(arr)
	_min_val = sys.maxsize
	_min_pairs = []
	for i in range(l - 1):
		val = abs(arr[i] - arr[i + 1])
		if val < _min_val:
			_min_val = val
			_min_pairs.clear()
			_min_pairs.extend([arr[i], arr[i + 1]])
		elif val == _min_val:
			_min_pairs.extend([arr[i], arr[i + 1]])
	return _min_pairs
l = int(input().strip())
a = list(map(int, input().strip().split(' ')))
print(' '.join(map(str, closest_nums(l, a))))


x = int(input())
lis = [int(i) for i in input().split()]
lis.sort()
mindif = 2 ** 30
minpairs = []
for i in range(1, x):
	dif = lis[i] - lis[i - 1]
	if dif < mindif:
		mindif = dif
		minpairs = [str(lis[i - 1]), str(lis[i])]
	elif dif == mindif:
		minpairs.extend((str(lis[i - 1]), str(lis[i])))
print(' '.join(minpairs))


N = int(input())
X = [int(x) for x in input().strip().split(' ')]
X.sort()
m = X[1] - X[0]
for x in range(1, N - 1):
	if X[x + 1] - X[x] < m:
		m = X[x + 1] - X[x]
l = []
for x in range(0, N - 1):
	if X[x + 1] - X[x] == m:
		l += [X[x], X[x + 1]]
print(' '.join(map(str, l)))


n = int(input())
a = [int(num) for num in input().split()]
a.sort()
sol = [a[0], a[1]]
for i in range(1, n - 1):
	if a[i + 1] - a[i] < sol[1] - sol[0]:
		sol = [a[i], a[i + 1]]
	elif a[i + 1] - a[i] == sol[1] - sol[0]:
		sol += [a[i], a[i + 1]]
for i in sol:
	print(i, end=' ')


n = int(input())
a = map(int, input().strip().split(' '))
b = list(a)
b.sort()
l = [b[0], b[1]]
dif = b[1] - b[0]
for i in range(1, len(b) - 1):
	if b[i + 1] - b[i] == dif:
		l.append(b[i])
		l.append(b[i + 1])
	if b[i + 1] - b[i] < dif:
		dif = b[i + 1] - b[i]
		l = [b[i], b[i + 1]]
for j in l:
	print(j, end=' ')


N = int(input())
s = input()
s = s.split(' ')
h = []
for c in s:
	if c:
		h.append(c)
s = h
ar = [int(x) for x in s]
ar.sort()
erg = []
abstand = -1
for i in range(0, len(ar) - 1):
	jetzt = abs(ar[i] - ar[i + 1])
	if abstand == -1 or jetzt <= abstand:
		if jetzt != abstand:
			erg = []
		abstand = jetzt
		paar = [ar[i], ar[i + 1]]
		erg.append(paar)
for paar in erg:
	for z in paar:
		print(z, end=' ')


def g():
	x = str(input())
	y = x.split()
	z = []
	for i in y:
		z.append(int(i))
	return z

def h(l):
	l.sort()
	x = l[1] - l[0]
	y = []
	for i in range(len(l) - 1):
		if l[i + 1] - l[i] == x:
			y.extend([l[i], l[i + 1]])
		if l[i + 1] - l[i] < x:
			y = [l[i], l[i + 1]]
			x = l[i + 1] - l[i]
	return listtostr(y)

def listtostr(l):
	x = str(l[0])
	for i in range(1, len(l)):
		x = x + ' ' + str(l[i])
	return x
n = int(input())
l = g()
print(h(l))


n = int(input())
a = input()
a = [int(i) for i in a.strip().split(' ')]
a.sort()
diff = []
for i in range(1, len(a)):
	diff.append((a[i] - a[i - 1], i - 1, i))
diff.sort()
mn = diff[0]
result = []
result.append(a[mn[1]])
result.append(a[mn[2]])
for i in range(1, len(diff)):
	if diff[i][0] == mn[0]:
		result.append(a[diff[i][1]])
		result.append(a[diff[i][2]])
	else:
		break
for i in range(len(result)):
	print(result[i], end=' ')


n = int(input())
array = list(map(int, input().split()))
array.sort()
min_diff = min((array[i + 1] - array[i] for i in range(len(array) - 1)))
print(' '.join((str(array[i]) + ' ' + str(array[i + 1]) for i in range(len(array) - 1) if array[i + 1] - array[i] == min_diff)))


import math
numberOfElements = int(input())
data = list(map(int, input().strip().split(' ')))
data.sort()
diff = []
for index in range(1, len(data)):
	diff.append(abs(data[index] - data[index - 1]))
minimum = min(diff)
indices = [i for (i, x) in enumerate(diff) if x == minimum]
for index in indices:
	print('{} {}'.format(data[index], data[index + 1]), end=' ')


n = int(input())
ar = sorted(map(int, input().split()))
d = min((abs(ar[i] - ar[i - 1]) for i in range(1, n)))
for i in range(1, n):
	if abs(ar[i] - ar[i - 1]) == d:
		print(ar[i - 1], ar[i], end=' ')
print()


import sys
n = int(sys.stdin.readline())
array = sorted(map(int, sys.stdin.readline().split()))
min_diff = min((b - a for (a, b) in zip(array, array[1:])))
print(*sum(([a, b] for (a, b) in zip(array, array[1:]) if b - a == min_diff), []))


import sys

def main():
	n = int(sys.stdin.readline())
	nums = [int(i) for i in sys.stdin.readline().split()]
	nums.sort()
	smallest_l = []
	smallest = nums[len(nums) - 1]
	for i in range(len(nums) - 1):
		if nums[i + 1] - nums[i] < smallest:
			smallest = nums[i + 1] - nums[i]
			smallest_l = [nums[i], nums[i + 1]]
		elif nums[i + 1] - nums[i] == smallest:
			smallest_l.append(nums[i])
			smallest_l.append(nums[i + 1])
	smallest_l = [str(i) for i in smallest_l]
	print(' '.join(smallest_l))
main()


n = int(input())
a = list(map(int, input().split()))
a.sort()
(m, r) = (a[-1] - a[0], [(a[0], a[-1])])
for i in range(1, len(a)):
	diff = a[i] - a[i - 1]
	if diff < m:
		(m, r) = (diff, [(a[i - 1], a[i])])
	elif diff == m:
		r.append((a[i - 1], a[i]))
for (a, b) in r:
	print(a, b, end=' ')


import sys
numitems = int(sys.stdin.readline().strip())
items = sorted([int(x) for x in sys.stdin.readline().split()])
diffs = {}
for i in range(1, numitems):
	diff = items[i] - items[i - 1]
	if diff not in diffs:
		diffs[diff] = []
	diffs[diff].append(str(items[i - 1]))
	diffs[diff].append(str(items[i]))
print(' '.join(diffs[min(diffs.keys())]))


import sys, itertools
n = int(sys.stdin.readline())
nums = sorted([int(s) for s in sys.stdin.readline().rstrip().split()])
min_diff = min((y - x for (x, y) in zip(nums[:-1], nums[1:])))
print(' '.join(['%d %d' % (x, y) for (x, y) in zip(nums[:-1], nums[1:]) if y - x == min_diff]))


n = int(input())
arr = list(map(int, str(input()).strip().split()))
arr.sort()
min_diff = arr[1] - arr[0]
pairs = [(arr[0], arr[1])]
for i in range(1, len(arr) - 1):
	diff = arr[i + 1] - arr[i]
	if diff < min_diff:
		min_diff = diff
		pairs = [(arr[i], arr[i + 1])]
	elif diff == min_diff:
		pairs.append((arr[i], arr[i + 1]))
print(' '.join(map(lambda pair: ' '.join(map(str, pair)), pairs)))


import sys
import collections
n = int(input())
ar = [int(x) for x in input().split()]

def smallestdiff(ar):
	ar.sort()
	smallest = sys.maxsize
	els = collections.defaultdict(list)
	for i in range(len(ar) - 1):
		if abs(ar[i] - ar[i + 1]) < smallest:
			smallest = abs(ar[i] - ar[i + 1])
			els[smallest].append(tuple([ar[i], ar[i + 1]]))
		elif abs(ar[i] - ar[i + 1]) == smallest:
			els[smallest].append(tuple([ar[i], ar[i + 1]]))
	res = ''
	for x in els[smallest]:
		res += ' '.join(map(str, x)) + ' '
	return res
print(smallestdiff(ar))


n = int(input())
arr = sorted([int(v) for v in input().split()])
result = []
mindiff = -1
for i in range(n - 1):
	diff = arr[i + 1] - arr[i]
	if mindiff == -1 or diff < mindiff:
		result = [(arr[i], arr[i + 1])]
		mindiff = diff
	elif mindiff == diff:
		result.append((arr[i], arr[i + 1]))
print(' '.join([' '.join([str(v) for v in pair]) for pair in result]))


numItems = int(input())
array = [int(x) for x in input().split()]
ans = ''
assert len(array) == numItems
array.sort()
minDiff = abs(array[0] - array[1])
for i in range(len(array) - 1):
	diff = abs(array[i] - array[i + 1])
	if diff < minDiff:
		minDiff = diff
for i in range(len(array) - 1):
	diff = abs(array[i] - array[i + 1])
	if diff == minDiff:
		ans += str(array[i]) + ' ' + str(array[i + 1]) + ' '
print(ans.strip())


input()
ar = sorted((int(i) for i in input().split()))
min_dif = abs(ar[0] - ar[1])
res = [ar[0], ar[1]]
for i in range(2, len(ar)):
	dif = abs(ar[i] - ar[i - 1])
	if dif == min_dif:
		res.extend((ar[i - 1], ar[i]))
	elif dif < min_dif:
		res = [ar[i - 1], ar[i]]
		min_dif = dif
print(' '.join((str(i) for i in res)))


import sys
T = int(sys.stdin.readline())
numbers = sorted([int(X) for X in sys.stdin.readline().split()])
closest_distance = 20000001
solutions = list()
for (i, left) in enumerate(numbers[:-1]):
	right = numbers[i + 1]
	if right - left < closest_distance:
		solutions = [(left, right)]
		closest_distance = right - left
	elif right - left == closest_distance:
		solutions.append((left, right))
for (left, right) in solutions:
	print(left, right, end=' ')


N = int(input())
tab = sorted([int(i) for i in str(input()).split()])
min_diff = tab[N - 1]
for i in range(N - 1):
	min_diff = min(min_diff, tab[i + 1] - tab[i])
ans = []
for i in range(N - 1):
	if min_diff == tab[i + 1] - tab[i]:
		ans.append(str(tab[i]))
		ans.append(str(tab[i + 1]))
print(' '.join(ans))


N = int(input())
A = [int(e) for e in input().split()]
A.sort()
d = 10 ** 8
L = []
for i in range(len(A) - 1):
	e = A[i + 1] - A[i]
	if e < d:
		d = e
		L = [A[i], A[i + 1]]
	elif e == d:
		L.append(A[i])
		L.append(A[i + 1])
print(' '.join([str(e) for e in L]))


input()
ns = [int(x) for x in input().split()]
ns.sort()
s = ''
m = abs(max(ns) - min(ns))
for i in range(len(ns) - 1):
	if abs(ns[i] - ns[i + 1]) < m:
		m = abs(ns[i] - ns[i + 1])
		s = str(ns[i]) + ' ' + str(ns[i + 1])
	elif abs(ns[i] - ns[i + 1]) == m:
		s += ' ' + str(ns[i]) + ' ' + str(ns[i + 1])
print(s)


import sys

def quick_sort(arr, s):
	if s == 1:
		return arr
	if s == 0:
		return []
	smaller = []
	bigger = []
	value = arr[0]
	for x in range(1, s):
		if arr[x] >= value:
			bigger.append(arr[x])
		else:
			smaller.append(arr[x])
	result = quick_sort(smaller, len(smaller)) + [value] + quick_sort(bigger, len(bigger))
	return result

def find_closest_numbers(ar):
	ar = sorted(ar)
	pairs = []
	smallest = ar[-1]
	for i in range(len(ar) - 1):
		if ar[i + 1] - ar[i] == smallest:
			pairs.append(ar[i])
			pairs.append(ar[i + 1])
		elif ar[i + 1] - ar[i] < smallest:
			pairs = []
			pairs.append(ar[i])
			pairs.append(ar[i + 1])
			smallest = ar[i + 1] - ar[i]
	return pairs

def test(a, b, c):
	print('Input:')
	print(' '.join([str(i) for i in a]))
	if b == c:
		print('Result - correct')
	else:
		print('Result - wrong')
		print('Expected:')
		print(' '.join([str(i) for i in c]))
		print('Outcome:')
		print(' '.join([str(i) for i in b]))

def main():
	n = int(sys.stdin.readline().strip())
	array = sys.stdin.readline().strip().split()
	array = [int(i) for i in array]
	output = sys.stdin.readline().strip().split()
	output = [int(i) for i in output]
	result = find_closest_numbers(array)
	print(' '.join([str(i) for i in result]))
main()


N = int(input())
arr = [int(x) for x in input().split()]
arr.sort()
result = []
diff = 2 ** 31 - 1
for i in range(1, len(arr)):
	if arr[i] - arr[i - 1] <= diff:
		if arr[i] - arr[i - 1] < diff:
			diff = arr[i] - arr[i - 1]
			result = []
		result.append(arr[i])
		result.append(arr[i - 1])
print(' '.join([str(x) for x in sorted(result)]))


x = input()
l = input().split()
z = [int(i) for i in l]
z.sort()
x = []
for i in range(len(l)):
	try:
		c = int(z[i + 1]) - int(z[i])
		x.append(abs(c))
	except:
		pass
m = min(x)
for i in range(len(l)):
	try:
		c = int(z[i + 1]) - int(z[i])
		if c == m:
			print(int(z[i]), int(z[i + 1]), end=' ')
	except:
		pass


def prog(total):
	numbers = sorted([int(x) for x in str(input()).split()])
	differences = {}
	for i in range(len(numbers) - 2 + 1):
		(a, b) = numbers[i:i + 2]
		differences[a, b] = abs(b - a)
	current_min = 1000000000000000
	mins = []
	for i in sorted(differences, key=differences.get):
		if differences[i] <= current_min:
			current_min = differences[i]
			mins.append(i[0])
			mins.append(i[1])
		if differences[i] > current_min:
			break
	print(' '.join([str(x) for x in sorted(mins)]))
prog(int(input()))


import sys
numberOfItems = int(input())
items = [int(x) for x in input().split()]
items.sort()
smallestDistance = sys.maxsize
smallestItems = []
for i in range(len(items) - 1):
	currentDistance = items[i + 1] - items[i]
	if currentDistance < smallestDistance:
		smallestDistance = currentDistance
		smallestItems = []
		smallestItems.append(str(items[i]))
		smallestItems.append(str(items[i + 1]))
	elif currentDistance == smallestDistance:
		smallestItems.append(str(items[i]))
		smallestItems.append(str(items[i + 1]))
print(' '.join(smallestItems))


import sys

def solve(loi):
	loi.sort()
	min_diff = sys.maxsize
	min_pairs = []
	for (i, n) in enumerate(loi):
		if i + 1 >= len(loi):
			break
		else:
			diff = loi[i + 1] - loi[i]
			if diff < min_diff:
				min_diff = diff
				min_pairs = []
				min_pairs.append(loi[i])
				min_pairs.append(loi[i + 1])
			elif diff == min_diff:
				min_pairs.append(loi[i])
				min_pairs.append(loi[i + 1])
	return ' '.join((str(x) for x in min_pairs))
if len(sys.argv) < 2:
	input = sys.stdin
else:
	input = open(sys.argv[1])
input.readline()
loi = [int(x) for x in input.readline().strip().split()]
print(solve(loi))


N = int(input())
array = sorted(list(map(int, input().split())))
min_diff = None
(num1, num2) = ([], [])
for i in range(len(array) - 1):
	diff = array[i + 1] - array[i]
	if min_diff == None or diff < min_diff:
		min_diff = diff
		(num1, num2) = ([array[i]], [array[i + 1]])
	elif diff == min_diff:
		num1.append(array[i])
		num2.append(array[i + 1])
for i in range(len(num1)):
	print(num1[i], num2[i], end=' ')


N = int(input())
ls = [int(x) for x in input().split()]
ls = sorted(ls)
l = []
for i in range(0, N - 1):
	l.append([ls[i], ls[i + 1], ls[i + 1] - ls[i]])
l.sort(key=lambda x: float(x[2]))
s = l[0][2]
res = []
for i in range(0, N - 1):
	if l[i][2] > s:
		break
	res.append(l[i][0])
	res.append(l[i][1])
print(' '.join([str(x) for x in res]))


n = int(input())
l = sorted([int(x) for x in input().split()])
minDiff = float('Inf')
outStr = ''
for i in range(n - 1):
	diff = l[i + 1] - l[i]
	if diff < minDiff:
		minDiff = diff
		outStr = str(l[i]) + ' ' + str(l[i + 1])
	elif diff == minDiff:
		outStr += ' ' + str(l[i]) + ' ' + str(l[i + 1])
print(outStr)


N = int(input())
A = [int(c) for c in input().split()[:N]]
a = sorted(A)
min_items = a[:2]
min_value = abs(a[1] - a[0])
for i in range(1, N - 1):
	d = abs(a[i + 1] - a[i])
	if d == min_value:
		min_items += a[i:i + 2]
	elif d < min_value:
		min_items = a[i:i + 2]
		min_value = d
print(' '.join(map(str, min_items)))


from collections import Counter
n = int(input())
nums = sorted([int(x) for x in str(input()).split()])
result = []
min = None
for i in range(len(nums) - 1):
	current = abs(nums[i] - nums[i + 1])
	if min is None or current < min:
		min = current
for i in range(len(nums) - 1):
	current = abs(nums[i] - nums[i + 1])
	if current == min:
		result += [nums[i], nums[i + 1]]
print(str(result)[1:-1].replace(',', ''))


N = int(input())
ar = list(map(int, input().split()))
ar.sort()
pairs = []
smallest_difference = abs(ar[-1])
for i in range(len(ar) - 1):
	difference = abs(ar[i] - ar[i + 1])
	if difference < smallest_difference:
		smallest_difference = difference
		pairs.clear()
	if difference == smallest_difference:
		pairs.append(ar[i])
		pairs.append(ar[i + 1])
print(' '.join(map(str, pairs)))


def closest(numbers):
	d = {}
	for (i, num) in enumerate(numbers[:-1]):
		difference = numbers[i + 1] - num
		d[difference] = d.get(difference, []) + [num, numbers[i + 1]]
	smallestDifference = min(d.keys())
	return ' '.join((str(x) for x in d[smallestDifference]))
n = int(input())
numbers = [int(x) for x in input().split()]
numbers.sort()
print(closest(numbers))


n = int(input())
l = list(map(int, input().split()))
l.sort()
l1 = []
d1 = abs(l[0] - l[1])
mi = d1
l1.append(l[0])
l1.append(l[1])
for i in range(1, n - 1):
	d1 = abs(l[i] - l[i + 1])
	if d1 < mi:
		mi = d1
		l1 = []
		l1.append(l[i])
		l1.append(l[i + 1])
	elif d1 == mi:
		l1.append(l[i])
		l1.append(l[i + 1])
for j in range(len(l1)):
	print(l1[j], end=' ')


n = int(input())
a = sorted([int(e) for e in input().split()])
mind = float('inf')
mindl = []
for i in range(n - 1):
	if a[i + 1] - a[i] < mind:
		mind = a[i + 1] - a[i]
		mindl = [a[i], a[i + 1]]
	elif a[i + 1] - a[i] == mind:
		mindl.extend((a[i], a[i + 1]))
print(' '.join((str(e) for e in mindl)))


def main():
	N = int(input())

	def makelistsameline(strnos):
		return list(map(lambda x: int(x), strnos.split()))
	a = makelistsameline(input())
	a.sort()
	func(a)

def func(a):
	N = len(a)
	l = []
	p = []

	def dif(a, b):
		return abs(a - b)
	for x in range(1, N):
		l.append(dif(a[x], a[x - 1]))
		t = (a[x - 1], a[x])
		p.append(t)
	minval = min(l)
	for x in range(len(l)):
		if l[x] != minval:
			continue
		else:
			k = p[x]
			print(' '.join(map(str, k)), end=' ')

def randomarray(L):
	r = [random.randint(1, 1000000) for x in range(L)]
	return list(set(r))
main()


n = eval(input())
a = list(map(int, input().split()))
a.sort()
diflist = []
for i in range(len(a) - 1):
	diflist.append([a[i + 1] - a[i], a[i], a[i + 1]])
diflist.sort(key=lambda i: i[0])
m = diflist[0][0]
outlist = [diflist[i][1:] for i in range(len(diflist)) if diflist[i][0] == m]
out = [item for sublist in outlist for item in sublist]
print(' '.join(map(str, out)))


n = int(input())
nbs = sorted([int(i) for i in input().split()])
mini = float('inf')
found = []
for i in range(n - 1):
	tmp = nbs[i + 1] - nbs[i]
	if tmp < mini:
		mini = tmp
		found = [str(nbs[i]) + ' ' + str(nbs[i + 1])]
	elif tmp == mini:
		found.append(str(nbs[i]) + ' ' + str(nbs[i + 1]))
print(' '.join(map(str, found)))


input()
lst = sorted([int(x) for x in input().strip().split(' ')])
min_diff = abs(lst[1] - lst[0])
for i in range(1, len(lst)):
	diff = abs(lst[i] - lst[i - 1])
	if diff < min_diff:
		min_diff = diff
for i in range(1, len(lst)):
	diff = abs(lst[i] - lst[i - 1])
	if diff == min_diff:
		print(str(lst[i - 1]) + ' ' + str(lst[i]), end=' ')
print()


n = int(input())
array = list(map(int, input().split()))
array.sort()
diffs = []
for i in range(len(array) - 1):
	diffs.append(abs(array[i] - array[i + 1]))
mindiff = min(diffs)
for j in range(len(diffs)):
	if diffs[j] == mindiff:
		print(array[j], array[j + 1], end=' ')


def find_closest_numbers(arr):
	min_value = float('inf')
	min_arr = []
	arr = sorted(arr)
	for (index, i) in enumerate(arr):
		if index > 0:
			if abs(i - arr[index - 1]) < min_value:
				min_value = i - arr[index - 1]
				min_arr = [arr[index - 1], i]
			elif abs(i - arr[index - 1]) == min_value:
				min_arr += [arr[index - 1], i]
	return ' '.join(map(str, min_arr))

def main():
	input()
	arr = [int(i) for i in input().split()]
	print(find_closest_numbers(arr))
main()


import sys

def close_pairs(arr):
	arr = sorted(arr)
	diff = sys.maxsize
	res = []
	for i in range(len(arr) - 1):
		delta = arr[i + 1] - arr[i]
		if delta < diff:
			diff = delta
			res = [arr[i], arr[i + 1]]
		elif delta == diff:
			res.extend([arr[i], arr[i + 1]])
		else:
			pass
	return res
_ = input()
arr = [int(x) for x in input().split()]
print(' '.join((str(x) for x in close_pairs(arr))))


nu = int(input())
lnu = list(map(int, str(input()).split()))
lnu.sort()
first = lnu[0]
small_diff = 9999999999999999999999999
result_list = []
for second in lnu[1:]:
	new_diff = second - first
	if new_diff > small_diff:
		first = second
		continue
	if new_diff < small_diff:
		result_list = []
		small_diff = new_diff
	result_list.append(first)
	result_list.append(second)
	first = second
print(' '.join(map(str, result_list)))


input()
stdin = [int(i) for i in input().split()]
stdin.sort()
min = 9999999999
pair = []
for i in range(len(stdin) - 1):
	diff = abs(stdin[i] - stdin[i + 1])
	if diff == 0:
		continue
	if diff < min:
		pair.append(stdin[i])
		pair.append(stdin[i + 1])
		min = diff
		pair = []
	if diff == min:
		pair.append(stdin[i])
		pair.append(stdin[i + 1])
print(' '.join([str(i) for i in pair]))


N = int(input().strip())
arr = [int(i) for i in input().strip().split(' ')]
arr.sort()
iList = []
if arr[0] > arr[1]:
	mindiff = arr[0] - arr[1]
else:
	mindiff = arr[1] - arr[0]
for i in range(len(arr) - 1):
	if arr[i] > arr[i + 1]:
		diff = arr[i] - arr[i + 1]
	else:
		diff = arr[i + 1] - arr[i]
	if mindiff > diff:
		iList = []
		mindiff = diff
		iList.append(i)
	elif mindiff == diff:
		iList.append(i)
ans = ''
for i in iList:
	ans += str(arr[i]) + ' ' + str(arr[i + 1]) + ' '
print(ans)


input()
allVals = sorted(list(map(int, input().split())))
savedVals = []
currentDifference = 0
for i in range(len(allVals) - 1):
	if savedVals == []:
		savedVals.append(allVals[i])
		savedVals.append(allVals[i + 1])
		currentDifference = savedVals[1] - savedVals[0]
	elif currentDifference > abs(allVals[i] - allVals[i + 1]):
		savedVals = []
		savedVals.append(allVals[i])
		savedVals.append(allVals[i + 1])
		currentDifference = savedVals[1] - savedVals[0]
	elif currentDifference == abs(allVals[i] - allVals[i + 1]):
		savedVals.append(allVals[i])
		savedVals.append(allVals[i + 1])
s = ''
for i in range(len(savedVals)):
	s += str(savedVals[i]) + ' '
print(s)


import re

def checkn(z):
	if 1 <= z <= 200000:
		return True

def checkt(z):
	if -10000000 <= z <= 10000000:
		return True

def checkA(a):
	for i in range(len(a) - 1):
		if a[i] == a[i + 1]:
			return False
	return True
n = int(input())
ip = re.findall('(?<!\\S)[+-]?\\d+(?!\\S)', input())
if checkn(n):
	l = []
	for i in ip:
		l.append(int(i))
	if len(l) == n:
		l.sort()
		if checkA(l):
			x = []
			min = 2 * 10 ** 7 + 1
			for i in range(len(l) - 1):
				if abs(l[i] - l[i + 1]) == min:
					x.append((l[i], l[i + 1]))
				if abs(l[i] - l[i + 1]) < min:
					x = []
					min = abs(l[i] - l[i + 1])
					x.append((l[i], l[i + 1]))
			res = ' '
			for i in x:
				res += str(i[0]) + ' ' + str(i[1]) + ' '
			print(res.strip())


input()
a = sorted(map(int, input().split()))
n1 = len(a) - 1
mind = min((a[i + 1] - a[i] for i in range(n1)))

def v():
	for i in range(n1):
		if a[i + 1] - a[i] == mind:
			yield a[i]
			yield a[i + 1]
print(*v())


n = int(input(''))
list = []
temp = input('').split()
for i in range(0, n):
	list.append(int(temp[i]))
list.sort()
difference = 10000000
list_res = []
for k in range(0, n - 1):
	if list[k + 1] - list[k] <= difference:
		difference = list[k + 1] - list[k]
for r in range(0, n - 1):
	if list[r + 1] - list[r] <= difference:
		list_res.append(list[r])
		list_res.append(list[r + 1])
for h in range(0, len(list_res)):
	print(str(list_res[h]) + ' ', end='')


N = int(input())
data = sorted([int(x) for x in input().split()])
caled = []
small_def = data[1] - data[0]
for i in range(1, N):
	difference = data[i] - data[i - 1]
	if difference <= small_def:
		caled.append([data[i - 1], data[i], difference])
	if difference <= small_def:
		small_def = difference
out = ''
for j in caled:
	if j[2] == small_def:
		out += str(j[0]) + ' ' + str(j[1]) + ' '
print(out[:-1])


import sys
n = int(sys.stdin.readline())
array = sorted(map(int, sys.stdin.readline().split()))
min_diff = min((b - a for (a, b) in zip(array, array[1:])))
print(*sum(([a, b] for (a, b) in zip(array, array[1:]) if b - a == min_diff), []))


import sys
n = int(sys.stdin.readline())
array = sorted(map(int, sys.stdin.readline().split()))
min_diff = min((b - a for (a, b) in zip(array, array[1:])))
print(*sum(([a, b] for (a, b) in zip(array, array[1:]) if b - a == min_diff), []))


n = int(input())
array = sorted(map(int, input().split()))
diff = array[-1] - array[0]
for i in range(n - 1):
	if array[i + 1] - array[i] < diff:
		diff = array[i + 1] - array[i]
for i in range(n - 1):
	if array[i + 1] - array[i] == diff:
		print('{} {} '.format(array[i], array[i + 1]), end='')


import sys
n = int(sys.stdin.readline())
array = sorted(map(int, sys.stdin.readline().split()))
min_diff = min((b - a for (a, b) in zip(array, array[1:])))
print(*sum(([a, b] for (a, b) in zip(array, array[1:]) if b - a == min_diff), []))


N = int(input())
vector = sorted([int(el) for el in input().split()])
min_diff = float('inf')
if len(vector) < 2:
	print('')
else:
	pairs = []
	for index in range(len(vector[1:])):
		diff = abs(vector[index + 1] - vector[index])
		if diff == min_diff:
			pairs.extend([vector[index], vector[index + 1]])
		elif diff < min_diff:
			pairs = [vector[index], vector[index + 1]]
			min_diff = diff
	print(*pairs)


import sys
n = int(sys.stdin.readline())
array = sorted(map(int, sys.stdin.readline().split()))
min_diff = min((b - a for (a, b) in zip(array, array[1:])))
print(*sum(([a, b] for (a, b) in zip(array, array[1:]) if b - a == min_diff), []))