Problem:
 Score : 300 points    Problem Statement You are given an integer sequence of length N , a_1,a_2,...,a_N .  For each 1≤i≤N , you have three choices: add 1 to a_i , subtract 1 from a_i or do nothing.  After these operations, you select an integer X and count the number of i such that a_i=X .  Maximize this count by making optimal choices.      Constraints  1≤N≤10^5  0≤a_i<10^5 (1≤i≤N)  a_i is an integer.         Input The input is given from Standard Input in the following format:  N  a_1  a_2 .. a_N       Output Print the maximum possible number of i such that a_i=X .        Sample Input 1 7
3 1 4 1 5 9 2      Sample Output 1 4  For example, turn the sequence into 2,2,3,2,6,9,2 and select X=2 to obtain 4 , the maximum possible count.       Sample Input 2 10
0 1 2 3 4 5 6 7 8 9      Sample Output 2 3       Sample Input 3 1
99999      Sample Output 3 1

Solution (Python):


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

cnt = {}

for i in a:
    for j in [-1,0,1]:
        cnt.setdefault(i+j, 0)
        cnt[i+j] += 1

print(max(cnt.values()))

Solution (Python):
N = int(input())
a = [int(x) for x in input().split()]

li = [0 for _ in range(10**5+1)]
ans = 0
for i in a:
    li[i]+=1
for ind,x in enumerate(li[:-2]):
    ans = max(ans,x+li[ind+1]+li[ind+2])
print(ans)

Solution (Python):
import collections
N=int(input())
a=list(map(int,input().split()))
for i in range(len(a)):
    a.append(a[i]+1)
    a.append(a[i]-1)
A=collections.Counter(a)
print(max(A.values()))

Solution (Python):
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time

sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7

def LI(): return list(map(int,input().split()))
def I(): return int(input())
def LS(): return input().split()
def S(): return input()

def main():
  n=I()
  a=LI()
  b=[]
  for x in a:
    b.append(x-1)
    b.append(x)
    b.append(x+1)

  b.sort()
  c=1
  mx=1
  d=b[0]
  for x in b[1:]:
    if d==x:
      c+=1
    else:
      mx=max(mx,c)
      c=1
      d=x

  return mx

print(main())

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
count = {}
for i in a:
  for j in [-1, 0, 1]:
    count.setdefault(i+j, 0)
    count[i+j] += 1
  
print(max(count.values()))

Solution (Python):
n = int(input())
lis = sorted(list(map(int,input().split())))
now = -1
ans = 0
for x in range(n):
    if lis[x] == now:
        continue
    now = lis[x]
    val = 0
    for y in range(n-x):
        if lis[x]+2 >= lis[x+y]:
            val += 1
            ans = max(ans,val)
        else:
            break

print(ans)

Solution (Python):
N = int(input())
A = [int(s) for s in input().split()]

def main():
    a = list(map(lambda n: n - 1, A)) + A + list(map(lambda n: n + 1, A))
    d = {}
    for n in a:
        if not n in d.keys():
            d[n] = 0
        d[n] += 1
    print(max(d.values()))

if __name__ == '__main__':
    main()

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
b = [0]*(2+max(a))

for i in range(n):
    b[a[i]] += 1

ans = 0

for i in range(len(b)):
    if b[i] != 0:
        ans = max(ans, b[i-1]+b[i]+b[i+1])
print(ans)

Solution (Python):
N, *a = map(int, open(0).read().split())
cnt = [0] * (10 ** 5 + 2)

for v in a:
    cnt[v] += 1
    cnt[v + 1] += 1
    cnt[v + 2] += 1
    
print(max(cnt))

Solution (Python):
from collections import Counter
N = int(input())
alist = Counter(tuple(map(int,input().split())))
max_counter = 0
com = alist.most_common()
for num,cou in com:
    counter = 0
    counter += cou
    counter += alist[num-1]
    counter += alist[num+1]
    if counter > max_counter:
        max_counter = counter

print(max_counter)

Solution (Python):
import bisect

N = int(input())
A = list(map(int,input().split()))

min_A = min(A); max_A = max(A)
A.extend([-100, int(1e6)])
A.sort()

ans = 0
for i in range(min_A, max_A+1):
  lower = bisect.bisect_left(A, i-1) - 1
  upper = bisect.bisect_right(A, i+1) - 1
  ans = max(ans, upper - lower)
  
print(ans)

Solution (Python):
from collections import Counter
N=int(input())
a=Counter(map(int,input().split()))
_max_=0
for i in range(1,10**5-1):
    temp=a[i-1]+a[i]+a[i+1]
    if temp > _max_:
        _max_=temp
print(_max_)

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
m=max(a)
d={i:0 for i in range(m+1)}
for a_ in a:
    d[a_]+=1
if m==0:
    print(d[0])
elif m==1:
    print(d[0]+d[1])
elif m==2:
    print(d[0]+d[1]+d[2])
else:
    cnt=d[0]+d[1]+d[2]
    ma=cnt
    for i in range(3,m+1):
        cnt-=d[i-3]
        cnt+=d[i]
        ma=max(ma,cnt)
    print(ma)

Solution (Python):
def dic_counter(dic, num):
    if num in dic:
        dic[num] += 1
    else:
        dic[num] = 1

_ = int(input())
dic = {}
for i in input().split():
    dic_counter(dic, int(i)-1)
    dic_counter(dic, int(i))
    dic_counter(dic, int(i)+1)
print(sorted(dic.values(), key=lambda x: -x)[0])

Solution (Python):
from collections import Counter
_ = int(input())
A = [int(x) for x in input().split()]
A_ = Counter(A)
A_minusone = Counter([x - 1 for x in A])
A_plusone = Counter([x + 1 for x in A])
print((A_ + A_minusone + A_plusone).most_common()[0][1])

Solution (Python):
n = int(input())
A = list(map(int, input().split()))
count = [0] * (max(A) + 3)
for a in A:
    count[a] += 1
    count[a + 1] += 1
    count[a - 1] += 1
print(max(count))

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
cnt=[0]*10**6
for i in a:
    cnt[i-1]+=1
    cnt[i]+=1
    cnt[i+1]+=1
print(max(cnt))

Solution (Python):

n = int(input())
lst = list(map(int, input().rstrip().split()))
ans = [0] * 100001
for i in lst:
    ans[i] += 1
print(max(map(sum, zip(ans, ans[1:], ans[2:]))))

Solution (Python):
N = int(input())
A = list(map(int, input().split()))

L = [0 for _ in range(10 ** 5 + 2)]

for a in A:
    L[a] += 1

ans = 0
for i in range(1,100001):
    ans = max(ans,L[i-1]+L[i]+L[i+1])

print(ans)

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
count = [0] * 10 ** 5
for i in a:
    count[i] += 1
ans = 0
for i in range(10 ** 5):
    ans = max(ans, sum(count[i:i+3]))
print(ans)

Solution (Python):
N = int(input())
lst_A = list(map(int, input().split()))
lst_cnt = [0] * (10**6)
ans = 0


for a in lst_A:
    lst_cnt[a - 1] += 1
    lst_cnt[a] += 1
    lst_cnt[a + 1] += 1
ans = max(lst_cnt)


print(ans)

Solution (Python):
def dic_counter(dic, num):
    if num in dic:
        dic[num] += 1
    else:
        dic[num] = 1

N = int(input())
dic = {}
for i in input().split():
    dic_counter(dic, int(i)-1)
    dic_counter(dic, int(i))
    dic_counter(dic, int(i)+1)
for _, value in sorted(dic.items(), key=lambda x: -x[1]):
    print(value)
    break

Solution (Python):
from collections import Counter


N = int(input())
lst_A = list(map(int, input().split()))

cnt_A = Counter(lst_A)
ans = 0



for key in lst_A:
    cnt_X = cnt_A[key - 1] + cnt_A[key] + cnt_A[key + 1]
    ans = max(ans, cnt_X)


print(ans)

Solution (Python):
from collections import Counter


N = int(input())
lst_A = list(map(int, input().split()))
set_A = set(lst_A)
cnt_A = Counter(lst_A)
ans = 0


for key in set_A:
    cnt_X = cnt_A[key - 1] + cnt_A[key] + cnt_A[key + 1]
    ans = max(ans, cnt_X)


print(ans)

Solution (Python):

'''
N = int(input())
A = list(map(int,input().split()))
ans = []
for X in range(min(A),max(A)+1):
    count = 0
    for i in A:
        if(abs(i-X) <= 1):
            count += 1
    ans.append(count)
print(max(ans))
'''


import collections
N = int(input())
A = list(map(int,input().split()))
list_A = []
for i in  range(N):
    list_A.append(A[i])
    list_A.append(A[i]+1)
    list_A.append(A[i]-1)
print(collections.Counter(list_A).most_common()[0][1])

Solution (Python):
N = int(input())
A = map(int, input().split())

a_max = 10**5
cnts = [0]*(a_max+1)
for a in A:
    if a >= 1:
        cnts[a - 1] += 1
    cnts[a] += 1
    if a <= a_max - 1:
        cnts[a + 1] += 1

print(max(cnts))

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
f = [0 for i in range(10**5 + 10)]
for i in a:
    f[i] += 1
    f[i + 1] += 1
    f[i - 1] += 1

print(max(f))

Solution (Python):
import collections

n = int(input())
A = [int(i) for i in input().split()]
B = [i+1 for i in A]
C = [i-1 for i in A]

print(collections.Counter(A+B+C).most_common(1)[0][1])

Solution (Python):


import math


N = int(input())
An = list(map(int, input().split()))
MX = max(max(An),1)
Bn = [0 for _ in range(MX+2)]
for i in range(N):
  Bn[An[i]] += 1

ans = 0
for i in range(1,MX+1):
  ans = max(ans, Bn[i-1]+Bn[i]+Bn[i+1])
print(ans)

Solution (Python):

N = int(input())
ai = list(map(int, input().split()))
num = [0]*(10**5+1)
for a in ai:
   num[a+1] +=1
   num[a] +=1
   if a>0:
    num[a-1] +=1  
print(max(num))

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
num=[0]*(10**5+2)
for i in a:
    num[i]+=1
    num[i+1]+=1
    if i!=0:
        num[i-1]+=1
print(max(num))

Solution (Python):
N = int(input())
a = list(map(int, input().split()))

a.sort()

l = 0
r = 1
ans = 0

if N == 1:
    print(1)
    exit()

while r < N:
    if a[r] - a[l] <= 2 or r == l:
        ans = max(ans, r - l + 1)
        r += 1
    else:
        l += 1

print(ans)

Solution (Python):
from collections import Counter
N=int(input())
num=[]
for a in map(int, input().split()):
	num.extend((a-1, a, a+1))
print(max(Counter(num).values()))

Solution (Python):
n=int(input())
arr=list(map(int,input().split()))
cnt=[0]*(10**5+2)
for i in range(n):
  cnt[arr[i]-1]+=1
  cnt[arr[i]]+=1
  cnt[arr[i]+1]+=1
print(max(cnt))

Solution (Python):


def solve():
    """
    あるXに着目した時、X-1とX+1はXにできる。
    よって、各Xについて数を数える
    """
    N = int(input())
    A = list(map(int, input().split()))

    dic = {}
    for a in A:
        if not a in dic:
            dic[a] = 1
        else:
            dic[a] += 1
        if not a-1 in dic:
            dic[a-1] = 1
        else:
            dic[a-1] += 1
        if not a+1 in dic:
            dic[a+1] = 1
        else:
            dic[a+1] += 1
    
    dic_values = list(dic.values())
    dic_values.sort()
    print(dic_values[-1])


if __name__ == "__main__":
    solve()

Solution (Python):
N = int(input())
A = list(map(int,input().split()))

A.sort()
Aset = set(A)
ans = 1

    
def bisearch(a):
    idx = (N-1)//2
    s = 0
    e = N-1
    while s<e:
        if A[idx] == a: break
        elif A[idx] < a:
            s = idx+1
            idx = (s + e)//2
        else:
            e = idx-1
            idx = (s + e)//2 
    return idx


for a in list(Aset):
    cnt = 0
    idx = bisearch(a)
    i = 0
    while True:
        if idx + i < 0 or idx + i >= N:
            break            
        elif A[idx + i] <= a+1 and A[idx + i] >= a-1:
            cnt += 1
        else:
            break
        i += 1
    i = -1
    while True:
        if idx + i < 0 or idx + i >= N:
            break          
        elif A[idx + i] <= a+1 and A[idx + i] >= a-1:
            cnt += 1
        else:
            break
        i -= 1
        
    ans = max(ans,cnt)
    
print(ans)

Solution (Python):
N = int(input())
A = list(map(int, input().split()))

m = {}
for a in A:
	if a not in m:
		m[a] = 1
	else:
		m[a] += 1

r = 0
for n in m:
	t = m[n]
	if n + 1 in m:
		t += m[n + 1]
	if n + 2 in m:
		t += m[n + 2]
	r = max(r, t)

print(r)

Solution (Python):
N = int(input())
A = [int(i) for i in input().split()]

memo = [0] * (max(A) + 10)
for a in A:
    for i in (-1, 0, 1):
        memo[a + i + 1] += 1

print(max(memo))

Solution (Python):
import sys
import itertools
input = sys.stdin.readline

N = int(input())
L = [int(v) for v in input().split()]
m = max(L)
NL = [0] * (m + 1)

for i in range(N):
    v = L[i]
    NL[v] += 1

nz = [i for i in range(len(NL)) if NL[i] != 0]
if len(nz) == 1:
    print(NL[nz[0]])
elif len(nz) == 2:
    if abs(nz[0] - nz[1]) <= 2:
        print(NL[nz[0]] + NL[nz[1]])
    else:
        print(max(NL[nz[0]], NL[nz[1]]))
else:
    t = NL[0] + NL[1] + NL[2]
    ans = max(1, t)
    for i in range(1, len(NL) - 2):
        t = t - NL[i - 1] + NL[i + 2]
        ans = max(ans, t)

    print(ans)

Solution (Python):
N = int(input())
a = list(map(int,input().split()))

count = [0]*(10**5+2)
for ai in a:
    count[ai] += 1
    count[ai+1] += 1
    count[ai+2] += 1
print(max(count))

Solution (Python):
N = int(input())
a = list(map(int, input().split()))

count = [0] * (10**5 + 2)

for i in a:
    if i == 0:
        count[i] += 1
        count[i+1]+= 1
    else:
        count[i-1] += 1
        count[i] += 1
        count[i+1] += 1

print(max(count))

Solution (Python):
N = int(input())
A = list(map(int,input().split()))

MI, MA = min(A),max(A)
num = MA - MI + 1
L = [0] * num

if num < 3:
    print(N)
    exit()

for ctr in A:
    L[ctr-MI] += 1

ans,tmp = sum(L[0:3]),sum(L[0:3])
for j in range(num-3):
    tmp = tmp - L[j] + L[j+3]
    ans = max(ans,tmp)

print(ans)

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
B = [0] * (10**5 + 1)

for i in A:
    if i == 0:
        B[i] += 1
        B[i+1] += 1
    else:
        B[i-1] += 1
        B[i] += 1
        B[i+1] += 1

print(max(B))

Solution (Python):
N = int(input())
A = sorted(map(int,input().split(" ")))

o = []
l = 0
for r in range(N):
    a = A[r]
    while A[l] < a - 2:
        l += 1
    o.append(r-l+1)

print(max(o))

Solution (Python):
N = int(input())
a = list(map(int, input().split()))
a_min = min(a)
a_max = max(a)
if a_max - a_min <= 2:
  print(N)
else:
  b = [0] * (a_max-a_min+1)
  for i in range(N):
    b[a[i]-a_min] += 1
  
  s = b[0] + b[1] + b[2]
  s_max = s
  for i in range(len(b)-3):
    s += b[3+i]-b[i]
    if s > s_max:
      s_max = s
  print(s_max)

Solution (Python):

def read_input():
    n = int(input())
    alist = list(map(int, input().split()))

    return n, alist

def submit():
    n, alist = read_input()

    near_occurs = {}
    max_x = max(alist)
    for i in range(max_x + 1):
        near_occurs[i] = []

    for a in alist:
        near1 = a - 1
        near2 = a + 1

        if near1 >= 0:
            near_occurs[near1].append(a)

        near_occurs[a].append(a)

        if near2 <= max_x:
            near_occurs[near2].append(a)


    near_occurs = list(near_occurs.items())
    near_occurs.sort(key=lambda x: len(x[1]), reverse=True)

    print(len(near_occurs[0][1]))


if __name__ == '__main__':
    submit()

Solution (Python):
from functools import reduce
import collections
import math

def main():
    
    
    
    

    
    
    
    
    
    

    
    

    
    
    
    
    
    
    
    
    
    INF = float("inf")

    
    MOD = 10**9+7
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    

    
    
    
    
    N = int(input())
    a = list(int(_) for _ in input().split())
    a.sort()
    d = collections.defaultdict(int)
    for i in range(N):
        d[a[i]] +=1
    cnt = 0
    ans = 0

    for key in d:
        cnt = d[key]
        if key-1 in d:
            cnt += d[key-1]
        if key+1 in d:
            cnt += d[key+1] 
        ans = max(cnt,ans)
    print(ans)
if __name__ == '__main__':
    main()

Solution (Python):
from collections import Counter
N=int(input())
A=list(map(int,input().split()))
T=[]
for a in A:
    T.append(a+1)
    T.append(a)
    T.append(a-1)
c=list(Counter(T).most_common())
c.sort(key=lambda x:x[1],reverse=True)
print(c[0][1])

Solution (Python):

N=int(input())
A=[int(j) for j in input().split()]
cnt=[0]*(10**5+2)
for a in A:
    cnt[a]+=1
    cnt[a+1]+=1
    cnt[a+2]+=1
print(max(cnt))

Solution (Python):

N=int(input())
A=[int(j) for j in input().split()]
cnt=[0]*(10**5+10)
for a in A:
    cnt[a]+=1
    cnt[a+1]+=1
    cnt[a+2]+=1
print(max(cnt))

Solution (Python):

N=int(input())
A=[int(j) for j in input().split()]
cnt=[0]*(10**6)
for a in A:
    cnt[a]+=1
    cnt[a+1]+=1
    cnt[a+2]+=1
print(max(cnt))

Solution (Python):
N = int(input())
A = list(map(int, input().split()))

DA = {}
for a in A:
    if a in DA.keys():
        DA[a] += 1
    else:
        DA[a] = 1

ans = 0
c = 0
for k, v in DA.items():
    c += v
    if k+1 in DA:
        c += DA[k+1]
    if k-1 in DA:
        c += DA[k-1]
    ans = max(ans, c)
    c = 0
print(ans)

Solution (Python):
from collections import Counter
n = int(input())
A = list(map(int,input().split()))
B = [A[i]-1 for i in range(n)] +  A + [A[i]+1 for i in range(n)]
ans = Counter(B).most_common()[0][0]
print(B.count(ans))

Solution (Python):
N = int(input())
a = list(map(int, input().split()))

a.sort()
ans = 0
num = [0] * int(3e5)
for i in a:
    num[i] += 1

for i in range(int(2e5)):
    check = sum(num[i:i+3])
    ans = max(ans, check)

print(ans)

Solution (Python):
n = int(input())
A = list(map(int, input().split()))
lst =[0] * (10 ** 5 +10)
for i in A:
    if i > 0:
        lst[i - 1] += 1
    lst[i] += 1
    lst[i + 1] += 1
print(max(lst))

Solution (Python):
N = int(input())
a = [int(i) for i in input().split()]

c = [0] * (10**5 + 1)

for i in a:
    c[i] += 1
    if i != 0 and i != 10 ** 5:
        c[i-1] += 1
        c[i+1] += 1
    elif i == 0:
        c[i+1] += 1
    else:
        c[i-1] += 1

print(max(c))

Solution (Python):
N = int(input())
a = [int(i) for i in input().split(" ")]

lst = [0 for _ in range(max(a)+3)]
for x in a:
    lst[x] += 1
    lst[x+1] += 1
    lst[x+2] += 1
print(max(lst))

Solution (Python):
import collections
n=int(input())
A=list(map(int,input().split()))
B=collections.Counter(A)
ans=0
for i in range(max(A)+1):
  ans=max(ans,B[i-2]+B[i-1]+B[i])
print(ans)

Solution (Python):
N = int(input())
a = [int(i) for i in input().split()]
Min = min(a)-1
Max = max(a)+1
ans = [0 for i in range(Min, Max+1)]

for i in a:
    ans[i-Min-1] += 1
    ans[i-Min] += 1
    ans[i-Min+1] += 1
        
print(max(ans))

Solution (Python):
n=int(input())
d=dict()
for i in map(int,input().split()):
  if i not in d:
    d[i]=1
  else:
    d[i]+=1
  if i-1 not in d:
    d[i-1]=1
  else:
    d[i-1]+=1
  if i+1 not in d:
    d[i+1]=1
  else:
    d[i+1]+=1
print(max(d.values()))

Solution (Python):
import sys
fastinput=sys.stdin.readline
N=int(input())
bucket=[0]*(10**5+2)
A=[int(_) for _ in fastinput().split()]
for a in A:
    bucket[a-1]+=1
    bucket[a]+=1
    bucket[a+1]+=1
print(max(bucket))

Solution (Python):
import collections

N = int(input())
A = list(map(int, input().split()))
A.sort()
A = A+[i + 1 for i in A]+[i - 1 for i in A]
c = collections.Counter(A)
ans = max(c.values())
print(ans)

Solution (Python):
import itertools

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

aggregated_a = {x: sum(1 for _ in iter) for (x, iter) in itertools.groupby(sorted(a))}
counts = [aggregated_a[a_i] if a_i in aggregated_a else 0 for a_i in range(100000)]
print(max(sum(counts[a_i - 1 : a_i + 2]) for a_i in range(100000)))

Solution (Python):
n = int(input())

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

d = {}

for i in a:
  for j in range(i - 1, i + 2): 
    d[j] = d.get(j, 0) + 1
    
    
print(max(d.values()))

Solution (Python):

import copy
import numpy as np
import random
import bisect 
import math

N = int(input())
a = [int(i) for i in input().split()]

a.sort()

counts = {}
for i in range(N):
    if a[i] in counts.keys():
        counts[a[i]] += 1
    else:
        counts[a[i]] = 1

counts_list = []
tmp = 0
for num in range(max(a)+1):
    try:
        tmp += counts[num]
        
    except:
        pass

    counts_list.append(tmp)

ans = 0
for i in range(len(counts_list)-3):
    ans = max(ans,counts_list[i+3]-counts_list[i])
    

if len(counts_list) <= 3:
    ans = max(ans,max(counts_list))

print(ans)

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
c=[0]*100001
for i in a:c[i]+=1
ans=0
for i in range(99999):ans=max(ans,c[i]+c[i+1]+c[i+2])
print(ans)

Solution (Python):
import collections

n = int(input())
a = [int(num) for num in input().split()]

list_num = collections.Counter(a)

count_max = 0
for x in range(min(a), max(a)+1):
    count = list_num[x-1] + list_num[x] + list_num[x+1]
    count_max = max(count_max, count)

print(count_max)

Solution (Python):
N = int(input())
A = list(map(int,input().split()))
bit = [0]*10**6
for a in A:
    bit[a] += 1
    bit[a+1] += 1
    bit[a+2] += 1
ans = max(bit)
print(ans)

Solution (Python):
from collections import defaultdict

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

c = defaultdict(int)
for ai in a:
    c[ai] += 1

m = 0
for n in c.keys():
    sum_c = c[n]
    if n - 1 in c: sum_c += c[n - 1]
    if n + 1 in c: sum_c += c[n + 1]
    m = max(m, sum_c)
print(m)

Solution (Python):
from itertools import accumulate
n = int(input())
a = [int(item) for item in input().split()]

cum = [0]* (10**5 + 2)
for item in a:
    if item - 1 < 0:
        cum[0] += 1
    else:
        cum[item-1] += 1
    cum[item+2] -= 1
print(max(list(accumulate(cum))))

Solution (Python):
import sys
import collections
ns = lambda: sys.stdin.readline().rstrip()
ni = lambda: int(ns())
nm = lambda: map(int, sys.stdin.readline().split())
nl = lambda: list(nm())


n = ni()

a = nl()

minn = min(a)
maxx = max(a)

lis = [0 for i in range(maxx - minn + 3)]
for aa in a:
    i = aa - minn
    lis[i - 1] += 1
    lis[i] += 1
    lis[i + 1] += 1
print(max(lis))

Solution (Python):
n = int(input())

data = list(map(int,input().split()))

d = dict()

for x in data:
    if d.__contains__(x):
        d[x]+=1
    else:
        d[x]=1
         
    if d.__contains__(x-1):
        d[x-1]+=1
    else:
        d[x-1]=1
    if d.__contains__(x+1):
        d[x+1]+=1
    else:
        d[x+1]=1
res = 0
for v in d.values():
    res = max(res,v)
print(res)

Solution (Python):
N = int(input())
A = [0]*100000
AA = list(map(int, input().split()))
minA = 100000
maxA = 0
for a in AA:
  A[a] += 1
  minA = min(minA, a)
  maxA = max(maxA, a)
ans = 1
for i in range(max(minA-1,0), min(maxA+1, N-2)):
  ans = max(ans, A[i]+A[i+1]+A[i+2])
print(ans)

Solution (Python):
def main():
    N = input()
    
    a_s =list(map(int, input().split()))

    ai_to_a_num = {}
    for ai in a_s:
        if ai in ai_to_a_num.keys():
            ai_to_a_num[ai] += 1
        else:
            ai_to_a_num[ai] = 1
    
    max_num = 0
    for ai in ai_to_a_num.keys():
        num = 0
        num += ai_to_a_num[ai]
        if ai-1 in ai_to_a_num.keys():
            num += ai_to_a_num[ai-1]
        if ai+1 in ai_to_a_num.keys():
            num += ai_to_a_num[ai+1]
        max_num = max(max_num, num)
    
    print(max_num)

main()

Solution (Python):
from collections import Counter

N=int(input())
A=list(map(int,input().split()))
low=min(A)
high=max(A)

c_l=Counter(A)
ans=-1
for x in range(low,high+1):
    
    a=x-1
    b=x
    c=x+1
    num=c_l[a]+c_l[b]+c_l[c]
    ans=max(num,ans)
print(ans)

Solution (Python):
n,a=int(input()),list(map(int,input().split()));c=[0]*(max(a)+1)
for i in a:
  if i>0:c[i-1]+=1
  if i<len(c)-1:c[i+1]+=1
  c[i]+=1
print(max(c))

Solution (Python):
from collections import Counter
from sys import stdin
n = int(stdin.readline().rstrip())
al = [int(_) for _ in stdin.readline().rstrip().split()]
counter = Counter(al)
for a in al:
    counter[a-1]+=1
    counter[a+1]+=1
print(max(counter.values()))

Solution (Python):


import itertools
import collections
import bisect

def main():
    N = int(input())
    A = list(map(int, input().split()))

    c = collections.Counter()
    for a in A:
        c[a-1] += 1
        c[a] += 1
        c[a+1] += 1
    print(c.most_common()[0][1])


if __name__ == '__main__':
    main()

Solution (Python):
from collections import defaultdict
n = int(input())
a = list(map(int, input().split()))
d = defaultdict(int)
for i in a:
  d[i] += 1

max = 0
for i in set(a):
  if max < d[i]+d[i-1]+d[i+1]:
    max = d[i]+d[i-1]+d[i+1]
print(max)

Solution (Python):
n=int(input())
a=sorted(list(map(int,input().split())))
num=[0]*(a[-1]+3)
for x in a:
    num[x]+=1
ans=0
for i in range(len(num)-2):
    if ans<num[i]+num[i+1]+num[i+2]:
        ans=num[i]+num[i+1]+num[i+2]
print(ans)

Solution (Python):
from collections import Counter

N = int(input())
A = list(map(int, input().split()))

counter = Counter(A)
for a in A:
    counter[a-1] += 1
    counter[a+1] += 1

print(max(counter.values()))

Solution (Python):
n=int(input())
l=[int(i) for i in input().split()]
from collections import Counter
lst=[]
for i in l:
  lst.append(i-1)
  lst.append(i)
  lst.append(i+1)
print(Counter(lst).most_common(1)[0][1])

Solution (Python):
from statistics import mean, median,variance,stdev
import numpy as np
import sys
import math
import fractions
import itertools
import copy
import collections
from operator import itemgetter

def j(q):
    if q==1: print("Yes")
    else:print("No")
    exit(0)


def ct(x,y):
    if (x>y):print("+")
    elif (x<y): print("-")
    else: print("?")

def ip():
    return int(input())
def printrow(a):
    for i in range(len(a)):
        print(a[i])
n = ip()                                     

a = [int(i) for i in input().split()]        







a.sort()
pos = [0]
p = a[0]
for i in range(n):
    if a[i] != p:
        p = a[i]
        pos.append(i)
s = [1]
if n == 2:
    if a[1]-a[0]==1:s.append(2)
for i in range(len(pos)):
    p = a[pos[i]]
    position = pos[i]
    score = 0
    while p +  2 >= a[position]:
        position+=1
        score+=1
        if position == n: break
  
    s.append(score)
 
print(max(s))

Solution (Python):
N = int(input())
an = list(map(int, input().split()))

from collections import Counter
c =  Counter()
for a in an:
    c[a-1] += 1
    c[a] += 1
    c[a+1] += 1
print(c.most_common()[0][1])

Solution (Python):
from collections import defaultdict
import sys
input = sys.stdin.readline
N = int(input())
a = list(map(int, input().split()))
dic = defaultdict(int)
for i in range(N):
    dic[a[i] - 1] += 1
    dic[a[i]] += 1
    dic[a[i] + 1] += 1
print(max(dic.values()))

Solution (Python):


from collections import defaultdict

n=int(input())
a=[int(x) for x in input().split()]
d=defaultdict(int)
for i in a:
    d[i]+=1

b=[[-2,0],[-1,0]]
for i in d:
    b.append([i,d[i]])
b.sort()

ans=1
m=len(b)
for i in range(m):
    ans=max(ans,b[i][1])
    if i+1<m and b[i][0]+1==b[i+1][0]:
        ans=max(ans,b[i][1]+b[i+1][1])
    if i+2<m and b[i][0]+1==b[i+1][0] and b[i+1][0]+1==b[i+2][0]:
        ans=max(ans,b[i][1]+b[i+1][1]+b[i+2][1])
print(ans)

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
d = {}
for i in range(n):
    for j in range(-1, 2):
        if a[i] + j in d.keys():
            d[a[i] + j] += 1
        else:
            d[a[i] + j] = 1
print(max(d.values()))

Solution (Python):
n = int(input())
a = list(map(int,input().split()))
from collections import Counter
C = Counter(a)
ans = 0
for c in C:
    ans = max(ans,C[c-1]+C[c]+C[c+1])
print(ans)

Solution (Python):
import numpy as np
N = int(input())
alist = np.array(list(map(int, input().split())))
alist
alistp = alist + 1
alistm = alist - 1
d = {}
for a in alist:
    if a not in d.keys():
        d[a] = 1
    else:
        d[a] += 1
for a in alistp:
    if a not in d.keys():
        d[a] = 1
    else:
        d[a] += 1
for a in alistm:
    if a not in d.keys():
        d[a] = 1
    else:
        d[a] += 1
print(max(d.values()))

Solution (Python):
N = int(input())
A = list(map(int,input().split()))
A.sort()


counter = []
count = 0
for i in range(N):
  if(i == 0):
    counter.append([A[i],1])
  else:
    if(A[i] == A[i-1]):
      counter[count][1] += 1
    else:
      count += 1
      counter.append([A[i],1])

count += 1
ans = 0
for i in range(count):
  tmp = counter[i][1]
  if((i != 0)and(i != count-1)):
    if((counter[i][0]-counter[i-1][0])==1):
      tmp += counter[i-1][1]
    if((counter[i+1][0]-counter[i][0])==1):
      tmp += counter[i+1][1]
  elif((i == count-1)and(count >= 2)):
    if((counter[i][0]-counter[i-1][0])==1):
      tmp += counter[i-1][1] 
  ans = max(ans,tmp)

print(ans)

Solution (Python):
N=int(input())
A=list(map(int, input().split()))

memo=[0]*(10**5)
answer=0

for a in A:
  memo[a]+=1
  
for i in range(10**5-2):
  answer=max(answer, memo[i]+memo[i+1]+memo[i+2])
  
print(answer)

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
a.sort()
def binarysearch(x):
  ok=-1
  ng=n
  while abs(ok-ng)>1:
    mid=(ok+ng)//2
    if a[mid]>=x:
      ng=mid
    else:
      ok=mid
  return ok
ans=0
for i in range(-1,10**5+2):
  ans=max(ans,binarysearch(i+2)-binarysearch(i-1))
print(ans)

Solution (Python):
N = int(input())
alist = list(map(int,input().split()))
count = [0] * (10**5 + 2)
for a in alist:
    count[a] += 1
    count[a+1] += 1
    count[a+2] += 1
print(max(count))

Solution (Python):
import sys
from collections import defaultdict

def main():
    input = sys.stdin.readline
    N = int(input())
    A = list(map(int, input().split()))

    d = defaultdict(lambda: 0)
    for a in A:
        d[a-1] += 1
        d[a] += 1
        d[a+1] += 1

    ans = 0
    for key in d:
        ans = max(ans, d[key])

    return ans


if __name__ == '__main__':
    print(main())

Solution (Python):
N = int(input())
a = list(map(int, input().split()))
L = [0] * 10 ** 5
for i in range(N):
    L[a[i]] += 1
s = L[0] + L[1] + L[2]
A = s
for i in range(10 ** 5-3):
    s += L[i+3] - L[i]
    A = max(A,s)
print(A)

Solution (Python):
import bisect
n = int(input())
a = [int(i) for i in input().split()]
a.sort()
c = []
for i in a:
    c.append(bisect.bisect_right(a, i+1) - bisect.bisect_right(a, i-2))
print(max(c))

Solution (Python):
from collections import Counter

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

c=Counter(l+[x-1 for x in l]+[x+1 for x in l])
print(c.most_common()[0][1])

Solution (Python):
N = int(input())
a = list(map(int, input().split()))

count = [0] * 10**5

for e in a :
    if e != 0 :
        count[e - 1] += 1
    count[e] += 1
    if e != 10**5 - 1 :
        count[e + 1] += 1

print(max(count))

Solution (Python):
from collections import defaultdict
N = int(input())
a = [int(i) for i in input().split()]
d = defaultdict(int)
for x in a:
    d[x] += 1

ans = []
for x in d.keys():
    sum = d[x]
    if x-1 in d:
        sum += d[x-1]
    if x+1 in d:
        sum += d[x+1]
    ans.append(sum)
print(max(ans))

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
cnt = [0]*int(1e5)
ans = 0
for i in range(n) :
  cnt[a[i]] += 1
for i in range(1, len(cnt)-1) :
  ans = max(ans, cnt[i-1] + cnt[i] + cnt[i+1])
print(ans)

Solution (Python):
N=int(input())
from collections import defaultdict
d=defaultdict(int)
for a in map(int,input().split()):
    d[a-1]+=1
    d[a]+=1
    d[a+1]+=1
print(max(d.values()))

Solution (Python):
from collections import Counter

N = int(input())
an = Counter(map(int, input().split()))


def main():
    print(max([an[k - 1] + an[k] + an[k + 1] for k in an.keys()]))
    return


main()

Solution (Python):
from collections import Counter

N = int(input())
an = Counter(map(int, input().split()))


def main():
    print(max([an[k - 1] + v + an[k + 1] for k, v in an.items()]))
    return


main()

Solution (Python):
n=int(input())
l=[0]*100002
l2=list(map(int,input().split()))
for i in l2:
  for j in range(-1,2):
    l[i+j]+=1
print(max(l))

Solution (Python):
from collections import defaultdict
n = input()
nums = list(map(int, input().split()))
dic = defaultdict(int)
for num in nums:
    dic[num] += 1
keys = sorted(list(dic.keys()))
cnt = 1
for key in keys:
    cnt = max(dic[key] + dic[key + 1] + dic[key - 1], cnt)
print(cnt)

Solution (Python):
def together(N: int, A: list)->int:
    d = {}
    for a in A:
        d.setdefault(a, 0)
        d[a] += 1

        d.setdefault(a+1, 0)
        d[a+1] += 1

        d.setdefault(a-1, 0)
        d[a-1] += 1

    return max(d.values())


if __name__ == "__main__":
    N = int(input())
    A = [int(s) for s in input().split()]
    ans = together(N, A)
    print(ans)