Given an array of integers, find the longest subarray where the absolute difference between any two elements is less than or equal to $\mbox{1}$.  

Example  

$a=[1,1,2,2,4,4,5,5,5]$  

There are two subarrays meeting the criterion: $[1,1,2,2]$ and $[4,4,5,5,5]$.  The maximum length subarray has $5$ elements.

Function Description  

Complete the pickingNumbers function in the editor below.  

pickingNumbers has the following parameter(s):  

int a[n]: an array of integers  

Returns  

int:  the length of the longest subarray that meets the criterion  

Input Format

The first line contains a single integer $n$, the size of the array $\boldsymbol{a}$. 

The second line contains $n$ space-separated integers, each an $a[i]$.

Constraints

$2\leq n\leq100$
$0<a[i]<100$
The answer will be $\geq2$. 

Sample Input 0
6
4 6 5 3 3 1

Sample Output 0
3

Explanation 0

We choose the following multiset of integers from the array: $\{4,3,3\}$. Each pair in the multiset has an absolute difference $\leq1$ (i.e., $|4-3|=1$ and $|3-3|=0$), so we print the number of chosen integers, $3$, as our answer.

Sample Input 1
6
1 2 2 3 1 2

Sample Output 1
5

Explanation 1

We choose the following multiset of integers from the array: $\{1,2,2,1,2\}$.  Each pair in the multiset has an absolute difference $\leq1$ (i.e., $|1-2|=1$, $|1-1|=0$, and $|2-2|=0$), so we print the number of chosen integers, $5$, as our answer.

Difficulty: EASY
Tags: Implementation

Starter code:

Solutions:
import sys
n = int(input().strip())
a = [int(a_temp) for a_temp in input().strip().split(' ')]
from collections import Counter
d = Counter(a)
best = 0
for i in range(99):
	best = max(d[i] + d[i + 1], best)
print(best)


import sys
n = int(input().strip())
a = [int(a_temp) for a_temp in input().strip().split(' ')]
dic = {}
for v in a:
	dic[v] = dic.get(v, 0) + 1
	dic[v + 1] = dic.get(v + 1, 0) + 1
print(max(dic.values()))


import sys

def solve(aa):
	cache = [0] * 101
	for a in aa:
		cache[a] += 1
	mx = 0
	for i in range(1, len(cache)):
		v = cache[i - 1] + cache[i]
		if v > mx:
			mx = v
	return mx
n = int(input().strip())
a = [int(a_temp) for a_temp in input().strip().split(' ')]
print(solve(a))


import sys
n = int(input().strip())
a = [int(a_temp) for a_temp in input().strip().split(' ')]
d = {x: 0 for x in range(1, 100)}
for i in a:
	d[i] = d.get(i, 0) + 1
maxi = 0
arr = list(d.keys())
for i in range(1, 99):
	maxi = max(maxi, d[i] + d[i + 1])
print(maxi)


import sys
from collections import Counter, defaultdict, deque
N = int(input().strip())
A = [int(a_temp) for a_temp in input().strip().split(' ')]
Ac = Counter(A)
ans = 0
for (a, c) in Ac.items():
	ans = max(ans, c + Ac[a - 1], c + Ac[a + 1])
print(ans)


import sys
n = int(input().strip())
a = [int(a_temp) for a_temp in input().strip().split(' ')]
number_set = set(a)
max_score = 0

def calculate_score(number, array):
	number_count = a.count(number)
	number_minus_count = a.count(number - 1)
	number_plus_count = a.count(number + 1)
	number_count += max(number_minus_count, number_plus_count)
	return number_count
for number in number_set:
	current_score = calculate_score(number, a)
	if current_score > max_score:
		max_score = current_score
print(max_score)


import sys
n = int(input().strip())
a = [int(a_temp) for a_temp in input().strip().split(' ')]
counts = {}
for i in a:
	if i not in counts:
		counts[i] = 1
	else:
		counts[i] += 1
curmax = None
for k in counts:
	curcount = counts[k]
	leftcount = 0
	rightcount = 0
	if k - 1 in counts:
		leftcount = counts[k - 1]
	if k + 1 in counts:
		rightcount = counts[k + 1]
	thismax = curcount + max(leftcount, rightcount)
	if curmax == None or thismax > curmax:
		curmax = thismax
print(curmax)


import sys
n = int(input().strip())
a = [int(a_temp) for a_temp in input().strip().split(' ')]
from collections import Counter
c = Counter(a)
print(max((c[i] + c[i + 1] for i in range(100))))


import sys
n = int(input().strip())
a = [int(a_temp) for a_temp in input().strip().split(' ')]
from collections import Counter
counter = Counter(a)
result = 0
for (k, v) in counter.items():
	tmp = v
	(tmp1, tmp2) = (0, 0)
	if k - 1 in counter:
		tmp1 = counter[k - 1]
	if k + 1 in counter:
		tmp2 = counter[k + 1]
	tmp += max(tmp1, tmp2)
	if tmp > result:
		result = tmp
print(result)


import sys
n = int(input().strip())
a = [int(a_temp) for a_temp in input().strip().split(' ')]
low = min(a)
up = max(a)
lis = []
for i in range(low, up + 1):
	lis.append(a.count(i))
maa = 0
if up == low:
	maa = lis[0]
for i in range(0, up - low):
	if lis[i] + lis[i + 1] > maa:
		maa = lis[i] + lis[i + 1]
print(maa)


n = int(input())
l = [int(x) for x in input().split()]
l.sort()
t = 0
s = []
i = 0
while i < n - 1:
	s.append([l[i]])
	while i < n - 1:
		if l[i + 1] - min(s[len(s) - 1]) <= 1:
			s[len(s) - 1].append(l[i + 1])
			i += 1
		else:
			i += 1
			break
print(max([len(i) for i in s]))


import sys

def count(n, a):
	return len(list(filter(lambda x: x == n, a)))
n = int(input().strip())
a = [int(a_temp) for a_temp in input().strip().split(' ')]
mi = min(a)
ma = max(a)
maxCount = -1
if mi == ma:
	print(len(a))
else:
	for i in range(mi, ma):
		c = count(i, a) + count(i + 1, a)
		if c > maxCount:
			maxCount = c
	print(maxCount)


import sys
n = int(input().strip())
a = [int(a_temp) for a_temp in input().strip().split(' ')]
a.sort()
cnt = 0
ans = 0
for i in range(n):
	for j in range(i + 1, n):
		if a[j] - a[i] <= 1:
			cnt += 1
		else:
			ans = max(ans, cnt)
			cnt = 0
			break
	ans = max(ans, cnt)
	cnt = 0
print(ans + 1 if n != 2 else '2')


n = int(input())
a = [int(x) for x in input().split()]
b = [0] * 100
for i in range(n):
	b[a[i]] = b[a[i]] + 1
mx = 0
for i in range(99):
	mx = max(b[i] + b[i + 1], mx)
print(mx)


import sys
n = int(input().strip())
a = [int(a_temp) for a_temp in input().strip().split(' ')]
w = {e: [] for e in a}
for e in a:
	if e - 1 in w:
		w[e - 1].append(e)
	w[e].append(e)
print(max(map(len, w.values())))


import sys
n = int(input().strip())
a = sorted([int(a_temp) for a_temp in input().strip().split(' ')])
elems = [a[0]]
counts = [1]
for i in range(1, len(a)):
	if elems[-1] == a[i]:
		counts[-1] += 1
	else:
		elems.append(a[i])
		counts.append(1)
max_subseq = counts[-1]
for i in range(0, len(elems) - 1):
	now_count = counts[i]
	if abs(elems[i + 1] - elems[i]) == 1:
		now_count += counts[i + 1]
	max_subseq = max(max_subseq, now_count)
print(max_subseq)


import sys
n = int(input().strip())
a = [int(a_temp) for a_temp in input().strip().split(' ')]
d = [0] * 120
for i in a:
	d[i] += 1
ans = 0
for i in range(110):
	ans = max(ans, d[i] + d[i + 1])
print(ans)


import sys
n = int(input().strip())
a = [int(a_temp) for a_temp in input().strip().split(' ')]
lst = [0] * 101
count = 0
for i in a:
	lst[i] += 1
for i in range(1, 100):
	count = max(count, lst[i] + lst[i + 1])
print(count)


n = int(input())
A = [int(x) for x in input().split()]
count = 0
for i in range(0, n):
	localMax = 1
	for j in range(0, n):
		if i == j:
			continue
		diff = A[i] - A[j]
		if diff == 1 or diff == 0:
			localMax += 1
	if localMax > count:
		count = localMax
print(count)


import sys
n = int(input().strip())
a = [int(a_temp) for a_temp in input().strip().split(' ')]
a.sort()
b = []
i = 0
while i < len(a):
	j = i + 1
	x = 1
	while j < len(a) and a[j] == a[i]:
		x += 1
		j += 1
	b.append((x, a[i]))
	i = j
sol = 0
for i in range(0, len(b) - 1):
	if b[i + 1][1] - b[i][1] == 1:
		sol = max(sol, b[i][0] + b[i + 1][0])
	else:
		sol = max(sol, b[i][0])
sol = max(sol, b[-1][0])
print(sol)


from collections import Counter
n = int(input().strip())
a = [int(a_temp) for a_temp in input().strip().split(' ')]
a = Counter(a)
ans = max((a[i] + a[i + 1] for i in range(100)))
print(ans)


n = int(input())
a = [int(num) for num in input().split()]
m = 0
c = 0
for i in range(n):
	p = a.count(a[i])
	q = a.count(a[i] + 1)
	r = a.count(a[i] - 1)
	ma = max(p + q, p + r)
	m = max(m, ma)
print(m)


import sys
n = int(input().strip())
a = [int(a_temp) for a_temp in input().strip().split(' ')]
cnt = [0 for _ in range(max(a) + 1)]
for num in a:
	cnt[num] += 1
res = 0
for i in range(1, len(cnt)):
	res = max(res, cnt[i] + cnt[i - 1])
print(res)


import sys
n = int(input().strip())
a = [int(a_temp) for a_temp in input().strip().split(' ')]
countArr = [0] * (max(a) + 1)
for i in a:
	countArr[i] += 1
maxRepeatedNum = countArr.index(max(countArr))
ans = 2
for i in range(len(countArr) - 1):
	adjacentSum = countArr[i] + countArr[i + 1]
	if ans < adjacentSum:
		ans = adjacentSum
print(ans)


n = int(input())
L = list(map(int, input().split()))
L.sort()
mini = L[0]
maxi = L[-1]
if mini == maxi:
	print(len(L))
else:
	c = [L.count(x) for x in range(mini, maxi + 1)]
	nc = [c[x] + c[x + 1] for x in range(len(c) - 1)]
	print(max(nc))


n = int(input().strip())
a = [int(a_temp) for a_temp in input().strip().split(' ')]
counts = [0] * 99
for num in a:
	counts[num - 1] += 1
maxval = 2
for i in range(len(counts) - 1):
	result = counts[i] + counts[i + 1]
	if result > maxval:
		maxval = result
print(maxval)


n = int(input())
arr = [int(x) for x in input().split(' ')]
arr = sorted(arr)
maxc = 0
maxmax = 1
max_limit = 0
min_limit = 0
for (i, x) in enumerate(arr):
	if i == 0:
		maxc += 1
		max_limit = x
		min_limit = x
		continue
	if abs(x - max_limit) <= 1 and abs(x - min_limit) <= 1:
		maxc += 1
		max_limit = max(x, max_limit)
		min_limit = min(x, min_limit)
		if maxc > maxmax:
			maxmax = maxc
	else:
		maxc = 1
		max_limit = x
		min_limit = x
print(maxmax)


n = int(input().strip())
a = [int(a_temp) for a_temp in input().strip().split(' ')]
ae = list(a)
ae.remove(max(a))
ae.remove(min(a))

def count(array, a, b):
	return len([i for i in array if a <= i <= b])
ms = []
for i in ae:
	ms += [count(a, i, i + 1)]
	ms += [count(a, i - 1, i)]
print(max(ms))


import sys
n = int(input().strip())
a = [int(a_temp) for a_temp in input().strip().split(' ')]
longestRun = 0
thisRun = 0
a = sorted(a)
for i in range(n):
	thisRun = 1
	for j in range(i + 1, n):
		if a[j] - a[i] <= 1:
			thisRun += 1
	if thisRun > longestRun:
		longestRun = thisRun
print(longestRun)


import sys
f = 0
k = 1
n = int(input().strip())
a = [int(a_temp) for a_temp in input().strip().split(' ')]
if n >= 2 and n <= 100:
	for i in range(len(a)):
		if a[i] <= 0 or a[i] >= 100:
			f = 1
			break
	if f == 0:
		z = 1
		a = sorted(a)
		for i in range(0, len(a)):
			k = 1
			for j in range(i + 1, len(a)):
				if a[j] - a[i] <= 1:
					k = k + 1
				if k > z:
					z = k
		print(z)


import sys
n = int(input().strip())
a = [int(a_temp) for a_temp in input().strip().split(' ')]
t = list(set(a))
t.sort()
maxi = 0
for i in range(len(t)):
	temp = a.count(t[i])
	if t[i] + 1 in t:
		temp = a.count(t[i]) + a.count(t[i] + 1)
	if temp > maxi:
		maxi = temp
print(maxi)


from itertools import groupby
n = input()
a = [int(x) for x in input().split()]
g = [(x[0], len(list(x[1]))) for x in groupby(sorted(a))]
best = -1
for i in range(len(g)):
	best = max(best, g[i][1])
	if i > 0 and abs(g[i][0] - g[i - 1][0]) < 1:
		best = max(best, g[i][1] + g[i - 1][1])
	if i < len(g) - 1 and abs(g[i][0] - g[i + 1][0]) <= 1:
		best = max(best, g[i][1] + g[i + 1][1])
print(best)


import sys
n = int(input().strip())
a = [int(a_temp) for a_temp in input().strip().split(' ')]
a = sorted(a)
max_l = 0
for i in range(len(a)):
	for j in range(i + 1, len(a)):
		if a[j] - a[i] <= 1:
			if j != len(a) - 1:
				continue
			else:
				max_l = max(max_l, j - i + 1)
				break
		else:
			max_l = max(max_l, j - i)
			break
print(max_l)


import sys
from collections import Counter
n = int(input().strip())
a = [int(a_temp) for a_temp in input().strip().split(' ')]
c = Counter(a)
if len(c) == 1:
	print(n)
else:
	m = []
	for k in c:
		m.append(c[k])
		if k - 1 in c:
			m.append(c[k] + c[k - 1])
		if k + 1 in c:
			m.append(c[k] + c[k + 1])
	print(max(m))


import sys
n = int(input().strip())
a = [int(a_temp) for a_temp in input().strip().split(' ')]
a = sorted(a)
l = 0
ans = 0
sz = int(len(a))
i = 1
while i < sz:
	if abs(a[i] - a[l]) < 2:
		ans = max(ans, i - l + 1)
		i += 1
	else:
		l += 1
print(ans)


n = int(input())
a = input().split()
for i in range(len(a)):
	a[i] = int(a[i])
a.sort()
c = 0
d = 0
max = 0
for i in range(len(a) - 1):
	if a[i] == a[i + 1]:
		c += 1
	if a[i] + 1 == a[i + 1]:
		if d == 0:
			d = 1
			c += 1
		else:
			d = 0
			c = 0
			i = a.index(a[i])
	if a[i] + 1 < a[i + 1]:
		d = 0
		c = 0
		i = a.index(a[i])
	if max <= c:
		max = c
print(max + 1)


import sys
n = int(input().strip())
a = [int(a_temp) for a_temp in input().strip().split(' ')]
ls = []
for j in range(n):
	num = a[j]
	counteq = 0
	countlow = 0
	counthigh = 0
	for i in a:
		if i == num:
			counteq = counteq + 1
	for i in a:
		if i == num - 1:
			countlow = countlow + 1
	for i in a:
		if i == num + 1:
			counthigh = counthigh + 1
	if counthigh > countlow:
		final = counteq + counthigh
	else:
		final = counteq + countlow
	ls.append(final)
print(max(ls))


import sys
n = int(input().strip())
a = [int(a_temp) for a_temp in input().strip().split(' ')]
counts = [0] * 101
for x in a:
	counts[x] += 1
counts2 = [0] * 101
for i in range(len(counts) - 1):
	counts2[i] = counts[i] + counts[i + 1]
print(max(counts2))


import sys
n = int(input().strip())
a = [int(a_temp) for a_temp in input().strip().split(' ')]
a.sort()
m = 0
t = 0
f = 0
l = 0
v = a[0]
for i in a:
	if v != i:
		if l == v - 1 and m < t + f:
			m = t + f
		if f > m:
			m = f
		l = v
		t = f
		f = 0
		v = i
	if i == v:
		f += 1
if l == v - 1 and m < t + f:
	m = t + f
if f > m:
	m = f
print(m)


import sys
n = int(input().strip())
a = [int(a_temp) for a_temp in input().strip().split(' ')]
new_a = sorted(list(set(a)))
maxi = -1
store = []
for x in range(len(new_a) - 1):
	if a.count(new_a[x + 1]) + a.count(new_a[x]) > maxi and abs(new_a[x + 1] - new_a[x]) <= 1:
		maxi = a.count(new_a[x + 1]) + a.count(new_a[x])
	store.append(a.count(new_a[x]))
store.append(a.count(new_a[len(new_a) - 1]))
if len(store) != 0:
	print(max(maxi, max(store)))
else:
	print(maxi)


import sys
n = int(input().strip())
a = [int(a_temp) for a_temp in input().strip().split(' ')]
c = 0
for (i, x) in enumerate(a):
	temp1 = 0
	temp2 = 0
	for y in a[i + 1:]:
		if -1 <= x - y <= 0:
			temp1 += 1
		if 0 <= x - y <= 1:
			temp2 += 1
	if temp1 > c:
		c = temp1
	if temp2 > c:
		c = temp2
print(c + 1)


import sys
n = int(input().strip())
a = [int(a_temp) for a_temp in input().strip().split(' ')]
d = {}
for i in a:
	if i not in d:
		d[i] = 1
	else:
		d[i] += 1
m = 0
for i in d:
	x = 0
	if i - 1 in d:
		x = d[i - 1]
	m = max(m, d[i] + x)
print(m)


import sys
n = int(input().strip())
a = [int(a_temp) for a_temp in input().strip().split(' ')]
ac = {}
for i in a:
	if i not in ac:
		ac[i] = 1
	else:
		ac[i] += 1
res = 0
for i in ac:
	if i - 1 in ac:
		res = max(res, ac[i] + ac[i - 1])
	if i + 1 in ac:
		res = max(res, ac[i] + ac[i + 1])
	res = max(res, ac[i])
print(res)


import sys
n = int(input().strip())
a = [int(a_temp) for a_temp in input().strip().split(' ')]
new_list = []
for i in range(n):
	dist = [a[i]]
	for (index, aa) in enumerate(a):
		if abs(aa - a[i]) <= 1 and index != i:
			all = True
			for j in range(len(dist)):
				if abs(dist[j] - aa) > 1:
					all = False
			if all:
				dist.append(aa)
	new_list.append(dist)
print(max(list(map(lambda x: len(x), new_list))))


import sys
n = int(input().strip())
a = [int(a_temp) for a_temp in input().strip().split(' ')]
k = 0
s = []
a.sort()
for i in range(n):
	k = 0
	t = 0
	c = 0
	for j in range(n):
		if a[i] - a[j] == 0:
			k += 1
		if a[i] - a[j] == 1:
			t += 1
		if a[i] - a[j] == -1:
			c += 1
	s.append(k + t)
	s.append(c + k)
print(max(s))


n = int(input())
a = sorted(map(int, input().split()))
r = 0
for i in range(n):
	j = i + 1
	while j < n and abs(a[i] - a[j]) <= 1:
		j += 1
	r = max(r, j - i)
print(r)


n = int(input().strip())
a = [int(a_temp) for a_temp in input().strip().split(' ')]
a = list(sorted(a))
list1 = []
count = 0
for i in range(n):
	count = 0
	for j in range(i, n):
		if abs(a[i] - a[j]) <= 1:
			count += 1
		else:
			break
	list1.append(count)
print(max(list1))


import sys
n = int(input().strip())
a = [int(a_temp) for a_temp in input().strip().split(' ')]
newa = list(set(a))
c = []
for number in newa:
	if number + 1 in newa:
		count = a.count(number) + a.count(number + 1)
		c.append(count)
	elif number - 1 in newa:
		count = a.count(number) + a.count(number - 1)
		c.append(count)
	else:
		c.append(a.count(number))
print(max(c))


import sys
import itertools
n = int(input().strip())
a = [int(a_temp) for a_temp in input().strip().split(' ')]

def matches(l):
	return all([abs(x[0] - x[1]) <= 1 for x in itertools.combinations(l, 2)])
results = []

def check_list(l):
	if len(l) == 0:
		return
	m = max(l)
	test = [x for x in l if abs(x - m) <= 1]
	results.append(len(test))
	check_list(list(filter(lambda x: x != m, l)))
check_list(a)
print(max(results))


n = int(input())
a = [int(i) for i in input().split()]
m = [0] * 100
for i in a:
	m[i] += 1
mx = 0
for i in range(1, 100):
	if m[i] + m[i - 1] > mx:
		mx = m[i] + m[i - 1]
print(mx)


import sys
n = int(input().strip())
a = [int(a_temp) for a_temp in input().strip().split(' ')]
arr = []
for i in range(100):
	arr.append(0)
for i in a:
	arr[i] += 1
total = 0
for i in range(99):
	if arr[i] + arr[i + 1] > total:
		total = arr[i] + arr[i + 1]
print(total)


import sys
n = int(input().strip())
a = [int(a_temp) for a_temp in input().strip().split(' ')]
index = [0] * 100
max_sum = 0
for i in range(0, n):
	index[a[i]] = index[a[i]] + 1
for i in range(1, 100):
	if index[i] + index[i - 1] > max_sum:
		max_sum = index[i] + index[i - 1]
print(max_sum)


import sys
n = int(input().strip())
a = [int(a_temp) for a_temp in input().strip().split(' ')]
l = [0] * 99
l1 = []
for i in a:
	l[i - 1] += 1
for j in range(98):
	l1.append(l[j] + l[j + 1])
print(max(l1))


import sys
n = int(input().strip())
a = [int(a_temp) for a_temp in input().strip().split(' ')]
s = {}
for i in a:
	s[i] = s.get(i, 0) + 1
	s[i + 1] = s.get(i + 1, 0) + 1
c = max(s.values())
print(c)


import sys
from collections import Counter
n = int(input().strip())
a = [int(a_temp) for a_temp in input().strip().split(' ')]
numcounts = dict(Counter(a))
maxcounts = 0
for i in numcounts.keys():
	count1 = numcounts.get(i - 1, 0)
	count2 = numcounts.get(i, 0)
	count3 = numcounts.get(i + 1, 0)
	intercount1 = count1 + count2
	intercount2 = count2 + count3
	intercount = max(intercount1, intercount2)
	if intercount > maxcounts:
		maxcounts = intercount
print(maxcounts)


import sys
n = int(input().strip())
a = [int(a_temp) for a_temp in input().strip().split(' ')]
count = {}
for i in a:
	count[i] = count.get(i, 0) + 1
	if i > 0:
		count[i - 1] = count.get(i - 1, 0) + 1
max_count = 0
for (i, c) in count.items():
	if max_count < c:
		max_count = c
print(max_count)


import sys
n = int(input().strip())
a = [int(a_temp) for a_temp in input().strip().split(' ')]
count = {}
for nb in a:
	if nb in count:
		count[nb] += 1
	else:
		count[nb] = 1
map_lst = list(count.keys())
sorted(map_lst)
max_nb = 0
for i in range(len(map_lst) - 1):
	first = map_lst[i]
	second = map_lst[i + 1]
	if second - first <= 1 and count[first] + count[second] > max_nb:
		max_nb = count[map_lst[i]] + count[map_lst[i + 1]]
	elif count[first] > max_nb:
		max_nb = count[first]
last = map_lst[len(map_lst) - 1]
if count[last] > max_nb:
	max_nb = count[last]
print(max_nb)


from collections import Counter
n = int(input().strip())
a = [int(a_temp) for a_temp in input().strip().split(' ')]
c = Counter(a)
ans = 0
for (i, j) in zip(sorted(c), sorted(c)[1:]):
	tmp = c[i] + c[j]
	if j - i == 1 and tmp > ans:
		ans = tmp
print(max(ans, max(c.values())))


import sys
n = int(input().strip())
a = [int(a_temp) for a_temp in input().strip().split(' ')]
max = 0
a = sorted(a)
for i in range(n - 1):
	nbr = 1
	for j in range(i + 1, n):
		if a[j] - a[i] <= 1:
			nbr += 1
			if nbr > max:
				max = nbr
		else:
			if nbr > max:
				max = nbr
			break
print(max)


import sys
n = int(input().strip())
a = [int(a_temp) for a_temp in input().strip().split(' ')]
a.sort()
high = 0
for i in range(n - 1):
	count = 0
	for j in range(i + 1, n):
		if a[j] - a[i] <= 1:
			count += 1
		else:
			break
	if count > high:
		high = count
print(high + 1)


import sys
n = int(input().strip())
a = [int(a_temp) for a_temp in input().strip().split(' ')]
print(max([a.count(b - 1) + a.count(b) for b in set(a)]))


import sys
n = int(input().strip())
a = [int(a_temp) for a_temp in input().strip().split(' ')]
a.sort()
max = 0
for i in range(len(a)):
	count = 0
	for j in range(i, len(a)):
		if -1 <= a[i] - a[j] <= 1:
			count += 1
		else:
			break
	if count > max:
		max = count
print(max)


import sys
n = int(input().strip())
a = [int(a_temp) for a_temp in input().strip().split(' ')]
b = len(a)
max = 0
for i in range(b):
	count = 0
	count1 = 0
	for j in range(i, b):
		if a[i] - a[j] == 1:
			count += 1
		elif a[i] - a[j] == -1:
			count1 += 1
		elif a[i] - a[j] == 0:
			count += 1
			count1 += 1
	if count1 > count:
		count = count1
	if count > max:
		max = count
print(max)


import sys
n = int(input().strip())
a = [int(a_temp) for a_temp in input().strip().split(' ')]
counts = {}
for num in a:
	if num in counts:
		counts[num] += 1
	else:
		counts[num] = 1
maxCombo = 0
for num in counts.keys():
	combo = counts[num]
	if num - 1 in counts:
		combo += counts[num - 1]
	if combo > maxCombo:
		maxCombo = combo
print(maxCombo)


import sys
n = int(input().strip())
a = [int(a_temp) for a_temp in input().strip().split(' ')]
a.sort()
max1 = []
maxx = 1
for i in range(len(a) - 1):
	maxx = 1
	for j in range(i + 1, len(a)):
		if a[j] - a[i] < 2:
			maxx += 1
			max1.append(maxx)
		else:
			max1.append(maxx)
			break
print(max(max1))


import sys
n = int(input().strip())
a = [int(a_temp) for a_temp in input().strip().split(' ')]
arr = [0] * 101
for i in a:
	arr[i] += 1
maximum = 0
i = 1
while i < n:
	temp = arr[i] + arr[i - 1]
	if temp > maximum:
		maximum = temp
	i += 1
print(maximum)


import sys
n = int(input().strip())
a = [int(a_temp) for a_temp in input().strip().split(' ')]
s = []
for i in range(min(a), max(a) + 1):
	s1 = a.count(i)
	s2 = a.count(i + 1)
	s.append(s1 + s2)
print(max(s))


n = int(input().strip())
a = [int(a_temp) for a_temp in input().strip().split(' ')]
d = {}
count = 0
selected = 0
for itm in a:
	count = d.get(itm, 0)
	count += 1
	d[itm] = count
	tmp1 = count + d.get(itm - 1, 0)
	tmp2 = count + d.get(itm + 1, 0)
	selected = max(tmp1, tmp2, selected)
print(selected)


n = int(input().strip())
a = [int(a_temp) for a_temp in input().strip().split(' ')]
d = {}
count = 0
selected = 0
for itm in a:
	count = d.get(itm, 0)
	count += 1
	d[itm] = count
	tmp1 = count + d.get(itm - 1, 0)
	tmp2 = count + d.get(itm + 1, 0)
	selected = max(tmp1, tmp2, selected)
print(selected)


import sys
n = int(input().strip())
a = [int(a_temp) for a_temp in input().strip().split(' ')]
setA = set(a)
maxnum = 0
for ele_a in setA:
	countA = a.count(ele_a)
	countAmin1 = a.count(ele_a - 1)
	countAplus1 = a.count(ele_a + 1)
	maxnum = max(maxnum, countA + countAmin1, countA + countAplus1)
print(maxnum)


n = int(input().strip())
a = [int(a_temp) for a_temp in input().strip().split(' ')]
d = {}
count = 0
selected = 0
for itm in a:
	count = d.get(itm, 0)
	count += 1
	d[itm] = count
	tmp1 = count + d.get(itm - 1, 0)
	tmp2 = count + d.get(itm + 1, 0)
	selected = max(tmp1, tmp2, selected)
print(selected)


n = int(input())
l = list(map(int, input().split()))
a = [0] * 101
for x in l:
	a[x] += 1
ans = 0
for i in range(100):
	if a[i] + a[i + 1] > ans:
		ans = a[i] + a[i + 1]
print(ans)


import sys
n = int(input().strip())
l = [int(a_temp) for a_temp in input().strip().split(' ')]
l.sort()
start = 0
cnt = 0
max = 0
while start < n:
	i = start + 1
	while i < n:
		if l[i] - l[start] < 2:
			cnt += 1
		i += 1
	if cnt > max:
		max = cnt
	cnt = 0
	start = start + 1
print(max + 1)


n = int(input().strip())
a = [int(a_temp) for a_temp in input().strip().split(' ')]
d = {}
count = 0
selected = 0
for itm in a:
	count = d.get(itm, 0)
	count += 1
	d[itm] = count
	tmp1 = count + d.get(itm - 1, 0)
	tmp2 = count + d.get(itm + 1, 0)
	selected = max(tmp1, tmp2, selected)
print(selected)


from collections import defaultdict
import sys
n = int(input().strip())
a = [int(a_temp) for a_temp in input().strip().split(' ')]
cnt = defaultdict(int)
for number in a:
	cnt[number] += 1
	cnt[number + 1] += 1
print(max(cnt.values()))


n = int(input().strip())
a = [int(a_temp) for a_temp in input().strip().split(' ')]
d = {}
count = 0
selected = 0
for itm in a:
	count = d.get(itm, 0)
	count += 1
	d[itm] = count
	tmp1 = count + d.get(itm - 1, 0)
	tmp2 = count + d.get(itm + 1, 0)
	selected = max(tmp1, tmp2, selected)
print(selected)


import sys
n = int(input().strip())
a = [int(a_temp) for a_temp in input().strip().split(' ')]
dp = [0] * 101
for a0 in a:
	dp[a0] += 1
result = 0
for i in range(100):
	result = max(dp[i] + dp[i + 1], result)
print(result)


n = int(input().strip())
a = [int(a_temp) for a_temp in input().strip().split(' ')]
d = {}
count = 0
selected = 0
for itm in a:
	count = d.get(itm, 0)
	count += 1
	d[itm] = count
	tmp1 = count + d.get(itm - 1, 0)
	tmp2 = count + d.get(itm + 1, 0)
	selected = max(tmp1, tmp2, selected)
print(selected)


import sys
n = int(input().strip())
a = [int(a_temp) for a_temp in input().strip().split(' ')]
u = []
for i in a:
	b = []
	b.append(i)
	t = 0
	for j in a:
		c = i - j
		if c == 0:
			t += 1
			if t > 1:
				b.append(j)
		elif c == 1 | c == -1:
			b.append(j)
		else:
			pass
	f = len(b)
	d = len(u)
	if d < f:
		u = []
		for i in b:
			u.append(i)
print(len(u))


import sys
from collections import Counter
n = int(input().strip())
a = [int(a_temp) for a_temp in input().strip().split(' ')]
cnt = Counter(a)
max_seen = 0
for i in cnt:
	max_seen = max(max_seen, cnt[i] + cnt[i - 1], cnt[i] + cnt[i + 1])
print(max_seen)


import sys
n = int(input().strip())
a = [int(a_temp) for a_temp in input().strip().split(' ')]
value = 0
a_dict = {}
max_selected = 0
for i in a:
	value = a_dict.get(i, 0)
	value += 1
	a_dict[i] = value
	tmp1 = value + a_dict.get(i - 1, 0)
	tmp2 = value + a_dict.get(i + 1, 0)
	max_selected = max(tmp1, tmp2, max_selected)
print(max_selected)


import sys
n = int(input().strip())
a = [int(a_temp) for a_temp in input().strip().split(' ')]
a.sort()
d = {}
mx = 0
for i in range(n):
	if a[i] not in d:
		d[a[i]] = 1
	else:
		d[a[i]] += 1
	sm = d[a[i]] + d.get(a[i] - 1, 0)
	if sm > mx:
		mx = sm
print(mx)


import sys
n = int(input().strip())
a = [int(a_temp) for a_temp in input().strip().split(' ')]
a.sort()
count = 0
for i in range(n):
	temp = 1
	for j in range(i + 1, n):
		if a[j] - a[i] > 1:
			break
		temp += 1
	if temp > count:
		count = temp
print(count)


import sys
n = int(input().strip())
a = [int(a_temp) for a_temp in input().strip().split(' ')]
d = {}
count = 0
selected = 0
for x in a:
	count = d.get(x, 0)
	count += 1
	d[x] = count
	temp1 = count + d.get(x - 1, 0)
	temp2 = count + d.get(x + 1, 0)
	selected = max(temp1, temp2, selected)
print(selected)


n = int(input().strip())
a = [int(a_temp) for a_temp in input().strip().split(' ')]
m = -1
for i in range(len(a)):
	s = [x for x in a if x == a[i] - 1 or x == a[i]]
	if len(s) > m:
		m = len(s)
print(m)


import sys
n = int(input().strip())
a = [int(a_temp) for a_temp in input().strip().split(' ')]
D = {}
for e in a:
	if e in D:
		D[e] += 1
	else:
		D[e] = 1
sorted_keys = sorted(D.keys())
res = D[sorted_keys[0]]
for i in range(1, len(sorted_keys)):
	current = sorted_keys[i]
	prev = sorted_keys[i - 1]
	if D[current] > res:
		res = D[current]
	if current - prev == 1 and D[current] + D[prev] > res:
		res = D[current] + D[prev]
print(res)


import sys
n = int(input().strip())
a = [int(a_temp) for a_temp in input().strip().split(' ')]
items = []
for (i, n) in enumerate(a):
	items.append(len([n2 for n2 in a if 0 <= n2 - n < 2]))
	items.append(len([n2 for n2 in a if 0 <= n - n2 < 2]))
items.sort(reverse=True)
print(items[0])


import sys
n = int(input().strip())
a = [int(a_temp) for a_temp in input().strip().split(' ')]
temp = [0 for i in range(min(a), max(a) + 1)]
for i in range(len(a)):
	temp[a[i] - min(a)] += 1
cpt = 0
for i in range(len(temp)):
	if i == len(temp) - 1:
		compa = temp[i]
	else:
		compa = temp[i] + temp[i + 1]
	if compa > cpt:
		cpt = compa
print(cpt)


import sys
n = int(input().strip())
a = sorted([int(a_temp) for a_temp in input().strip().split(' ')])
utilizados = []
maximo = 0
for i in range(n):
	sumaarriba = 0
	sumaabajo = 0
	if a[i] not in utilizados:
		utilizados.append(a[i])
		sumaarriba += a.count(a[i] + 1) + a.count(a[i])
		sumaabajo += a.count(a[i]) + a.count(a[i] - 1)
		if sumaarriba > maximo:
			maximo = sumaarriba
		if sumaabajo > maximo:
			maximo = sumaabajo
print(maximo)


def main():
	n = int(input())
	a = input().split(' ')
	a = list(map(int, a))
	for i in range(n):
		for j in range(i, n):
			if a[i] > a[j]:
				(a[i], a[j]) = (a[j], a[i])
	index = 1
	highest = 0
	current_value = a[0]
	for i in range(1, n):
		if a[i] - current_value <= 1:
			index += 1
		else:
			if index > highest:
				highest = index
			index = 1
			current_value = a[i]
	if highest < index:
		highest = index
	print(highest)
main()


import sys
n = int(input().strip())
a = [int(a_temp) for a_temp in input().strip().split(' ')]
s = sorted(set(a))
li = []
for i in s:
	t = a.count(i)
	y = a.count(i + 1)
	if y == 0:
		li.append(t)
	else:
		li.append(t + y)
print(max(li))


n = int(input().strip())
a = [int(a_temp) for a_temp in input().strip().split(' ')]
A = sorted(list(set(a)))
m = len(A)
B = [0] * m
B[0] = a.count(A[0])
score = B[0]
for i in range(1, m):
	B[i] = a.count(A[i])
	if score < B[i]:
		score = B[i]
	if A[i] - A[i - 1] == 1 and score < B[i] + B[i - 1]:
		score = B[i] + B[i - 1]
print(score)


import sys
n = int(input().strip())
a = [int(a_temp) for a_temp in input().strip().split(' ')]
maxval = max(a)
arr = []
for i in range(0, maxval):
	arr.append(0)
for i in a:
	arr[i - 1] += 1
answer = 0
for i in range(1, len(arr)):
	if arr[i] + arr[i - 1] > answer:
		answer = arr[i] + arr[i - 1]
print(answer)


import sys
n = int(input().strip())
a = [int(a_temp) for a_temp in input().strip().split(' ')]
totalArr = []
for i in range(n):
	num = a[i]
	arr = [num]
	for j in range(n):
		if i != j and a[j] in [num - 1, num]:
			arr.append(a[j])
	totalArr.append(arr)
	arr = [num]
	for j in range(n):
		if i != j and a[j] in [num + 1, num]:
			arr.append(a[j])
	totalArr.append(arr)
maxArr = max(totalArr, key=len)
print(len(maxArr))


import sys
n = int(input().strip())
a = [int(a_temp) for a_temp in input().strip().split(' ')]
lst = [0] * 100
for i in a:
	lst[i] += 1
best = -float('inf')
for x in range(len(lst) - 1):
	temp = lst[x] + lst[x + 1]
	if temp > best:
		best = temp
print(best)


import sys
n = int(input().strip())
a = [int(a_temp) for a_temp in input().strip().split(' ')]
a.sort()
b = []
q = 1
x = a[0]
for i in range(len(a) - 1):
	if abs(x - a[i + 1]) <= 1:
		q += 1
		continue
	else:
		b.append(q)
		q = 1
		x = a[i + 1]
b.append(q)
print(max(b))


import sys
n = int(input().strip())
a = [int(a_temp) for a_temp in input().strip().split(' ')]
d = [0] * 100
for e in a:
	d[e] += 1
maximum = 0
for i in range(1, len(d)):
	maximum = max(maximum, d[i] + d[i - 1])
print(maximum)


import sys
n = int(input().strip())
b = [int(a_temp) for a_temp in input().strip().split(' ')]
a = {}
for x in b:
	if x in a:
		a[x] += 1
	else:
		a[x] = 1
for x in range(max(a)):
	if not x in a:
		a[x] = 0
maxsum = 0
for x in range(len(a) - 1):
	if a[x] + a[x + 1] > maxsum:
		maxsum = a[x] + a[x + 1]
print(maxsum)