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



Sample input:
7
3 1 4 1 5 9 2


Sample output:
4

Submission status: Accepted

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

li = [[a[0],1]]

for i in range(1,n):
  if a[i] != a[i-1]:
    li.append([a[i],1])
  else:
    li[len(li)-1][1]+=1
ans = 0
for i in range(len(li)):
  ans_tmp =li[i][1]
  if 0<=i-1:
    if li[i-1][0]+1  == li[i][0]:
      ans_tmp = ans_tmp +li[i-1][1]
  if i+1<len(li):
    if li[i+1][0]-1  == li[i][0]:
      ans_tmp = ans_tmp +li[i+1][1]
  ans = max(ans,ans_tmp)
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter

N = int(input())
a = Counter(list(map(int, input().split())))#.most_common()
max_total = 0

for i in range(min(a), max(a)+1):
    total = 0
    if i in a:
        total += a[i]
    if i-1 in a:
        total += a[i-1]
    if i+1 in a:
        total += a[i+1]
    max_total = max(max_total, total)
    
print(max_total)

Submission status: Accepted

Solution code:
def IL(): return list(map(int,input().split()))
def SL(): return input().split()
def I(): return int(input())
def S(): return input()
n=I()
a=IL()
cnt=[0 for i in range(pow(10,5))]
for i in range(n):
    cnt[a[i]]+=1
m=0
for i in range(2,pow(10,5)):
    m=max(cnt[i]+cnt[i-1]+cnt[i-2],m)
print(m)

Submission status: Accepted

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

result = [0]*(10**5+10)
for a in A:
  result[a] += 1
  result[a-1] += 1
  result[a+1] += 1
print(max(result))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import Counter
n=int(input())
a=list(map(int,input().split()))
b=[i-1 for i in a]+a+[i+1 for i in a]
b=Counter(b).most_common(1)
print(b[0][1])

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import Counter
n=int(input())
A=list(map(int,input().split()))
C=Counter(A)
ans=0
for i in C.keys():
    tmp=C[i-1]+C[i]+C[i+1]
    ans=max(ans,tmp)
print(ans)

Submission status: Accepted

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

count = {}
for ai in a:
  count[ai] = count.get(ai, 0) + 1

ans = 0
for key in count.keys():
  ans = max(ans, count.get(key - 1, 0) + count.get(key) + count.get(key + 1, 0))
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter
n=int(input())
A=list(map(int,input().split()))
Cont=Counter(A)
Cont1=Counter(A)
for k,v in Cont.items():
  Cont1[k-1]+=v
  Cont1[k+1]+=v
print(Cont1.most_common()[0][1])

Submission status: Accepted

Solution code:
N=int(input())
A=list(map(int,input().split()))
from collections import defaultdict
d=defaultdict(int)

for a in A:
    d[a-1]+=1
    d[a]+=1
    d[a+1]+=1
print(max(d.values()))

Submission status: Accepted

Solution code:
N = int(input())
L = list(map(int,input().split()))
L.sort()
L.append(1000000)
M = [0]*((10**5)+1)
c = 0
d = 1
num = L[0]
if N == 1:
  print(1)
elif N == 2:
  if abs(L[0]-L[1]) <=1 :
    print(2)
  else:
    print(1)
else:
  while c <= N-1:
    if L[c] == L[c+1]:
      d += 1
      c += 1
    else:
      M[num] = d
      d = 1
      c += 1
      num = L[c]
  M.insert(0,0)
  M.append(0)
  ans = 0
  for i in range(len(M)-2):
    if M[i]+M[i+1]+M[i+2] >= ans:
      ans = M[i]+M[i+1]+M[i+2]
  print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import collections

from itertools import chain


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

A = [[a-1, a, a+1] for a in A]
A = list(chain.from_iterable(A))

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

Submission status: Accepted

Solution code:
# https://qiita.com/_-_-_-_-_/items/34f933adc7be875e61d0
# abcde	s=input()	s='abcde'
# abcde	s=list(input())	s=['a', 'b', 'c', 'd', 'e']
# 5(1つだけ)	a=int(input())	a=5
# 1 2	| x,y = map(int,input().split())|	x=1,y=2
# 1 2 3 4 5 ... n 　	li = input().split()	li=['1','2','3',...,'n']
# 1 2 3 4 5 ... n 　	li = list(map(int,input().split()))	li=[1,2,3,4,5,...,n]
# FFFTFTTFF 　	li = input().split('T')	li=['FFF', 'F', '', 'FF']

# INPUT
# 3
# hoge
# foo
# bar
# ANSWER
# n=int(input())
# string_list=[input() for i in range(n)]

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

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

ans_l = []
for a_i in a:
  ans_l.append(a_i)
  ans_l.append(a_i-1)
  ans_l.append(a_i+1)

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

Submission status: Accepted

Solution code:
import collections
n = int(input())
a = list(map(int,input().split()))
b = []
for i in range(len(a)):
    b.append(a[i])
    b.append(a[i]-1)
    b.append(a[i]+1)

c = collections.Counter(b)
most = c.most_common()[0]

print(most[1])

Submission status: Accepted

Solution code:
from collections import Counter

n = int(input())
c = Counter(map(int, input().split()))
print(max([c[i-1]+c[i]+c[i+1] for i in range(1, 10**5-1)]))

Submission status: Accepted

Solution code:
from collections import Counter

def solve(N, As):
    c = Counter(As)
    c.update([a + 1 for a in As])
    c.update([a - 1 for a in As])
    return max([v for v in c.values()])

if __name__ == "__main__":
    N = int(input())
    As = list(map(int, input().split(" ")))
    print(solve(N, As))

Submission status: Accepted

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

    count = {}
    for x in a:
        count[x] = count.get(x, 0) + 1
        count[x-1] = count.get(x-1, 0) + 1
        count[x+1] = count.get(x+1, 0) + 1

    # print(count)
    ans = max(count.values())
    return ans


def main():
    print(solve())


if __name__ == '__main__':
    main()

Submission status: Accepted

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

nums = [0]*(10**5+10)
for a in A:
  nums[a] += 1
  nums[a-1] += 1
  nums[a+1] += 1
print(max(nums))

Submission status: Accepted

Solution code:
n = int(input())
li = list(map(int, input().split()))

arr = li.copy()
arr += [i - 1 for i in li]
arr += [i + 1 for i in li]

arr.sort()

ans = 1
cnt = 1
tmp = arr[0]
for a in arr[1:]:
    if a != tmp:
        if cnt > ans:
            ans = cnt
        tmp = a
        cnt = 1
    else:
        cnt += 1

print(ans)

Submission status: Accepted

Solution code:
from collections import Counter
N = int(input())
A = list(map(int, input().split()))
c = Counter(A)
maxval = 0
for i in range(100000):
    x = c[i] if i in c else 0
    y = c[i+1] if i+1 in c else 0
    z = c[i+2] if i+2 in c else 0
    maxval = max(maxval, x+y+z)
print(maxval)

Submission status: Accepted

Solution code:
from collections import Counter
n = int(input())
A = Counter([int(i) for i in input().split()])

ans = 0
for k, v in A.items():
    tmp = v + A[k-1] + A[k+1]
    if tmp > ans:
        ans = tmp
    
print(ans)

Submission status: Accepted

Solution code:
from sys import stdin
input = stdin.readline

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

for i in range(n):
    a = A[i]
    for j in [a-1, a, a+1]:
        if j not in dic:
            dic[j] = 1
        else:
            dic[j] += 1

cou = 0
for x in dic:
    re_cou = dic[x]
    cou = max(cou, re_cou)

print(cou)

Submission status: Accepted

Solution code:
import bisect
N = int(input())
a = list(map(int, input().split()))
a.sort()
s = [0]*(10**5+1)

ans = 0
for i in range(10**5+1-2):
    ans = max(ans, bisect.bisect_right(a, i+2) - bisect.bisect_left(a, i))
print(ans)

Submission status: Accepted

Solution code:
import sys
N = int(input())
a = [int(n) for n in input().split()]
# A = [0]*N
# for i in range(N):
#     A[i] = int(input())
bucket = [0]*100000
for ak in a:
    bucket[ak]+=1
ans = 0
for i in range(1,99999):
    ans = max(ans,bucket[i-1] + bucket[i] + bucket[i+1])
print(ans)

Submission status: Accepted

Solution code:
def abc072_c():
    from collections import Counter
 
    _ = int(input())
    a = list(map(int, input().split()))
    a_ex = []
    for num in a:
        a_ex.append(num)
        a_ex.append(num-1)
        a_ex.append(num+1)
 
    print(max(Counter(a_ex).values()))
 
 
abc072_c()

Submission status: Accepted

Solution code:
import collections
n = int(input())
A = list(map(int,input().split()))
counter = collections.Counter(A)
max_A = max(A)
cnt = 0

for i in range(-1,max_A):
    temp_cnt = counter[i-1] + counter[i] + counter[i+1]
    cnt = max(cnt,temp_cnt)

print(cnt)

Submission status: Accepted

Solution code:
# coding: UTF-8
import sys
#sys.setrecursionlimit(n)
import heapq
import re
import bisect
import random
import math
import itertools
from collections import defaultdict, deque
from copy import deepcopy
from decimal import *

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

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

mx = 0
for dd in d:
    mx = max(d[dd], mx)
print(mx)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
from collections import Counter

sys.setrecursionlimit(200000)


def input():
    return sys.stdin.readline()[:-1]


def ii(t: type = int):
    return t(input())


def il(t: type = int):
    return list(map(t, input().split()))


def imi(N: int, t: type = int):
    return [ii(t) for _ in range(N)]


def iml(N: int, t: type = int):

    return [il(t) for _ in range(N)]


def solve():
    N = ii()
    A = il()
    A_p, A_n = [0] * N, [0] * N
    for i in range(N):
        A_p[i] = A[i] + 1
        A_n[i] = A[i] - 1
    c = Counter(A + A_p + A_n)
    m = max(c.values())
    return m


if __name__ == "__main__":
    print(solve())

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int,input().split()))
check = [0]*((10**5)+1)
for i in a:check[i] += 1
ans = check[0] + check[1]
for i in range(len(check)-2):ans = max(ans,check[i]+check[i+1]+check[i+2])
print(ans)

Submission status: Accepted

Solution code:
import bisect
n = int(input())
a = sorted([int(i) for i in input().split()])
ans = 0
if n > 1:
 for i in range(100005):
  tmp = bisect.bisect_right(a,i+2)-bisect.bisect_left(a,i)
  ans = max(ans,tmp)
else:
  ans = 1
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter

#tmp_list = [1, 1, 1, 1, 0, 1, 1]


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]
d=a+b+c

counter = Counter(d)

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

Submission status: Accepted

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

num = [0] * (10**5+2)
for i in A:
    num[i] += 1
    num[i+1] += 1
    num[i+2] += 1

print(max(num))

Submission status: Accepted

Solution code:
n=int(input())
a=[int(i) for i in input().split()]

import collections
 
c=collections.Counter(a)
l=c.most_common()

from operator import itemgetter

l.sort(key=itemgetter(0))   
    

ans=0
for i in range(len(l)-2):
    if int(l[i][0])+1==int(l[i+1][0]) and int(l[i+1][0])+1==int(l[i+2][0]):
        ans=max(ans,l[i][1]+l[i+1][1]+l[i+2][1])
    elif int(l[i][0]+1)==int(l[i+1][0]):
        ans=max(ans,l[i][1]+l[i+1][1])
    else:
        ans=max(ans,l[i][1])

if len(l)==1:
    ans=l[0][1]

if len(l)==2:
    if int(l[1][0])-int(l[0][0])<=2:
        ans=l[0][1]+l[1][1]
    else:
        ans=max(ans,l[0][1],l[1][1])
        
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
arr = sorted(list(map(int,input().split())))

carr = [0 for _ in range(10**5+1)]

for i in range(N):
    tmp = arr[i]
    carr[tmp] += 1
  
max = 0
for i in range(10**5-2):
    sum = carr[i] + carr[i+1] + carr[i+2]
    if max < sum:
        max = sum
print(max)

Submission status: Accepted

Solution code:
N=int(input())
A=[0]*(10**5+2)

a=list(map(int,input().split()))
for i in range(N):
    for j in range(3):
        A[a[i]-1+j]+=1
print(max(A))

Submission status: Accepted

Solution code:
def abc072_c():
    from collections import Counter

    _ = int(input())
    a = list(map(int, input().split()))
    a_ex = []
    for num in a:
        a_ex.extend([num-1, num, num+1])

    print(max(Counter(a_ex).values()))


abc072_c()

Submission status: Accepted

Solution code:
if __name__ == '__main__':
    n = int(input())
    a = list(map(int, input().split()))

    x = [0]*10**5

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

    ans = 0
    for i in range(len(x)):
        count = 0
        if i == 0:
            ans = max(x[0] + x[1], ans)
        elif i == len(x)-1:
            ans = max(x[len(x)-2] + x[len(x)-1], ans)
        else:
            ans = max(x[i-1] + x[i] + x[i+1], ans)

    print(ans)

Submission status: Accepted

Solution code:
N = int(input())
a = [int(_a) for _a in input().split()]

b = [0] * int(1e5)
c = [0] * int(1e5)
for _a in a:
    b[_a] += 1

for i in range(int(1e5)):
    if i != 0:
        c[i] += b[i-1]
    if i != int(1e5-1):
        c[i] += b[i+1]
    c[i] += b[i]

print(max(c))

Submission status: Accepted

Solution code:
def main():
    _ = input()
    aa = list(map(int, input().split()))
    counts = [0] * 100001
    for a in aa:
        counts[a] += 1
    res = max(counts[0] + counts[1], counts[10000-1] + counts[100000])
    for i in range(1, 100000):
        res = max(res, counts[i - 1] + counts[i] + counts[i + 1])
    print(res)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
if __name__ == '__main__':
    num = int(input())
    int_list = [int(i) for i in input().split()]

    result = {}
    for i in int_list:
        if str(i) in result:
            result[str(i)] += 1
        else:
            result[str(i)] = 1

        if str(i + 1) in result:
            result[str(i + 1)] += 1
        else:
            result[str(i + 1)] = 1

        if str(i - 1) in result:
            result[str(i - 1)] += 1
        else:
            result[str(i - 1)] = 1

    print(max(result.values()))

Submission status: Accepted

Solution code:
import collections

n = int(input())
nums = list(map(int, input().split()))
nums2 = [0] * 3 * n
nums2_i = 0
for num in nums:
  nums2[nums2_i] = num - 1
  nums2_i += 1
  nums2[nums2_i] = num
  nums2_i += 1
  nums2[nums2_i] = num + 1
  nums2_i += 1


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

Submission status: Accepted

Solution code:
import bisect as bi
n = int(input())
a = list(map(int, input().split()))
a.sort()
ans = 0
for i in range(a[0], a[n-1]+1):
	ans = max(ans, bi.bisect_right(a, i+1)-bi.bisect_left(a, i-1))
print(ans)

Submission status: Accepted

Solution code:
import collections
N=int(input())
A=list(map(int,input().split()))
B=dict(collections.Counter(A))
a=0
Bk=B.keys()
for i in Bk:
    if i-1 in Bk:
        if i+1 in Bk:
            if a<B[i]+B[i-1]+B[i+1]:
                a=B[i]+B[i-1]+B[i+1]
        else:
            if a<B[i]+B[i-1]:
                a=B[i]+B[i-1]
    elif i+1 in Bk:
        if a<B[i]+B[i+1]:
            a=B[i]+B[i+1]
    else:
        if a<B[i]:
            a=B[i]
print(a)

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
l=[]
for i in range(max(a)+3):
  l.append(0)
for j in range(n):
  l[a[j]+1]+=1
m=[]
for i in range(1,max(a)+2):
  m.append(l[i-1]+l[i]+l[i+1])
print(max(m))

Submission status: Accepted

Solution code:
import collections as cl
N = int(input())
a_li = [int(i) for i in input().split()]
a_li2 = [i-1 for i in a_li]
a_li3 = [i+1 for i in a_li]
a_li.extend(a_li2)
a_li.extend(a_li3)
c = cl.Counter(a_li)
print(c.most_common()[0][1])

# keys = list(c.keys())
# max_sum = 0
# if (len(c) == 1) :
#     # 1種類しかない
#     print(len(a_li))
# elif (len(c) == 2) :
#     # 2種類ある
#     if (abs(int(keys[0]) - int(keys[1])) <= 2 ) :
#         # 差が2以下なら全部同じ値にできる
#         print(len(a_li))
#     else :
#         # 差が2より大きいなら多いほうの個数が最大
#         if (int(c[keys[0]]) > int(c[keys[1]])) :
#             print(c[keys[0]])
#         else :
#             print(c[keys[1]])
# else :
#     # この思想はNG。keys[k]からkeys[k+2]の差が2以下じゃないと成り立たない。
#     for k in range(len(c) -2 ) :
#         sum_v = c[keys[k]] + c[keys[k+1]] + c[keys[k+2]]
#         if (max_sum < sum_v) :
#             max_sum = sum_v
    
#     print(max_sum)

Submission status: Accepted

Solution code:
n = int(input())
b = [0]*(10**5+4)
a = list(map(int,input().split()))
for i in a:
    b[i] += 1
    b[i+1] += 1
    b[i+2] += 1

print(max(b))

Submission status: Accepted

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

for A in a:
    l = [A-1,A,A+1]
    for i in l:
        if not i in dic:
            dic[i] = 1
        else:
            dic[i] += 1
            
dic = sorted(dic.items(), key=lambda x: x[1])
dic.reverse()
print(dic[0][1])

Submission status: Accepted

Solution code:
n = int(input())
num_list = list(map(int, input().split()))
m = {}
for i in range(-1, 10**5+2):
    m[i] = 0
for i in num_list:
    m[i-1] += 1
    m[i] += 1
    m[i+1] += 1
ans_list = m.values()
print(max(ans_list))

Submission status: Accepted

Solution code:
n = int(input())
a = [int(i) for i in input().split()]
b = []
c = {}
for i in a:
    b.append(i-1)
    b.append(i)
    b.append(i+1)

for i in b:
    if i in c.keys():
        c[i] = c[i]+1
    else:
        c[i] = 1

print(sorted(c.items(),key = lambda x: x[1],reverse = True)[0][1])

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import Counter

n = int(input())
num_list = list(map(int,input().split()))
plus_list  = [num+1 for num in num_list]
minus_list = [num-1 for num in num_list]
result_list = num_list+plus_list+minus_list
result_cntr = Counter(result_list).most_common()
print(result_cntr[0][1])

Submission status: Accepted

Solution code:
n = int(input())
a = [int(i) for i in input().split()]
table = [0 for i in range(10**5+2)]#末尾は番兵
for i in range(len(a)):
    table[a[i]] += 1
    if a[i] > 0:
        table[a[i]-1] += 1
    table[a[i] + 1] += 1
maxCount = 0
for i in range(len(table)-1):
    maxCount = max(maxCount, table[i])
print(maxCount)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
cli = {}
for i in range(-1,10**5+1):
  cli[i] = 0
for j in a:
  cli[j - 1] += 1
  cli[j] += 1
  cli[j + 1] += 1
print(max(cli.values()))

Submission status: Accepted

Solution code:
import bisect

N = int(input())
A = sorted([int(i) for i in input().split()])
ans = 0
for i in set(A):
    l = bisect.bisect_left(A, i-1)
    r =  bisect.bisect_right(A, i+1)
    ans = max(ans, r-l)
print(ans)

Submission status: Accepted

Solution code:
## ABC 072 c
from collections import Counter

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

ad = Counter(a)
cnt = 0
for x in range(a[0]-1, a[-1]+2):
    c = ad[x]+ad[x-1]+ad[x+1]
    cnt = max(c,cnt)

print(cnt)

Submission status: Accepted

Solution code:
N=int(input())
A=list(map(int,input().split()))
B=[0 for i in range(100002)]
for i in range(N):
    B[A[i]-1]+=1
    B[A[i]]+=1
    B[A[i]+1]+=1
print(max(B))

Submission status: Accepted

Solution code:
# encoding: utf-8
N = int(input())
a = list(map(int, input().split()))

cnt = [0] * (10 ** 5 + 2) # +2 for contain (min(ai) - 1) and (max(ai) + 1)

for ai in a:
    cnt[ai] += 1 
    cnt[ai + 1] += 1
    cnt[ai + 2] += 1

print(max(cnt))

Submission status: Accepted

Solution code:
import collections

n=int(input())
l=list(map(int,input().split()))
s=collections.Counter(l)
s=list(s.items())
s=sorted(s,key=lambda x:x[0])
#print(s)
f=[0]*len(s)
for i in range(len(s)):
    f[i]=s[i][1]
    if i>=1:
        if s[i-1][0]==s[i][0]-1:
            f[i]+=s[i-1][1]
    if i<=len(s)-2:
        if s[i+1][0]==s[i][0]+1:
            f[i]+=s[i+1][1]
print(max(f))

Submission status: Accepted

Solution code:
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))

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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])

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
_ = 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))

Submission status: Accepted

Solution code:
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))

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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))

Submission status: Accepted

Solution code:
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)
解()

Submission status: Accepted

Solution code:
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)
解()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
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)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
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)

Submission status: Accepted

Solution code:
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()))

Submission status: Accepted

Solution code:
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])

Submission status: Accepted

Solution code:
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])

Submission status: Accepted

Solution code:
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))

Submission status: Accepted

Solution code:
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])

Submission status: Accepted

Solution code:
import collections

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

#print(ac)

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)

Submission status: Accepted

Solution code:
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))

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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])

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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]
#len(cc)-1にしておくと例えばN=2 A= 0 0のときに不具合が出てくるそれがWAの理由
for i in range(len(cc)):
    ans =  max(ans, sum(cc[i:i+3]))

print(ans)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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()))

Submission status: Accepted

Solution code:
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))

Submission status: Accepted

Solution code:
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)