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()))
X = [0 for i in range(10**5+10)]
for i in A:
    X[i-1] += 1
    X[i] += 1
    X[i+1] += 1
ans = max(X)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
S = list(map(int,input().split()))  
S=sorted(S)
C=[0]*(max(S)+3)
for i in range(N):
    C[S[i]+1]+=1
    C[S[i]+2]+=1
    C[S[i]]+=1
print(max(C))

Submission status: Accepted

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

cnt = [0] * 100100
for x in a:
    cnt[x] += 1

ans = 0
for i in range(100000):
    ans = max(ans, cnt[i] + cnt[i + 1] + cnt[i + 2])
print(ans)

Submission status: Accepted

Solution code:
import sys
import collections

input = sys.stdin.readline


def main():
    N = int(input())
    A = [int(x) for x in input().split()]
    C = collections.Counter(A)

    ans = 0
    for k in range(10 ** 5 + 1):
        ans = max(ans, sum([C[k], C[k + 1], C[k - 1]]))

    print(ans)


    
    

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from collections import defaultdict

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

li_count = defaultdict(int)

# 要素ごとの出現数を記録
for a in li:
    li_count[a] += 1

max_x_count = 0

for x in range(1, 10**5):
    # xにできるやつ
    x_count = li_count[x-1] + li_count[x] + li_count[x+1]
    max_x_count = max(max_x_count, x_count)
print(max_x_count)

Submission status: Accepted

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

Submission status: Accepted

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

num_count = collections.Counter(numbers)
print(max(num_count.values()))

Submission status: Accepted

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

cnt = {}

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

ans = max(cnt.values())

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
A = [int(x) for x in input().split()]
L = [0]*(10**5+1)
for i in range(N):
    L[A[i]] += 1
    L[A[i] + 1] += 1
    if A[i] != 0:
        L[A[i] - 1] += 1
print(max(L))

Submission status: Accepted

Solution code:
N = int(input())
lst = [0] * 100001
for x in map(int, input().split()):
    lst[x] += 1

ret = 0
for x in range(100001):
    ret = max(sum(lst[x : x + 3]), ret)
print(ret)

Submission status: Accepted

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

arr = [0] * (10**5 + 2)
for i in range(n):
    arr[a[i]] += 1
ans = []
for x in range(1,10**5):
    ans.append(arr[x-1] + arr[x] + arr[x+1])

print(max(ans))

Submission status: Accepted

Solution code:
N=int(input())
A=list(map(int,input().split()))
L=[0 for i in range(100001)]
result=0

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

for i in range(100001-2):
    if L[i]+L[i+1]+L[i+2]>result:
        result=L[i]+L[i+1]+L[i+2]

print(result)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
N = int(input())
a = [int(i) for i in input().split()]
aa = a[:]
aaa = a[:]
for i in range(N) :
    aa[i] -=1
for i in range(N) :
    aaa[i] += 1
A = a + aa + aaa
import collections
c = collections.Counter(A)
li = list(c.values())
li.sort()
print(li[-1])

Submission status: Accepted

Solution code:
N = int(input())
alist = list(map(int, input().split()))
from collections import Counter
acounter = Counter(alist)
ans = 0
for num in acounter.keys():
    ans = max(ans, acounter[num - 1] + acounter[num] + acounter[num + 1])
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter
input()
A = list(map(int, input().split()))
C = Counter(A)
def get(i): return C[i] if i in C else 0
print(max(sum(get(x) for x in range(i-1,i+2)) for i in range(10**5)))

Submission status: Accepted

Solution code:
from collections import Counter

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

ans = []
for i in a:
    ans.append(i)
    ans.append(i-1)
    ans.append(i+1)

mc = Counter(ans).most_common()[0][1]
print(mc)

Submission status: Accepted

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

d = defaultdict(int)
for a in actions:
    d[a-1] += 1
    d[a] += 1
    d[a+1] += 1
print(sorted(d.items(), key=lambda x: -x[1])[0][1])

Submission status: Accepted

Solution code:
n = int(input())
cnt = [0] * 100002
a = list(map(int, input().split()))
for v in a:
    cnt[v] += 1
    cnt[v-1] += 1
    cnt[v+1] += 1
    
print(max(cnt))

Submission status: Accepted

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

main()

Submission status: Accepted

Solution code:
from collections import defaultdict

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

hoge = defaultdict(int)
for ai in a:
  hoge[ai+1] += 1
  hoge[ai] += 1
  hoge[ai-1] +=1
print(max(hoge.values()))

Submission status: Accepted

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

from collections import Counter
Z = []

for a in A:
  Z.append(a)
  Z.append(a-1)
  Z.append(a+1)

print(Counter(Z).most_common()[0][1])

Submission status: Accepted

Solution code:
import sys
from collections import Counter
n = int(input())
a_ls = [int(i) for i in sys.stdin.readline().split()]
ct = Counter(a_ls)
_max = max(a_ls)
_min = min(a_ls)
best = 0
for i in range(_min, _max + 1):
    best = max(ct[i] + ct[i+1] + ct[i-1], best)
print(best)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import collections

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

x = []
x_append = x.append

for a in A:
    x_append(a-1)
    x_append(a)
    x_append(a+1)
    
X = collections.Counter(x)

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
a = [int(i) for i in input().split()]
ans = 0
cnt = [0]*(max(a)+1)

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

if len(cnt) < 3:
    ans = sum(cnt)
else:
    for i in range(len(cnt)-2):
        ans = max(ans,cnt[i]+cnt[i+1]+cnt[i+2])

print(ans)

Submission status: Accepted

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

li=[]
for i in a:
  li+=[i-1,i,i+1]

from collections import Counter
c=Counter(li)
print(c.most_common()[0][1])

Submission status: Accepted

Solution code:
n = int(input())
as_ = map(int, input().strip().split())

data = [0] * 100002

for a in as_:
    data[a] += 1

max_count = 0
for i in range(1, 100001):
    count = data[i - 1] + data[i] + data[i + 1]
    max_count = max(count, max_count)

print(max_count)

Submission status: Accepted

Solution code:
N=int(input())
A=list(map(int,input().split()))
m,M=min(A),max(A)
from collections import Counter
A=Counter(A)
ans = 0
for _ in range(m,M+1):
  ans = max(ans,A[_-1]+A[_]+A[_+1])
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split( )))
mx = max(a)
#+4は配列外とかの対応
l = [0 for _ in range(mx+4)]

#a[i]=0の処理が面倒なので+1,+2にずらす
for i in range(n):
    l[a[i]] += 1
    l[a[i]+1] += 1
    l[a[i]+2] += 1
#print(l)
lmax = max(l)

print(lmax)

Submission status: Accepted

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd
from bisect import bisect

def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

N = INT()
a = LIST()

cnt = [0]*10**5

for n in a:
	cnt[n] += 1

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from bisect import bisect_left,bisect_right
N = int(input())
A = sorted(list(map(int,input().split())))
G = {i:0 for i in range(10**5)}
cntmax = 0
for i in range(10**5):
    ind0 = bisect_left(A,i-1)
    ind1 = bisect_right(A,i+1)
    cntmax = max(cntmax,ind1-ind0)
print(cntmax)

Submission status: Accepted

Solution code:
import collections
N=int(input())
s=list(map(int,input().split(' ')))
cand = []
for i in s:
    cand.append(i+1)
    cand.append(i-1)
cand += s
ans = max(collections.Counter(cand).values())
print(ans)

Submission status: Accepted

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

ans = [0]*(max(A)+3)
for a in A:
  ans[a-1] += 1
  ans[a] += 1
  ans[a+1] += 1
  
print(max(ans))

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
d={}
ans=0
for i in range(n):
    for j in range(-1,2):
        try:
            d[a[i]+j]+=1
            ans=max(ans,d[a[i]+j])
        except:
            d[a[i]+j]=1
            ans=max(ans,d[a[i]+j])
print(ans)

Submission status: Accepted

Solution code:
from copy import deepcopy
from sys import exit,setrecursionlimit
import math
from collections import defaultdict,Counter,deque
from fractions import Fraction as frac
import fractions
from functools import reduce
from operator import mul
import bisect
import sys
import logging
import heapq
import itertools

logging.basicConfig(level=logging.ERROR)

input = sys.stdin.readline

setrecursionlimit(1000000)


def main():
	n = int(input())
	a = list(map(int,input().split()))
	d = defaultdict(int)
	for i in a:
		d[i-1] += 1
		d[i]   += 1
		d[i+1] += 1
	ans = -1
	for i in range(-1,max(a)+3):
		if(d[i] > ans):
			ans = d[i]
	print(ans)

main()

Submission status: Accepted

Solution code:
N=int(input())
a=list(map(int,input().split()))
l=[]
for i in range(N):
    l.extend([a[i]-1,a[i],a[i]+1])

import collections
c=collections.Counter(l)

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

Submission status: Accepted

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

tmp = [0] * (max(a)+3)

for i in a:
    for j in range(i-1,i+2):
        tmp[j+1] += 1

print(max(tmp))

Submission status: Accepted

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

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

for i in range(n):
	ans[ls[i]] += 1
	ans[ls[i]+1] += 1
	ans[ls[i]+2] += 1
	
print(max(ans))

Submission status: Accepted

Solution code:
N = int(input())


*A, = map(int, input().split())

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

for i in range(len(A)):
    up = A[i]+1
    down = A[i]-1
    no = A[i]
    L[up]=L[up]+1
    L[down]=L[down]+1
    L[no] = L[no]+1

print(max(L))

Submission status: Accepted

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools
from collections import deque

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

DR = [1, -1, 0, 0]
DC = [0, 0, 1, -1]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
     
def main():
    N = I()
    A = LI()
    freq = [0] * (10**5)
    for a in A:
        freq[a] += 1
    ans = -1
    for i in range(len(freq)):
        if i == 0:
            l = 0
        else:
            l = freq[i-1]

        m = freq[i]

        if i == len(freq)-1:
            r = 0
        else:
            r = freq[i+1]
        ans = max(ans, l + m + r)
    print(ans)
main()

Submission status: Accepted

Solution code:
N=int(input())
a=list(map(int, input().split()))
a.sort()
nums=[0]*(10**5+1)
for i in range(N):
  nums[a[i]]+=1
maxsum=0
for x in range(1, 10**5-1):
  tmp=nums[x-1]+nums[x]+nums[x+1]
  if maxsum<tmp:
    maxsum=tmp
print(maxsum)

Submission status: Accepted

Solution code:
#n = int(input())
#n,k = map(int,input().split())
#x = list(map(int,input().split()))

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

li = [0 for i in range(10**5)]
for i in range(n):
    li[a[i]] += 1

x = 0
for i in range(1,10**5-1):
    x = max(li[i-1]+li[i]+li[i+1],x)

print(x)

Submission status: Accepted

Solution code:
import collections

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

ans = 0
for k,v in C.items():
    ans = max(ans,C[k]+C[k-1]+C[k+1])

print(ans)

Submission status: Accepted

Solution code:
import collections
n = int(input())
*a, = map(int, input().split())

cc = collections.Counter(a)
cc = dict(sorted(cc.items(), key=lambda x: x[1], reverse=True))

ans = 0
for k, v in cc.items():
    t = cc[k]
    if k+1 in cc:
        t += cc[k+1]
    if k-1 in cc:
        t += cc[k-1]

    ans = max(ans, t)

print(ans)

Submission status: Accepted

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

dic={}
for v in al:
    if not v in dic.keys():
        dic[v]=1
    else:
        dic[v]=dic[v]+1
count_l=[]
for v in range(min(al)-1,max(al)+2):
    count=0
    count=dic.get(v-1,0)+dic.get(v,0)+dic.get(v+1,0)
    count_l.append(count)
print(max(count_l))

Submission status: Accepted

Solution code:
import bisect
n=int(input())
a=[int(i) for i in input().split()]
b=[0]*(10**5)
for i in range(n):
    b[a[i]]+=1
ma=0
for i in range(1,10**5-1):
    ma=max(b[i-1]+b[i]+b[i+1],ma)
print(ma)
#累積和でやると早い

Submission status: Accepted

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

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

Submission status: Accepted

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

t = max(a)
num_list = [0] * (t + 2)

for i in range(n):
    num_list[a[i]] += 1
    if a[i] == 0:
        continue
    else:
        num_list[a[i]-1] += 1
    if a[i] == t:
        continue
    else:
        num_list[a[i]+1] += 1

print(max(num_list))

Submission status: Accepted

Solution code:
from collections import Counter, defaultdict
import sys

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

dict = defaultdict(int)
for n in range(N):
    dict[A[n]] += 1
    dict[A[n] + 1] += 1
    if A[n] - 1 >= 1:
        dict[A[n] - 1] += 1

count = sorted(dict.items(), key=lambda x: -x[1])
print(count[0][1])
#print(count)

"""A = Counter(sorted([int(_) for _ in input().split()])).items()
if N == 1:
    print(1)
    sys.exit()

A = list(A)
ans = 0
for n in range(1, len(A) - 1):
    tmp = A[n][1]
    if A[n - 1][0] + 1 == A[n][0]:
        tmp += A[n - 1][1]
    if A[n][0] + 1 == A[n + 1][0]:
        tmp += A[n + 1][1]
    ans = max(ans, tmp)
print(ans)
"""

Submission status: Accepted

Solution code:
N = int(input())
A = [int(x) for x in input().split()]
X = [0]*(10**5+2)
for x in A:
  X[x-1] += 1
  X[x] += 1
  X[x+1] += 1
answer = max(X)
print(answer)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
import collections
N = int(input())
a = [int(x) for x in input().split()]
c = collections.Counter(a)
X = -100
X_c = 0
for i in range(min(a) - 1, max(a) + 2):
    temp = c[i-1] + c[i] + c[i+1]
    if temp >= X_c:
        X = i
        X_c = temp
print(X_c)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
from collections import defaultdict

n, *a = map(int, sys.stdin.read().split())

def main():
    res = defaultdict(int)
    for i in a:
        res[i-1] += 1
        res[i] += 1
        res[i+1] += 1

    return max(res.values()) 
    
if __name__ == '__main__':
    ans = main()
    print(ans)

Submission status: Accepted

Solution code:
n,*a=map(int,open(0).read().split())
b,c=[],[]
for i in a:
  b.append(i-1)
  c.append(i+1)
d=a+b+c
from collections import Counter
e=Counter(d)
f=e.most_common()
print(f[0][1])

Submission status: Accepted

Solution code:
import sys
import numpy as np

stdin = sys.stdin

ri = lambda: int(rs())
rl = lambda: list(map(int, stdin.readline().split()))  # applies to only numbers
rs = lambda: stdin.readline().rstrip()  # ignores trailing space

N = ri()
A = np.array(rl())
c = np.bincount(A)
if len(c) == 1:
    answer = c[0]
else:
    answer = c[0] + c[1]

for i in range(1, len(c)-1):
    result = c[i-1] + c[i] + c[i+1]
    if result > answer:
        answer = result

print(answer)

Submission status: Accepted

Solution code:
import sys
import numpy as np

stdin = sys.stdin

ri = lambda: int(rs())
rl = lambda: list(map(int, stdin.readline().split()))  # applies to only numbers
rs = lambda: stdin.readline().rstrip()  # ignores trailing space

N = ri()
A = np.array(rl())
c = np.bincount(A)
answer = c[0]

for i in range(1, len(c)-1):
    result = c[i-1] + c[i] + c[i+1]
    if result > answer:
        answer = result

print(answer)
# 32

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
f = [0 for i in range(100000 + 10)]
for x in a:
	f[x] = f[x] + 1
mx = f[0] + f[1]
for i in range(1, 100000 + 1):
	mx = max(mx, f[i - 1] + f[i] + f[i + 1])
print(mx)

Submission status: Accepted

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

Submission status: Accepted

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

Memory = {}

# ひとつひとつの要素を見て、+1~-1の範囲の値を確定させる
for i in a:
  p = i + 1
  q = i
  r = -1
  if i >= 0: 
    r = i-1 
  
  # 確定した範囲の値を+1する
  if p in Memory:
    Memory[p] += 1
  else:
    Memory[p] = 1
  if q in Memory:
    Memory[q] += 1
  else:
    Memory[q] = 1
  if r != -1:
    if r in Memory:
      Memory[r] += 1
    else:
      Memory[r] = 1
  
#print(Memory)
  # 最後にキャッシュの中で一番数が多いのが正解
max = 0
maxVal = 0
for k,i in Memory.items():
  if max < i:
    max = i
    maxVal = k

print(max)

Submission status: Accepted

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

a_dic = dict()
for i in range(N):
    if a[i] + 1 in a_dic:
        a_dic[a[i] + 1] += 1
    else:
        a_dic[a[i] + 1] = 1
    if a[i] in a_dic:
        a_dic[a[i]] += 1
    else:
        a_dic[a[i]] = 1
    if a[i] - 1 in a_dic:
        a_dic[a[i] - 1] += 1
    else:
        a_dic[a[i] - 1] = 1
        
ans = 0
for v in a_dic.values():
    if ans < v:
        ans = v
        
print(ans)

Submission status: Accepted

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

b = [0]*(max_a + 2)

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

ans = 0
for i in range(max_a + 1):
    tmp = b[i-1] + b[i] + b[i+1]
    ans = max(ans, tmp)

print(ans)

Submission status: Accepted

Solution code:
def list_count(my_list):
    dic={} #辞書型の宣言
    for num in my_list:
        if num in dic: #もしnumがkeyとして辞書にあれば
            dic[num]+=1 #valueに+1
        else: #なければ
            dic[num]=1 #dicにnumというkeyを追加し、対応するvalueを1とする
    return dic
n=int(input())
a=list(map(int,input().split()))
b=[0]*(3*n)
for i in range(3):
    for j in range(n):
        b[i*n+j]=a[j]+i-1
dict=list_count(b)
print(max(dict.values()))

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import sys


def main()->None:
    N = int(sys.stdin.readline())
    a = list(map(int, sys.stdin.readline().split()))
    max_a = max(a)
    # print(max_a)
    l = [0 for _ in range(max_a+2)] # 配列の初期化、固定長、位置が配列の数字で要素がカウント
    for i in a:
        l[i] += 1
    res = 0
    for i in range(0, max_a+1):
        tmp = l[i-1]+l[i]+l[i+1]
        res = max(res, tmp)
    print(res)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
#072C Together
import sys
n = int(sys.stdin.readline())
A = list(map(int, sys.stdin.readline().split()))
maxA = max(A)
L = [0 for _ in range(maxA+2)]

for a in A:
    L[a]+=1
#print(L)
ans = 0
for i in range(0, maxA+1):
    tmp = L[i-1] + L[i] + L[i+1]
    ans = max(ans, tmp)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import collections

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

A_plus = [x+1 for x in A]
A_minus = [x-1 for x in A]

A_all = A_minus + A_plus + A

Count = collections.Counter(A_all)
ans = Count.most_common()[0][1]

print(ans)

Submission status: Accepted

Solution code:
import bisect
N = int(input())
a = sorted(list(map(int,input().split())))
amin = a[0]
amax = a[-1]
suma = []
for i in range(amin,amax+1):
    sumb = bisect.bisect_right(a,i+1) - bisect.bisect_left(a,i-1)
    suma.append(sumb)
print(max(suma))

Submission status: Accepted

Solution code:
N = int(input())
a = list(map(int,input().split()))
a.sort()
p = 1
q = 0
r = 0
ans = 1
for i in range(N-1):
    if a[i+1] == a[i]+1:
        r = q
        q = p
        p = 1
    elif a[i+1] == a[i]:
        p += 1
    elif a[i+1] == a[i] + 2:
        r = p
        q = 0
        p = 1
    elif a[i+1] != a[i]:
        r = 0
        q = 0
        p = 1
    if p+q+r > ans:
        ans = p+q+r
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
ans = [0 for i in range(max(a)+3)]

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

Submission status: Accepted

Solution code:
def two_int():
    N, K = map(int, input().split())
    return N,K

def one_int():
    return int(input())

def one_str():
    return input()

def many_int():
    return list(map(int, input().split()))

from collections import defaultdict
dicts = defaultdict(int)

N=one_int()
X=many_int()

sorted_X = sorted(X)

for i in sorted_X:
    if i in dicts:
        dicts[i] += 1
    else:
        dicts[i] = 1

max_num = 1
for j in range(sorted_X[0]-1,sorted_X[-1]+1):
    temp_sum = dicts[j-1] + dicts[j] + dicts[j+1]
    
    if max_num < temp_sum:
        max_num = temp_sum

print(max_num)

Submission status: Accepted

Solution code:
import collections
n = int(input())
a = list(map(int,input().split()))
c = dict(collections.Counter(a))
c = dict(sorted(c.items(),key = lambda x:x[0]))

ans = 0
for i in range(1,10**5):
  cnt = 0
  try:
    cnt += c[i-1]
  except KeyError:
    pass
  
  try:
    cnt += c[i]
  except KeyError:
    pass
    
  try:
    cnt += c[i+1]
  except KeyError:
    pass
    
  ans = max(ans,cnt)  
  
print(ans)

Submission status: Accepted

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

al = sorted(al)

import bisect

ans = 0
for i in range(-2,max(al)+2):
    a = bisect.bisect_right(al,i+2) -  bisect.bisect_left(al,i)
    ans = max(ans, a)

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
dct = {}
for i in range(-1,10**5+1):
  dct[i] = 0
for i in A:
  dct[i-1] += 1
  dct[i] += 1
  dct[i+1] += 1

sorted_dct = sorted(dct.items(), key=lambda x:-x[1])
print(sorted_dct[0][1])

Submission status: Accepted

Solution code:
import collections
n = int(input())
a = list(map(int,input().split()))
c = dict(collections.Counter(a))
c = dict(sorted(c.items(),key = lambda x:x[0]))

ans = 0
for i in range(1,10**5):
  cnt = 0
  try:
    cnt += c[i-1]
  except KeyError:
    pass
  
  try:
    cnt += c[i]
  except KeyError:
    pass
    
  try:
    cnt += c[i+1]
  except KeyError:
    pass
    
  ans = max(ans,cnt)  
  
print(ans)

Submission status: Accepted

Solution code:
from bisect import *
N = int(input())
a = list(map(int,input().split()))
a.sort()
ans = 0
min_a = min(a)
max_a = max(a)
for x in range(min_a , max_a + 1):
    r = bisect_right(a , x + 1)
    l = bisect_left(a , x - 1)
    tmp_max = r - l
    if tmp_max > ans:
        ans = tmp_max
print(ans)

Submission status: Accepted

Solution code:
from bisect import bisect_left,bisect_right
N = int(input())
A = list(map(int,input().split()))
A.sort()
ans = 0
for i in range(A[0]-1,A[-1]+1):
    value = bisect_right(A,i+1)-bisect_left(A,i-1)
    if value > ans:
        ans = value
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
a=sorted(list(map(int,input().split())))
def groupby(a):
    a2=[[a[0],1]]
    for i in range(1,len(a)):
        if a2[-1][0]==a[i]:
            a2[-1][1]+=1
        else:
            a2.append([a[i],1])
    return a2
b=groupby(a)
l=len(b)
if l==1:
    print(b[0][1])
elif l==2:
    if b[1][0]-b[0][0]>1:
        print(max(b[1][1],b[0][1]))
    else:
        print(b[1][1]+b[0][1])
else:
    m=0
    for i in range(l-2):
        x=b[i+1][1]
        if b[i+1][0]-b[i][0]==1:
            x+=b[i][1]
        if b[i+2][0]-b[i+1][0]==1:
            x+=b[i+2][1]
        m=max(m,x)
    print(m)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

for i in l:
  ans[i] += 1
  ans[i-1] += 1
  ans[i+1] += 1
  

print(max(ans))

Submission status: Accepted

Solution code:
import sys

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]

def main():
    n = int(input())
    aa = LI()
    cnt = [0] * 100005
    for a in aa:
        cnt[a] += 1

    ans = s = sum(cnt[:3])
    for x in range(100000):
        s += cnt[x + 3] - cnt[x]
        if s > ans: ans = s

    print(ans)

main()

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split()))
X = [0]*(10**5+2)
for a in A:
    X[a-1] += 1
    X[a] += 1

    X[a+1] += 1
maxx = 0
print(max(X))
# maxx_idx = 0
# for idx,x in enumerate(X):
#     if maxx <= x:
#         maxx = x
#         maxx_idx = idx
# print(maxx)

Submission status: Accepted

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

xs = []
for ai in a:
  xs.append(ai)
  xs.append(ai+1)
  xs.append(ai-1)

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

Submission status: Accepted

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

import bisect
li = []
for i in range(n):
    li.append(bisect.bisect(L,L[i]+2)-i)
print(max(li))

Submission status: Accepted

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

tmp = []
for i in range(n):
    tmp.extend([a[i], a[i] - 1, a[i] + 1])

from collections import Counter
c = Counter(tmp)
print(max(list(c.values())))

Submission status: Accepted

Solution code:
import sys
from collections import defaultdict

cnt = defaultdict(int)

def main():
    n, *a = map(int, sys.stdin.read().split())
    a.sort()
    l = a[0]
    r = a[-1]

    for val in a:
        cnt[val] += 1
    
    ans = 0
    for i in range(l, r+1):
        ans = max(ans, cnt[i-1] + cnt[i] + cnt[i+1])

    print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
from collections import defaultdict

cnt = defaultdict(int)

def main():
    n, *a = map(int, sys.stdin.read().split())

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

    print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

L = []
for i in range(N):
    L.append(A[i]-1)
    L.append(A[i])
    L.append(A[i]+1)
    
L.sort()
num = L[0]
cnt = 0
ans = 0
for i in range(len(L)+1):
    if i < len(L) and num == L[i]:
        cnt += 1
    else:
        ans = max(ans, cnt)
        cnt = 1
        num = L[min(i, len(L)-1)]
        
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
a = list(map(int,input().split()))
num = list(range(10**5+1))
val = [0]*(10**5+1)
dic = dict(zip(num,val))
for i in a:
    if i == 0:
        dic[i] += 1
        dic[i+1] += 1
    elif i == 10**5:
        dic[i-1] += 1
        dic[i] += 1
    else:
        dic[i-1] += 1
        dic[i] += 1
        dic[i+1] += 1
print(max(dic.values()))