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()))
for i in range(N):
	a.append(a[i] - 1)
	a.append(a[i] + 1)

import collections
a_c=collections.Counter(a)


s_val, s_count=zip(*a_c.most_common())

print(max(s_count))

Solution (Python):
N = int(input())
a = list(map(int, input().split()))
number = [0 for i in range(100001)]
for i in range(N):
    number[a[i]] += 1
ans = 0
for i in range(1, 100000):
    xans = number[i-1] + number[i] + number[i+1]
    ans = max(ans, xans)

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) :
        a = A[i]+j
        if a not in D :
            D[a] = 1
        else :
            D[a] += 1
L = sorted(list(D.values()))
print(L[-1])

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

d = {}
for x in a:
    if x in d:
        d[x] += 1
    else:
        d[x] = 1

d = sorted(d.items())

m = len(d)

if m == 1:
    print(d[0][1])
    exit()
elif m == 2:
    if d[1][0] - d[0][0] <= 2:
        print(d[0][1] + d[1][1])
    else:
        print(max(d[0][1], d[1][1]))
    exit()

ans = d[0][1]
if d[1][0] == d[0][0] + 1:
    ans += d[1][1]
r = d[m-1][1]
if d[m-2][0] == d[m-1][0] - 1:
    r += d[m-2][1]
ans = max(r, ans)

for i in range(1, m-1):
    r = d[i][1]
    if d[i-1][0] + 1 == d[i][0]:
        r += d[i-1][1]
    if d[i+1][0] - 1 == d[i][0]:
        r += d[i+1][1]
    ans = max(ans, r)


print(ans)

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
m = {}
for a in A:
    m.setdefault(a-1, 0)
    m.setdefault(a, 0)
    m.setdefault(a+1, 0)
    m[a-1] += 1
    m[a] += 1
    m[a+1] += 1

max_num = 0
for n in m.values():
    if n > max_num:
        max_num = n
print(max_num)

Solution (Python):
_ = input()
aa = list(map(int,input().split()))
cnt = [0] * 100002

for a in aa:
    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()))
l = [0]*(10**5)
for e in A:
    if e == 0:
        l[0] += 1
        l[1] += 1
    elif e == 99999:
        l[99999] += 1
        l[99998] += 1
    else:
        l[e-1] += 1
        l[e] += 1
        l[e+1] += 1
print(max(l))

Solution (Python):
n = int(input())
l = [0 for _ in range(100001)]
a = list(map(int, input().split()))
for i in a:
    l[i] += 1
maxc = 0
for i in range(1, len(l) - 1):
    maxc = max(maxc, l[i - 1] + l[i] + l[i + 1])
print(maxc)

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
mx = max(a)+1
mn = min(a)-1
b = [0]*(mx-mn+1)
for i in range(n):
    b[a[i]-1-mx] += 1
    b[a[i] -mx] += 1
    b[a[i]+1-mx] += 1
print(max(b))

Solution (Python):
def 解():
    iN = int(input())
    aC = [0] *(10** 5+2)
    dsA = set()
    for a in [int(_) for _ in input().split()]:
        aC[a] += 1
    iMax = 0
    for a in range(1,10**5 + 1):
        iMax = max(iMax,sum(aC[a-1:a+2]))
    print(iMax)
解()

Solution (Python):
def 解():
    iN = int(input())
    d={}
    for a in [int(_) for _ in input().split()]:
        if a in d:
            d[a] += 1
        else:
            d[a] = 1
    iMax = 0
    for a in d.keys():
        iMax = max(iMax,d.get(a,0) + d.get(a+1,0) + d.get(a-1,0))
    print(iMax)
解()

Solution (Python):
import numpy
n = int(input())
a = list(map(int,input().split()))
list_ = [0]*100004 
for i in a:
    list_[i] += 1
    list_[i+3] -= 1
res = numpy.cumsum(list_)
print(max(res))

Solution (Python):
N = int(input())
L = list(map(int, input().split()))
K = [ 0 for i in range(10**5)]
for i in range(N):
    K[L[i]] += 1
a = []   
res = 0
for i in range(len(K)):
    a.append(K[i])
    if len(a) > 3:
        a.pop(0)
    if sum(a) > res:
        res =sum(a)
print(res)

Solution (Python):
from collections import Counter

n = int(input())
A = sorted(list(map(int, input().split())))
A_ = []
for a in A:
    A_ += [a-1, a, a+1]
ans = Counter(A_).most_common()[0][1]
print(ans)

Solution (Python):
import collections

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

el_count = collections.Counter(A)

res = 0
for X in range(100000):
    tmp = el_count[X-1] + el_count[X] + el_count[X+1]
    if res < tmp:
        res = tmp

print(res)

Solution (Python):

from collections import Counter
def inpl(): return list(map(int, input().split()))

N = int(input())
C = Counter(inpl())

ans = 0
for k in C.keys():
    for j in range(-1, 2):
        ans = max(C[k-1+j] + C[k+j] + C[k+j+1], ans)
print(ans)

Solution (Python):

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

cnt_dict = {}

for a in an:
    if a in cnt_dict:
        cnt_dict[a] += 1
    else:
        cnt_dict[a] = 1

ans = 0
for k in range(10**5):
    a,b,c = 0,0,0
    if k-1 in cnt_dict:
        a = cnt_dict[k-1]
    if k in cnt_dict:
        b = cnt_dict[k]
    if k+1 in cnt_dict:
        c = cnt_dict[k+1]

    if ans < a+b+c:
        ans = a+b+c

print(ans)

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

Solution (Python):
import collections

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

l = []
for X in A:
    l += [X-1, X, X+1]

c = collections.Counter(l)
print(c.most_common()[0][1])

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

Solution (Python):
cnt=[0]*100002
input()
for x in map(int,input().split()):
    cnt[x]+=1
    cnt[x+1]+=1
    cnt[x+2]+=1
print(max(cnt))

Solution (Python):
from collections import Counter

N = int(input())
a = list(map(int, input().split()))
li = []
for ai in a:
    li.extend([ai-1, ai, ai+1])
c = Counter(li)
print(c.most_common()[0][1])

Solution (Python):
import collections

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



maxcnt = 0
for i in range(n):
    cnt = ac[a[i]] + ac[a[i]+1] + ac[a[i]+2]
    if cnt > maxcnt:
        maxcnt = cnt

print(maxcnt)

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

Solution (Python):
from collections import Counter
n = int(input())
a = list(map(int, input().split()))

c = Counter(a)

ans = c[0]
x = 0
for i in range(max(a)):
    if ans < c[i] + c[i+1] + c[i+2]:
        ans = c[i] + c[i+1] + c[i+2]
        x = c[i+1]

print(ans)

Solution (Python):
import sys
stdin = sys.stdin

def li(): return map(int, stdin.readline().split())
def li_(): return map(lambda x: int(x)-1, stdin.readline().split())
def lf(): return map(float, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())

from collections import Counter

n = ni()
a = list(li())

ap = [ai+1 for ai in a]
am = [ai-1 for ai in a]

cnt = Counter(am+a+ap)

print(cnt.most_common(1)[0][1])

Solution (Python):
from collections import Counter
N = int(input())
src = list(map(int,input().split()))
ctr = Counter(src)

ans = 0
for n in range(1,10**5+1):
    tmp = ctr[n-1] + ctr[n] + ctr[n+1]
    ans = max(ans, tmp)
print(ans)

Solution (Python):
from collections import Counter

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

c = Counter(a)
ans = 0
for i in range(min(a) - 1, max(a) + 1):
    res = c[i - 1] + c[i] + c[i + 1]
    ans = max(ans, res)
print(ans)

Solution (Python):
import collections

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

dic = collections.Counter(A)
tmp = [0 for _ in range(A[-1]+1)]


for X in dic.keys():
    tmp[X] = dic[X]

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

print(ans)

Solution (Python):
import collections
N = int(input())
A = sorted(list(map(int, input().split())))
ans = 1

AA = collections.Counter(A)
cc = [0]*(max(A)+1)

for i in AA.keys():
    cc[i] = AA[i]

for i in range(len(cc)):
    ans =  max(ans, sum(cc[i:i+3]))

print(ans)

Solution (Python):
import collections
N = int(input())
A = sorted(list(map(int, input().split())))
ans = 1

AA = collections.Counter(A)
cc = [0]*(max(A)+1)

for i in AA.keys():
    cc[i] = AA[i]

for i in range(len(cc)):
    ans =  max(ans, sum(cc[i:i+3]))

print(ans)

Solution (Python):
il = lambda: list(map(int, input().split()))
ils = lambda n, s="int(input())": [eval(s) for _ in range(n)]

n = int(input())
a = il()
dic = dict()
for i in a:
    for j in [-1, 0, 1]:
        if i+j in dic:
            dic[i+j] += 1
        else:
            dic[i+j] = 1
print(max(dic.values()))

Solution (Python):
arr = [0 for _ in range(-1, 10**5+2)]
N = int(input())
for x in list(map(int,input().split())):
    arr[x+1] +=1
    arr[x] +=1
    arr[x-1] +=1

print(max(arr))

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

A.sort()

i=0
j=1
ANS=1

while j<N:
    x=A[i]
    if A[j]<=x+2:
        ANS=max(ANS,j-i+1)
        j+=1
    else:
        i+=1
        
print(ANS)

Solution (Python):
N = int(input())
a = list(map(int, input().split()))
for i in range(N):
    a.append(a[i] - 1)
    a.append(a[i] + 1)
a.sort()
ans = 0
cnt = 1
for i in range(1, len(a)):
    if a[i] == a[i - 1]:
        cnt += 1
    else:
        if cnt > ans:
            ans = cnt
        cnt = 1
if cnt > ans:
    ans = cnt
print(ans)

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

cand_dict = defaultdict(lambda:0)
for a in A:
    cand_dict[a-1] += 1
    cand_dict[a] += 1
    cand_dict[a+1] += 1
print(max(cand_dict.values()))

Solution (Python):

N=int(input())
A=list(map(int,input().split()))
max_A=max(A)
TGS=[0]*(max_A+1)
ans=0
for i in range(N):
    TGS[A[i]]+=1
for i in range(max_A-1):
    if ans<TGS[i]+TGS[i+1]+TGS[i+2]:
        ans=TGS[i]+TGS[i+1]+TGS[i+2]
if max_A<2:
    ans=N
print(ans)

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

input_map = {i: 0 for i in range(10**5+1)}
for a in a_list:
    input_map[a] += 1


max_num = 0
for i in range(1, 10**5):
    max_num = max(input_map[i-1] + input_map[i] + input_map[i+1], max_num)
print(max_num)

Solution (Python):
from collections import defaultdict, Counter
from itertools import product, groupby, count, permutations, combinations
from math import pi, sqrt
from collections import deque
from bisect import bisect, bisect_left, bisect_right
from string import ascii_lowercase
from functools import lru_cache
import sys
sys.setrecursionlimit(10000)
INF = float("inf")
YES, Yes, yes, NO, No, no = "YES", "Yes", "yes", "NO", "No", "no"
dy4, dx4 = [0, 1, 0, -1], [1, 0, -1, 0]
dy8, dx8 = [0, -1, 0, 1, 1, -1, -1, 1], [1, 0, -1, 0, 1, 1, -1, -1]

def inside(y, x, H, W):
    return 0 <= y < H and 0 <= x < W


def ceil(a, b):
    return (a + b - 1) // b


def main():
    N = int(input())
    items = list(sorted(list(Counter(map(int, input().split())).items())))
    ans = 0
    for i in range(len(items)):
        n = items[i][1]
        if i >= 1:
            if abs(items[i][0] - items[i - 1][0]) == 1:
                n += items[i - 1][1]
        if i < len(items) - 1:
            if abs(items[i][0] - items[i + 1][0]) == 1:
                n += items[i + 1][1]
        ans = max(ans, n)

    print(ans)


if __name__ == '__main__':
    main()

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

Solution (Python):

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

    
    ans_lst = [0 for i in range(max(a)+4)]
    """
    for i in range(n):
        lst[3*i] = a[i]
        lst[3*i+1] = a[i]+1
        lst[3*i+2] = a[i]+2
    """

    for i in a:
        ans_lst[i] += 1
        ans_lst[i+1] += 1
        ans_lst[i-1] += 1

    """
    ans = -1
    for i in set(lst):
        ans = max(lst.count(i),ans)
    """

    print(max(ans_lst))


if __name__ == "__main__":
        main()

Solution (Python):

from collections import Counter


def main():
    N = input()
    a = [int(ele) for ele in raw_input().split()]
    a_plus = [ele + 1 for ele in a]
    a_minus = [ele - 1 for ele in a]
    ct = Counter(a)
    ct_plus = Counter(a_plus)
    ct_minus = Counter(a_minus)

    for k, v in ct_plus.items():
        ct[k] += v

    for k, v in ct_minus.items():
        ct[k] += v

    print(ct.most_common()[0][1])


if __name__ == "__main__":
    main()

Solution (Python):
N = int(input())
a = [int(ai) for ai in input().split()]
from collections import Counter
c = Counter(a)
best = 0
for x in c.keys():
    count = c[x] + c.get(x-1, 0) + c.get(x+1, 0)
    best = max(count, best)
print(best)

Solution (Python):
N = int(input())
A = [int(x) for x in input().split()]
C = [0] * (10 ** 5 + 1)
for a in A:
  C[a] += 1
cnt = 0
for i in range(1, 10 ** 5):
  cnt = max(C[i - 1] + C[i] + C[i + 1], cnt)
print(cnt)

Solution (Python):
from collections import Counter

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

count = Counter(A)
sort_count = sorted(count.items())
sort_count.append((0, 0))

best_ans = 0

counter_minus2 = (0, 0)
counter_minus1 = (0, 0)

for i, counter in enumerate(sort_count):
    ans = counter_minus1[1]

    if counter_minus1[0] - counter_minus2[0] == 1:
        ans += counter_minus2[1]

    if counter[0] - counter_minus1[0] == 1:
        ans += counter[1]

    best_ans = max(best_ans, ans)

    counter_minus2 = counter_minus1
    counter_minus1 = counter

print(best_ans)

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

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

num = {}
for i in range(n):
    if not a[i] in num.keys():
        num[a[i]] = 1
    else:
        num[a[i]] += 1
    if not a[i]-1 in num.keys():
        num[a[i]-1] = 1
    else:
        num[a[i]-1] += 1
    if not a[i]+1 in num.keys():
        num[a[i]+1] = 1
    else:
        num[a[i]+1] += 1

ans = 0
for k in num.keys():
    ans = max(ans, num[k])
print(ans)

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

Amax = max(max(A),10)
K = [0] * (Amax+1)

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

ans = 1

for i in range(Amax - 1):
  ans = max(ans, K[i] + K[i+1] + K[i+2])

print(ans)

Solution (Python):
from collections import Counter

n = int(input())
a = [int(v) for v in input().split()]
cnt = Counter(a)

maxv = 0
for k in cnt:
    maxv = max(maxv, cnt[k-1] + cnt[k] + cnt[k+1])
print(maxv)

Solution (Python):
import sys
import math
import collections
import itertools
import array
import inspect


sys.setrecursionlimit(10000)



def chkprint(*args):
    names = {
        id(v): k
        for k, v in inspect.currentframe().f_back.f_locals.items()
    }
    print(', '.join(
        names.get(id(arg), '???') + ' = ' + repr(arg) for arg in args))



def to_bin(x):
    return bin(x)[2:]


def li_input():
    return [int(_) for _ in input().split()]




dp = None


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

    ans = 1
    Bkt = [0] * 100001

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

    for i in range(1, len(A) - 1):
        ans = max(ans, Bkt[i - 1] + Bkt[i] + Bkt[i + 1])

    print(ans)


main()

Solution (Python):
from collections import Counter
N = int(input())
A = list( map( int, input().split()))
CA = Counter(A)
ans = 0
for i in range(1,10**5):
    ans = max( ans, CA[i-1]+CA[i]+CA[i+1])
print(ans)

Solution (Python):
from itertools import groupby
n = int(input())
a = sorted(list(map(int,input().split())))
g = groupby(a)

r = [0]*100010
for k,v in g:
    r[k] = len(list(v))
s = []
for i in range(len(r)-2):
    t = r[i] + r[i+1] + r[i+2]
    s.append(t)
print(max(s))

Solution (Python):
from collections import defaultdict
N = int(input())
a = list(map(int,input().split()))

r = defaultdict(int)

for i in a:
    r[i-1] += 1
    r[i] += 1
    r[i+1] += 1

print(max(r.items(), key=(lambda x : x[1]))[1])

Solution (Python):
n = int(input())
a = [int(_) for _ in input().split()]

def add_dict(dict, n):
    if n in dict.keys():
        dict[n] += 1
    else:
        dict[n] = 1
    return dict

dict = {}
for ai in a:
    dict = add_dict(dict, ai)
    dict = add_dict(dict, ai+1)
    dict = add_dict(dict, ai-1)

print(max(dict.values()))

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

count_dct = {}
for i in int_list:
    if i-1 not in count_dct.keys():
        count_dct[i-1] = 1
    if i not in count_dct.keys():
        count_dct[i] = 1
    if i+1 not in count_dct.keys():
        count_dct[i+1] = 1
        
    if i + 1 in count_dct.keys():
        count_dct[i+1] += 1
    if i  in count_dct.keys():
        count_dct[i] += 1
    if i -1 in count_dct.keys():
        count_dct[i-1] += 1
        
print(max(count_dct.values())-1)

Solution (Python):
'''input
10
0 1 2 3 4 5 6 7 8 9
'''
n = input()
arr = map(int,raw_input().split())
cou = [0]*(10**5+10)
for i in range(n):
	cou[arr[i]]+=1
	cou[arr[i]+1] += 1
	cou[arr[i]-1] += 1
print max(cou)

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

a_list = [a_i-i for a_i in a for i in range(-1, 2)]

d = dict()
for a_i in a_list:
	if a_i not in d:
		d[a_i] = 0
	d[a_i] += 1

print(max(d.values()))

Solution (Python):

from collections import Counter

def main():
    n = int(input())
    a_list = list(map(int, input().split()))
    counter = Counter(a_list)

    max_cnt = 0
    for i in set(a_list):
        cnt = 0
        cnt += counter[i]
        cnt += counter[i - 1]
        cnt += counter[i + 1]
        if cnt > max_cnt:
            max_cnt = cnt
    print(max_cnt)

if __name__ == '__main__':
    main()

Solution (Python):
from collections import Counter

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

A_p1 = [a+1 for a in A]
A_m1 = [a-1 for a in A]

cnt = Counter(A_p1)
cnt += Counter(A)
cnt += Counter(A_m1)

print(max(list(cnt.values())))

Solution (Python):
n=int(input())
l=sorted(list(map(int,input().split())))
p=0
o=len(l)

t=[]
for i in range(o-2):
  while p<o and l[p]-l[i]<=2:
    p+=1
  t.extend([p-i])

if len(l)==1:
  print(1)
elif len(l)==2:
  if abs(l[0]-l[1])<=2:
    print(2)
  else:
    print(1)
else:
  print(max(t))

Solution (Python):
def inpl():
    return input().split()

n = int(input())
a = list(map(int, inpl()))
d = {}
for i in range(n):
    d[a[i]] = d.get(a[i], 0) + 1

ans = 0
for k in d.keys():
    b = 0
    b += d.get(k - 1, 0)
    b += d.get(k, 0)
    b += d.get(k + 1, 0)
    ans = max(ans, b)
print(ans)

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

for i in a :
    if i in d.keys() :
        d[i] += 1
    else :
        d[i] = 1
        
    if i - 1 in d.keys() :
        d[i - 1] += 1
    else :
        d[i - 1] = 1
        
    if i + 1 in d.keys() :
        d[i + 1] += 1
    else :
        d[i + 1] = 1
        
print(max(d.values()))

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

Solution (Python):
from collections import Counter
N = int(input())
a = list(map(int,input().split()))
counter = Counter(a)
ans=[]
for k,v in counter.items():
    if k-1 in counter:
        v+=counter[k-1]
    if k+1 in counter:
        v+=counter[k+1]
    ans.append(v)
print(max(ans))

Solution (Python):
from collections import Counter
N = int(input())
a = list(map(int,input().split()))
counter = Counter(a)
ans=[]
for k,v in counter.items():
    if k-1 in counter:
        v+=counter[k-1]
    if k+1 in counter:
        v+=counter[k+1]
    ans.append(v)
print(max(ans))

Solution (Python):
n = int(input())
a = map(int,input().split())
field = [0 for _ in range(100001)]

for i in a:
    field[i] += 1

ans = 0

for i in range(100001-3):
    ans = max(ans,field[i]+field[i+1]+field[i+2])

print(ans)

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
MAX_N = int(1e5) + 1
cnts = [0 for i in range(MAX_N)]
for a in A:
  cnts[a] += 1
res = 0
for idx in range(1, len(cnts) + 1):
  tmp = sum(cnts[idx-1:idx+2])
  if tmp > res:
    res = tmp
print(res)

Solution (Python):


from collections import Counter 

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


AN_dict = Counter(AN)

ans = 0
for key, value in AN_dict.items():
    tmp = 0
    
    tmp += value
    if key-1 in AN_dict:
        tmp += AN_dict[key-1]
    if key+1 in AN_dict:
        tmp += AN_dict[key+1]
    ans = max(ans, tmp)

print(ans)

Solution (Python):
from collections import Counter
def line_input(f, type):
    return f(map(type, input().split(" ")))

n = input()
a = line_input(list, int)

c = Counter(a)
so = sorted(c)

cnt = [0] * (max(so) + 1)
for idx in range(0, max(so)+1):
    cnt[idx] = cnt[idx-1] + c[idx]

m = 0
for j in range(1, 4):
    for idx in range(len(cnt)-j):
        m = max(m, cnt[idx + j] - cnt[idx])

for i in c.values():
    m = max(m, i)

print(m)

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

sum = [0]*100005

for a in A:
	sum[a] += 1
	sum[a+1] += 1
	sum[a+2] += 1

print(max(sum))

Solution (Python):
from collections import defaultdict as dd
N = input()
a_raw = list(map(int, input().split()))
num_count = dd(int)
for item in a_raw:
    num_count[item] += 1
    num_count[item-1] += 1
    num_count[item+1] += 1
print(max(num_count.values()))

Solution (Python):
n=int(input())
a=[int(i) for i in input().split()]
ans=0
d={}
for i in a:
    if i-1 in d:
        d[i-1]+=1
    else:
        d[i-1]=1
    if d[i-1]>ans: ans=d[i-1]
    if i in d:
        d[i]+=1
    else:
        d[i]=1
    if d[i]>ans: ans=d[i]
    if i+1 in d:
        d[i+1]+=1
    else:
        d[i+1]=1
    if d[i+1]>ans: ans=d[i+1]
print(ans)

Solution (Python):
N=int(input())
a=list(map(int,input().split()))
distribution={}
for element in a:
    for num in [element-1,element,element+1]:
        if num not in distribution:
            distribution[num]=1
        else:
            distribution[num]+=1
ans=0
for key in distribution:
    ans = max(ans,distribution[key])
print(ans)

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
dic = {}
for nums in A:
  if nums in dic:
    dic[nums] += 1
  else:
    dic[nums] = 1
  if nums-1 in dic:
    dic[nums-1] += 1
  else:
    dic[nums-1] = 1
  if nums+1 in dic:
    dic[nums+1] += 1
  else:
    dic[nums+1] = 1
b = list(dic.values())
print(max(b))

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

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

a_max=100001
ans=[0]*(a_max)

for i in range(N):
    ans[a[i]]+=1

ll = [ans[0], ans[0]+ans[1], ans[a_max-2]+ans[a_max-1], ans[a_max-1]]

ans_a=max(ll)

for i in range(a_max-2):
    aa = ans[i]+ans[i+1]+ans[i+2]
    if aa>ans_a:
        ans_a=aa
    
print(ans_a)

Solution (Python):


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

A_RANGE = range(0, 10 ** 5)

num_of = [0] * 10 ** 5
for ai in a:
    num_of[ai] += 1

max_X = 0
max_count = max(num_of)
max_count_indexes = [i for i, x in enumerate(num_of) if x == max_count]
for i in A_RANGE:
    if (i + 1 in A_RANGE) and (i + 2 in A_RANGE):
            max_X = max(num_of[i] + num_of[i + 1] + num_of[i + 2], max_X)
    if (i - 1 in A_RANGE) and (i + 1 in A_RANGE):
            max_X = max(num_of[i - 1] + num_of[i] + num_of[i + 1], max_X)
    if (i - 2 in A_RANGE) and (i - 1 in A_RANGE):
            max_X = max(num_of[i - 2] + num_of[i - 1] + num_of[i], max_X)
print(max_X)

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

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
limit=100000
count=[]
for i in range(limit):
  count.append(0)
for i in range(n):
  count[a[i]]+=1
x=count[0]+count[1]+count[2]
maxx=x
for i in range(limit-3):
  x+=count[i+3]-count[i]
  maxx=max(maxx,x)
print(maxx)

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

Solution (Python):
N=int(input())
A=list(map(int,input().split()))
bucket=[0]*(max(A)+1)
answer=0
for number in A:
    bucket[number]+=1
if len(bucket)<3:
    answer=sum(bucket)
for i in range(1,max(A)):
    candidate=bucket[i-1]+bucket[i]+bucket[i+1]
    if candidate>answer:
        answer=candidate
print(answer)

Solution (Python):
from collections import Counter
import numpy as np

N = int(input())
a = list(map(int, input().split()))
l = [0] * ((10 ** 5) + 5)
c = Counter(a)

for k, v in c.items():
    l[k] = v

c_l = np.cumsum(l)

max_n = c_l[2]
for i in range(3, len(l)):
    max_n = max(max_n, c_l[i] - c_l[i - 3])
print(max_n)

Solution (Python):
import collections
N = int(input())
numbers = [int(i) for i in input().split()]
count = collections.Counter(numbers)
biggest = 1
for i in count:
    counting = count[i-1] + count[i] + count[i+1]
    if  counting > biggest:
        biggest = counting

print(biggest)

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

arr = map(int,raw_input().split())

dic = [0 for i in range(10**5)]

for i in arr:
	dic[i] += 1

ans = 0
for i in range(10**5-2):
	ans = max(ans,dic[i]+dic[i+1]+dic[i+2])

print ans

Solution (Python):

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

A = sorted(A)
l, r, ans = 0, 0, 0

while r < N:
    if A[r] <= A[l] + 2:
        if r - l + 1 > ans:
            ans = r - l + 1
        r += 1
    else:
        l += 1

print(ans)

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

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

for a in A:
    xs[a] += 1
    xs[a+1] += 1
    xs[a+2] += 1

print(max(xs))

Solution (Python):


n = int(input())
A = list(map(int, input().split()))
A_plus = []
for a in A:
    A_plus.append(a+1)
A_minus = []
for a in A:
    A_minus.append(a-1)

B = {}
for a in A:
    if a not in B:
        B[a] = 1
    else:
        B[a] += 1
for a_plus in A_plus:
    if a_plus not in B:
        B[a_plus] = 1
    else:
        B[a_plus] += 1
for a_minus in A_minus:
    if a_minus not in B:
        B[a_minus] = 1
    else:
        B[a_minus] += 1

count = 0
for b in B.keys():
    count = max(count, B[b])

print(count)

Solution (Python):
from collections import Counter
N = int(input())
A = list(map(int, input().split()))
c = Counter()

for i in range(N):
    c[A[i]] += 1
    c[A[i] + 1] += 1
    c[A[i] - 1] += 1

print(max(c.values()))

Solution (Python):
from collections import Counter

N = int(input())
input_list = sorted([int(i) for i in input().split()])
inupt_dict = (Counter(input_list))
result = 0
input_dict_keys = inupt_dict.keys()

if N == 1:
    result = 1
elif N == 2 and input_list[0] == input_list[1]:
    result = 2
elif N == 2 and input_list[0] != input_list[1]:
    if input_list[1] == input_list[0] + 1:
        result = 2
    else:
        result = 1
else:
    for i in range(N-2):
        if i in input_dict_keys:
            if i+1 in input_dict_keys:
                if i+2 in input_dict_keys:
                    result = max(result, (inupt_dict[i] + inupt_dict[i+1] + inupt_dict[i+2]))
    for i in range(N-1):
        if i in input_dict_keys:
            if i+1 in input_dict_keys:
                result = max(result, (inupt_dict[i] + inupt_dict[i+1]))
    for i in range(N):
        if i in input_dict_keys:
            result = max(result, inupt_dict[i])

print(result)

Solution (Python):
cnt=[0]*100002;input()
for x in map(int,input().split()):cnt[x]+=1;cnt[x+1]+=1;cnt[x+2]+=1
print(max(cnt))

Solution (Python):
cnt = [0]*10**5; input()
a = list(map(int,input().split()))
for x in a:cnt[x]+=1
print(max(map(sum,zip(cnt,cnt[1:],cnt[2:]))))

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

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

Solution (Python):
import collections
n = int(input())
a = list(map(int,input().split()))
count = collections.Counter(a)
max = 0
for i in count:
    if count[i] + count[i-1] + count[i+1] > max:
        max = count[i] + count[i-1] + count[i+1]
print(max)

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

Whairetsu={}
Xhairetsu={}
Yhairetsu={}


for i in range(len(AN)):
    if AN[i] not in Xhairetsu.keys():
        Xhairetsu[AN[i]]=1
    elif AN[i] in Xhairetsu.keys():
        Xhairetsu[AN[i]]+=1
    
    if AN[i] -1 not in Whairetsu.keys():
        Whairetsu[AN[i]-1]=1
    elif AN[i]-1 in Whairetsu.keys():
        Whairetsu[AN[i]-1]+=1
    
    if AN[i] +1 not in Yhairetsu.keys():
        Yhairetsu[AN[i]+1]=1
    elif AN[i]+1 in Yhairetsu.keys():
        Yhairetsu[AN[i]+1]+=1
    


anshairetsu={}
for i in range(10**5+1):
    if i in Whairetsu.keys():
        if i in anshairetsu.keys():
            anshairetsu[i]+=Whairetsu[i]
        else:
            anshairetsu[i]=Whairetsu[i]
            
    if i in Xhairetsu.keys():
        if i in anshairetsu.keys():
            anshairetsu[i]+=Xhairetsu[i]
        else:
            anshairetsu[i]=Xhairetsu[i]
            
    if i in Yhairetsu.keys():
        if i in anshairetsu.keys():
            anshairetsu[i]+=Yhairetsu[i]
        else:
            anshairetsu[i]=Yhairetsu[i]
print(max(anshairetsu.values()))

Solution (Python):
from collections import Counter
import math
import sys
def getinputdata():

    
    array_result = []
    
    data = input()

    
    array_result.append(data.split(" "))

    flg = 1

    try:


        while flg:

            data = input()

            array_temp = []

            if(data != ""):
                
                array_result.append(data.split(" "))

                flg = 1

            else:

                flg = 0
    finally:


        return array_result

arr_data = getinputdata()

n = int(arr_data[0][0]) 

arr01 = [int(x) for x in arr_data[1]]
arr02 = [int(x) + 1 for x in arr_data[1]]
arr03 = [int(x)-1 for x in arr_data[1]]



arr = Counter(arr01 + arr02 + arr03)

print(arr.most_common(1)[0][1])

Solution (Python):
N=int(input())
a=list(input().split())
mydict={}
for i in range (len(a)):
    num=int(a[i])
    if str(str(num-1) in mydict)==str(False):
        mydict[str(num-1)]=1
    else:
        mydict[str(num-1)]+=1
    if str(str(num) in mydict)==str(False):
        mydict[str(num)]=1
    else:
        mydict[str(num)]+=1
    if str(str(num+1) in mydict)==str(False):
        mydict[str(num+1)]=1
    else:
        mydict[str(num+1)]+=1
  
print(max(mydict.values()))

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