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())
hoge=list(map(int,input().split()))
huga=[0]*(10**5+1)
for i in hoge:
  if(i==10**5):
    huga[i-1]+=1
    huga[i]+=1
  elif(i==0):
    huga[i]+=1
    huga[i+1]+=1
  else:
    huga[i-1]+=1
    huga[i]+=1
    huga[i+1]+=1
print(max(huga))

Submission status: Accepted

Solution code:
from collections import Counter

N = int(input())
A = [int(zz) for zz in input().split()]
b = Counter(A)
p = list(set(A))
ans = 0
for i in range(len(p)-2):
    q = b[p[i]]+b[p[i+1]]+b[p[i+2]]
    if ans < q:
        ans = q

if N == 2:
    if A[0]-1 <= A[1] <= A[0]+1:
        ans = 2
    else:
        ans = 1
elif N == 1:
        ans = 1

if len(p) == 2:
    if p[0]-1 <= p[1] <= p[0]+1:
        ans = N
    else:
        ans = max(b[p[0]],b[p[1]])
elif len(p) == 1:
    ans = N
    
print(ans)

Submission status: Accepted

Solution code:
N=int(input())
A=list(map(int,input().split()))
D={}
for a in A:
    if a in D:D[a]=D[a]+1
    else:D[a]=1
    if a-1 in D:D[a-1]=D[a-1]+1
    else:D[a-1]=1
    if a+1 in D:D[a+1]=D[a+1]+1
    else:D[a+1]=1
L=list(D.values())
L.sort(reverse=True)
print(L[0])

Submission status: Accepted

Solution code:
N=int(input())

from collections import defaultdict
d = defaultdict(int)

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

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

Submission status: Accepted

Solution code:
n = int(input())
d = {}
a = list(map(int,input().split()))
for i in range(len(a)):
    d[a[i]+1] = 0
    d[a[i]-1] = 0
    d[a[i]] = 0
for i in range(len(a)):
    d[a[i]+1]+=1
    d[a[i]-1]+=1
    d[a[i]]+=1
l = 0
for key in d:
    if(d[key]>l):
        l=d[key]
print(l)

Submission status: Accepted

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

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+2] += 1
    
print(max(cnt))

Submission status: Accepted

Solution code:
# ハッシュで数えてキーでソートして+-1を全探索
# と思ったけど登場してないやつでもいける 3 5 なら4が2個になるのでN個から全探索

n = int(input())
arr = list(map(int, input().split()))
lis = [0 for _ in range(10**5+1)]
hash = dict()
for a in arr:
    lis[a] += 1

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

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
l = list(map(int, input().split()))
import collections
c = collections.Counter(l + list(map(lambda x: x+1, l)) + list(map(lambda x: x-1, l)))
print(c.most_common()[0][1])

Submission status: Accepted

Solution code:
n = int(input())
a = [int(i) for i in input().split()]
b = []
for i in range(n):
    b.extend([a[i], a[i]+1, a[i]-1])

b.sort()
cnt = 1
max_cnt = 0
for i in range(len(b)-1):
    if b[i] == b[i+1]:
        cnt += 1
    else:
        max_cnt = max(max_cnt, cnt)
        cnt = 1

print(max_cnt)

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    A = list(map(int, input().split()))
    C = [0] * (10 ** 5)
    for a in A:
        C[a] += 1
    ans = 0
    for i in range(1, len(C) - 1):
        ans = max(ans, C[i - 1] + C[i] + C[i + 1])
    print(ans)


main()

Submission status: Accepted

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


main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import collections
N=int(input())
L=list(map(int,input().split()))
L.sort()
c = collections.Counter(L)
LL=[c[l-1]+c[l]+c[l+1] for l in set(L)]
print(max(LL))

Submission status: Accepted

Solution code:
import bisect
n=int(input())
al=list(map(int,input().split()))
al.sort()
sa=set(al)
ans=0
for a in sa:
    ans=max(ans,bisect.bisect_right(al,a+1)-bisect.bisect_left(al,a-1))
print(ans)

Submission status: Accepted

Solution code:
import collections

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

res = []

for i in a:
        res.append(i)
        res.append(i + 1)
        res.append(i - 1)

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

Submission status: Accepted

Solution code:
n = int(input())

from collections import defaultdict

d1 = defaultdict(int)

for i in input().split():
    d1[int(i)] += 1

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

for i in d1.keys():
    ans[i-1] += d1[i]
    ans[i]   += d1[i]
    ans[i+1] += d1[i]
print(max(ans))

Submission status: Accepted

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

n = int(input())
a = list(map(int, input().split()))
ans = [0]*(100000+2)
for i in a:
    ans[i-1] += 1
    ans[i] += 1
    ans[i+1] += 1
print(max(ans))

Submission status: Accepted

Solution code:
import numpy as np
N = int(input())
A = np.array(input().split(), dtype = np.int32)

counter = np.bincount(A)
B = counter.copy() # そのままとる
B[1:] += counter[:-1] # 1増やして回収
B[:-1] += counter[1:] # 1減らして回収

answer = B.max()
print(answer)

Submission status: Accepted

Solution code:
import numpy as np
N = int(input())
A = np.array(input().split(), dtype = np.int32)

counter = np.bincount(A)
if len(counter) <= 2:
  answer = counter.sum()
else:
  answer = (counter[2:] + counter[1:-1] + counter[:-2]).max()
print(answer)

Submission status: Accepted

Solution code:
import numpy as np
# 前後1まで回収できる
N = int(input())

# n以下のものをかぞえる
A = np.bincount(np.array(input().split(), dtype=np.int32))

B = A.copy() # そのままとる
B[1:] += A[:-1] # 1増やして回収
B[:-1] += A[1:] # 1減らして回収

answer = B.max()
print(answer)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
ans = [0]*(100000+2)

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

  
print(max(ans))

Submission status: Accepted

Solution code:
N = int(input())
aList = [int(x) for x in input().split()]
x = [0] * 10**5

for a in aList:
    x[a] += 1
    if a > 0:
        x[a-1] += 1
    if N-1 > a:
        x[a+1] += 1
print(max(x))

Submission status: Accepted

Solution code:
from collections import defaultdict, Counter
import itertools

def main():
    num = int(input())
    data = Counter(list(map(int, input().split())))
    min_num, max_num = min(data.keys()), max(data.keys())

    ans = 0
    for i in range(min_num - 1, max_num + 1):
        kari = data[i - 1] + data[i + 1] + data[i]
        ans = max(kari, ans)
    print(ans)



if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N = int(input())

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

a.sort()

#print (a)

a = [-2] + a + [-8]

lst = []
count = 1
for i in range(1,N+2):
    if a[i] == a[i-1]:
        count += 1
    else:
        lst += [[a[i-1], count]]
        count = 1

# print (lst)

ans = 0
lst += [[-10,0], [-20,0]]
# print (lst)
for i in range(0, len(lst)-2):
    count = lst[i][1]
    if lst[i][0] + 1 == lst[i+1][0]:
        count += lst[i+1][1]
        if lst[i][0] +2 == lst[i+2][0]:
            count += lst[i+2][1]
    ans = max(ans, count)

print (ans)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
s = {}
for aa in a:
    if aa not in s.keys():
        s[aa] = 1
    else:
        s[aa] += 1
    
    if aa + 1 not in s.keys():
        s[aa+1] = 1
    else:
        s[aa+1] += 1
    
    if aa - 1 not in s.keys():
        s[aa-1] = 1
    else:
        s[aa-1] += 1

print(max(s.values()))

Submission status: Accepted

Solution code:
n=int(input())
dic=dict()
l=list(map(int,input().split()))
for a in l:
	for i in range(a-1,a+2):
		if i not in dic.keys():
			dic[i]=1
		else:
			dic[i]+=1
print(max(dic.values()))

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys


def solve(N: int, a: "List[int]"):
    counts = [0] * 100000
    a.sort()
    for v in a:
        counts[v] += 1
    ret = 0
    for i in range(len(counts) - 2):
        ret = max(ret, counts[i] + counts[i + 1] + counts[i + 2])
    print(ret)
    return


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    a = [ int(next(tokens)) for _ in range(N) ]  # type: "List[int]"
    solve(N, a)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

dic = {}

for a in A:
    for i in range(-1,2):
        if a + i in dic:
            dic[a+i] += 1
        else:
            dic[a+i] = 1
print(max(list(dic.values())))

Submission status: Accepted

Solution code:
from collections import Counter
n=int(input())
a=sorted(map(int,input().split()))
ans={}
for i in range(n):
    ans[a[i]]=ans.get(a[i],0)+1
    ans[a[i]+1]=ans.get(a[i]+1,0)+1
    ans[a[i]-1]=ans.get(a[i]-1,0)+1
print(max(ans.values()))

Submission status: Accepted

Solution code:
N = int(input())

tmp = input().split(" ")

als = [0]*(10**5 + 10)

for n in tmp:
    nowNum = int(n)
    als[nowNum - 1] += 1
    als[nowNum] += 1
    als[nowNum + 1] += 1


    
print(max(als))

Submission status: Accepted

Solution code:
import collections

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

plus = []
minus = []

for i in a:
    minus.append(i-1)
    plus.append(i+1)

all_l = a + minus + plus
ans = collections.Counter(all_l)
print(ans.most_common(1)[0][1])

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
countlist = []
for i in range(0, 100001):
	countlist.append(0)
for i in range(0, N):
	countlist[A[i]] += 1
max = 0
for i in range(0, 100001):
	sum = countlist[i]
	if i != 0: sum += countlist[i-1]
	if i != 100000: sum += countlist[i+1]
	if max < sum: max = sum
print(max)

Submission status: Accepted

Solution code:
from collections import defaultdict

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

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

v = cnt.values()
print(max(v))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#
# 　　  ⋀_⋀　 
#　　  (･ω･)  
# .／ Ｕ ∽ Ｕ＼
#  │＊　合　＊│
#  │＊　格　＊│ 
#  │＊　祈　＊│ 
#  │＊　願　＊│ 
#  │＊　　　＊│ 
#      ￣
#
import sys
input=sys.stdin.readline
inf=float('inf')
mod = 10**9+7
def INT_(n): return int(n)-1
def MI(): return map(int,input().split())
def MF(): return map(float, input().split())
def MI_(): return map(INT_,input().split())
def LI(): return list(MI())
def LI_(): return [int(x) - 1 for x in input().split()]
def LF(): return list(MF())
def LIN(n:int): return [input() for _ in range(n)]
def LLIN(n: int): return [LI() for _ in range(n)]
def LLIN_(n: int): return [LI_() for _ in range(n)]
def LLI(): return [list(map(int, l.split() )) for l in input()]
def I(): return int(input())
def F(): return float(input())
def ST(): return input().replace('\n', '')
def main():
    from collections import Counter
    N = I()
    a = LI()
    c = Counter(a)
    ans=0
    for i in range(0, max(a) + 1):
        candidate = 0
        for j in range(-1, 2):
            if i + j in c:
                candidate += c[i + j]
        ans = max(ans, candidate)
    print(ans)
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from collections import Counter

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

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

Submission status: Accepted

Solution code:
from collections import defaultdict

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

arr_count = defaultdict(int)

for a in arr:
    arr_count[a] += 1

max_x_count = 0
for x in range(1, 10 ** 5):
    x_count = arr_count[x-1] + arr_count[x] + arr_count[x+1]
    max_x_count = max(max_x_count, x_count)

print(max_x_count)

Submission status: Accepted

Solution code:
import collections as cl

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

plus = [x+1 for x in a]
minus = [x-1 for x in a]
mix = plus  + minus + a #インクリメントしたものデクリメントしたものそのままにしたものを1つの配列に格納
res = cl.Counter(mix) #各要素の個数を数え上げてその最大値となる最初の値を出力する
ans = max(res.values())

print(ans)

Submission status: Accepted

Solution code:
import collections

n=int(input())
a=list(map(int,input().split()))
m=max(a)
cnt=collections.Counter(a)
if m==0:
  print(len(a))
else:
  ans=[0]*(m-1)
  for j in range(m-1):
    ans[j]=cnt[j]+cnt[j+1]+cnt[j+2]
  print(max(ans))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# 辞書を用意して最大値を取得する

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

cnt = {}

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

print(max(cnt.values()))

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

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

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

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

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

  return mx

print(main())

Submission status: Accepted

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

Submission status: Accepted

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

print(ans)

Submission status: Accepted

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

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

if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

ans = 0

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

Submission status: Accepted

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

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

Submission status: Accepted

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

print(max_counter)

Submission status: Accepted

Solution code:
import bisect

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

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

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

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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


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


print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
from collections import Counter


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


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


print(ans)

Submission status: Accepted

Solution code:
from collections import Counter


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


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


print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

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

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

print(max(cnts))

Submission status: Accepted

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

print(max(f))

Submission status: Accepted

Solution code:
import collections

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

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

Submission status: Accepted

Solution code:
# /usr/bin/python
# -*- coding: utf-8 -*-
import math


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

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

Submission status: Accepted

Solution code:
#https://atcoder.jp/contests/abc072/tasks/arc082_a
N = int(input())
ai = list(map(int, input().split()))
num = [0]*(10**5+1)
for a in ai:
   num[a+1] +=1
   num[a] +=1
   if a>0:
    num[a-1] +=1  
print(max(num))

Submission status: Accepted

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

Submission status: Accepted

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

a.sort()

l = 0
r = 1
ans = 0

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

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# -*- coding:utf-8 -*-

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

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


if __name__ == "__main__":
    solve()

Submission status: Accepted

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

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

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


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

Submission status: Accepted

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

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

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

print(r)

Submission status: Accepted

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

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

print(max(memo))

Submission status: Accepted

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

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

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

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

    print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

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

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

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

print(max(count))

Submission status: Accepted

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

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

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

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

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

print(ans)

Submission status: Accepted

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

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

print(max(B))

Submission status: Accepted

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

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

print(max(o))

Submission status: Accepted

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

Submission status: Accepted

Solution code:

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

    return n, alist

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

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

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

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

        near_occurs[a].append(a)

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


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

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


if __name__ == '__main__':
    submit()

Submission status: Accepted

Solution code:
from functools import reduce
import collections
import math

def main():
    # 文字列の2進数を数値にする
    # '101' → '5'
    # 文字列の頭に'0b'をつけてint()にわたす
    # binary = int('0b'+'101',0)

    # 2進数で立っているbitを数える
    # 101(0x5) → 2
    # cnt_bit = bin(5).count('1')
    
    # N! を求める
    # f = math.factorial(N)

    # N の逆元
    # N_inv = pow(N,MOD-2,MOD)

    # nCr
    # Nの階乗 * rの階乗の逆元 * n-rの階乗の逆元
    
    # 切り捨て
    # 4 // 3
    # 切り上げ
    #-(-4 // 3)
    
    # 初期値用:十分大きい数(100億)
    INF = float("inf")

    # 大きな素数
    MOD = 10**9+7
    
    # 1文字のみを読み込み
    # 入力:2
    # a = input().rstrip()
    # 変数:a='2'
    
    # スペース区切りで標準入力を配列として読み込み
    # 入力:2 4 5 7
    # a, b, c, d = (int(_) for _ in input().split())  
    # 変数:a=2 b=4 c=5 d =7
    
    # 1文字ずつ標準入力を配列として読み込み
    # 入力:2 4 5 7
    # a = list(int(_) for _ in input().split())
    # 変数:a = [2, 4, 5, 7]    

    # 1文字ずつ標準入力を配列として読み込み
    # 入力:2457
    # a = list(int(_) for _ in input())
    # 変数:a = [2, 4, 5, 7]
    N = int(input())
    a = list(int(_) for _ in input().split())
    a.sort()
    d = collections.defaultdict(int)
    for i in range(N):
        d[a[i]] +=1
    cnt = 0
    ans = 0

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

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

Submission status: Accepted

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