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:
from collections import Counter
n=int(input())
l=[]
a=list(map(int,input().split()))
for i in range(n):
  l.append(a[i]-1)
  l.append(a[i])
  l.append(a[i]+1)
ans=Counter(l)
print(ans.most_common()[0][1])

Submission status: Accepted

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

Submission status: Accepted

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

from collections import Counter

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

Submission status: Accepted

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

ans = 0
for x,y in a.items():
  cnt = y
  if x-1 in a: cnt += a[x-1]
  if x+1 in a: cnt += a[x+1]
  if cnt > ans: ans = cnt
print(ans)

Submission status: Accepted

Solution code:
from bisect import bisect_right
N = int(input())
A = sorted(list(map(int,input().split())))
cmax = 0
for i in range(N):
    a = A[i]
    ind = bisect_right(A,a+2)
    cmax = max(cmax,ind-i)
print(cmax)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import defaultdict

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

for num in numbers:
    for x in [-1, 0, 1]:
        if num + x >= 0:
            d[num + x] += 1

ans = 0
for k, v in d.items():
    ans = max(ans, v)

print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict

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

cnt = defaultdict(int)
for num in a:
    for i in [-1, 0, 1]:
        cnt[num + i] += 1
ans = 0
for i in cnt:
    ans = max(cnt[i], ans)
print(ans)

Submission status: Accepted

Solution code:
import sys
from collections import Counter
sys.setrecursionlimit(10 ** 7)
f_inf = float('inf')
mod = 10 ** 9 + 7


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

    for i in range(n):
        A.append(A[i] - 1)
        A.append(A[i] + 1)

    D = Counter(A)
    res = 0
    for v in D.values():
        res = max(res, v)

    print(res)

if __name__ == '__main__':
    resolve()

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 range(10**5):
    cnt = c[i-1]+c[i]+c[i+1]
    if ans < cnt:
        ans = cnt
print(ans)

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 range(0, 10 ** 5):
    tmp = C[i - 1] + C[i] + C[i + 1]
    ans = max(ans, tmp)

print (ans)

Submission status: Accepted

Solution code:
import numpy as np
from collections import Counter

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

b = a + 1
c = a - 1

d = np.concatenate((a, b, c))
e = Counter(d)

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

Submission status: Accepted

Solution code:
n = int(input())
A = list(map(int,input().split()))
num = [0 for _ in range(10**6)]
for a in A:
    num[a] += 1
    num[a+1] += 1
    num[a-1] += 1
mx = max(num)
tg = num.index(mx)
print(num[tg])

Submission status: Accepted

Solution code:
from collections import Counter,defaultdict,deque
from heapq import heappop,heappush,heapify
import sys,bisect,math,itertools,fractions
sys.setrecursionlimit(10**8)
mod = 10**9+7
INF = float('inf')
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))

n = inp()
a = inpl()
d = defaultdict(int)
for i in a:
    d[i-1] += 1
    d[i] += 1
    d[i+1] += 1
res = 0
for key in list(d):
    res = max(res, d[key])
print(res)

Submission status: Accepted

Solution code:
n, *a = map(int, open(0).read().split())
cnt = [0] * 100000
for x in a: cnt[x] += 1
ans = 0
for i in range(99998):
    ans = max(ans, cnt[i]+cnt[i+1]+cnt[i+2])
print(ans)

Submission status: Accepted

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


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N,*A=map(int, open(0).read().split())
C=[0]*(10**5+5)
for a in A:
    C[a-1]+=1
    C[a]+=1
    C[a+1]+=1
print(max(C[i] for i in range(10**5+1)))

Submission status: Accepted

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

from collections import Counter
ctr = Counter(A)
ans = 0
for i in range(10**5+1):
    t = ctr[i-1] + ctr[i] + ctr[i+1]
    ans = max(ans,t)
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
lis = list(map(int,input().split()))
num = [0 for i in range(10**5+1)]
for nu in lis:
    if nu > 0:
        num[nu-1] += 1
    num[nu] += 1
    if nu < 10 ** 5:
        num[nu+1] += 1
print(max(num))

Submission status: Accepted

Solution code:
from collections import defaultdict

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

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:
from collections import defaultdict

n = int(input())
nums = list(map(int, input().split()))
frequency = defaultdict(int)

for n in nums:
    for x in [-1, 0, 1]:
        if 0 <= n + x and n + x <= 10 **5:
            frequency[n + x] += 1

ans = 0
for k, v in frequency.items():
    ans = max(ans, v)

print(ans)

Submission status: Accepted

Solution code:
#from math import gcd

from math import factorial as f

from math import ceil,floor,sqrt


import bisect
import re
import heapq


from copy import deepcopy
import itertools

from sys import exit

ii = lambda : int(input())
mi = lambda : map(int,input().split())
li = lambda : list(map(int,input().split()))

yes = "Yes"
no = "No"

def main():
    n = ii()
    a = li()
    acnt = [0 for i in range(100005)]
    for i in a:
        acnt[i]+=1
        acnt[i+1]+=1
        acnt[i+2]+=1
    print(max(acnt))


main()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

counts = {}
for a in A:
    for i in [a-1, a, a+1]:
        if not i in counts:
            counts[i] = 0
        counts[i] += 1


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

print(counts[0][1])

Submission status: Accepted

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

L = [-10**10] +L +[10**10]
li = []
from bisect import bisect,bisect_right,bisect_left
for i in range(n+2):
    #a = (bisect_left(L,L[i]-1),bisect_right(L,L[i]+1),L[i])
    li.append((bisect_left(L,L[i]-1),bisect_right(L,L[i]+1)))

max1 = 0
for i in range(1,len(li)-1):
    max1 = max(max1,li[i][1]-li[i][0])
print(max1)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
import itertools
sys.setrecursionlimit(1000000000)
from heapq import heapify,heappop,heappush,heappushpop
import math
import collections

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

Submission status: Accepted

Solution code:
import sys
import itertools
sys.setrecursionlimit(1000000000)
from heapq import heapify,heappop,heappush,heappushpop
import math
import collections

n = int(input())
a = sorted(list(map(int,input().split())))
c = collections.Counter(a)
item = []
for key,Value in c.items():
    item.append([key,Value])
koho = []
if len(item) == 1:
    print(item[0][1])
    sys.exit()
if item[1][0] - item[0][0] == 1:
    cnt = item[1][1] + item[0][1]
else:
    cnt = item[0][1]
koho.append(cnt)
cnt = 0
for i in range(1,len(item)-1):
    cnt = 0
    if item[i][0] - item[i-1][0] == 1:
        cnt += item[i-1][1] + item[i][1]
    if item[i+1][0] - item[i][0] == 1:
        cnt += item[i+1][1]
    koho.append(cnt)
cnt = 0
if item[-1][0] - item[-2][0] == 1:
    cnt += item[-1][1] + item[-2][1]
else:
    cnt += item[-1][1]
koho.append(cnt)
print(max(koho))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys

readline = sys.stdin.readline

def main():
    N = int(input())
    A = list(map(int, readline().split()))
    count = [0] * (pow(10, 5)+3)
    for a in A:
        count[a] += 1
        count[a+1] += 1
        count[a+2] += 1
    ans = max(count) 
    print(ans)     


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
N = int(input())
A = [int(x) for x in input().split()]
max_val = max(A)
B = [0]*(max_val+3)
for i in range(N):
  B[A[i]]+=1
  B[A[i]+1]+=1
  B[A[i]+2]+=1
print(max(B))

Submission status: Accepted

Solution code:
n = int(input())
x = [0]*100001
a = list(map(int,input().split()))
m = 0
ma = 0
for i in range(n):
    x[a[i]]+=1
for i in range(100001-2):
    m=x[i]+x[i+1]+x[i+2]
    if m>ma:
        ma=m
print(ma)

Submission status: Accepted

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

if __name__ == '__main__':
    main()

Submission status: Accepted

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

if __name__ == '__main__':
    main()

Submission status: Accepted

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


def read():
    N = int(input().strip())
    A = map(int, input().strip().split())
    return N, A


def solve(N, A):
    b = [0 for i in range(10**5+2)]
    for a in A:
        b[a-2] += 1
        b[a-1] += 1
        b[a] += 1
    return max(b)


if __name__ == '__main__':
    inputs = read()
    print(solve(*inputs))

Submission status: Accepted

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

dic_a=defaultdict(int)
for a in alist:
  dic_a[a]+=1
#print(dic_a)

max_answer=0
for v in list(dic_a):
  answer=dic_a[v-2]+dic_a[v-1]+dic_a[v]
  max_answer=max(answer,max_answer)

  answer=dic_a[v-1]+dic_a[v]+dic_a[v+1]
  max_answer=max(answer,max_answer)

  answer=dic_a[v]+dic_a[v+1]+dic_a[v+2]
  max_answer=max(answer,max_answer)
  
print(max_answer)

Submission status: Accepted

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

dic_a=defaultdict(int)
for a in alist:
  dic_a[a]+=1
#print(dic_a)

max_answer=0
for v in dic_a:
  answer=0
  if v-1 in dic_a:
    answer+=dic_a[v-1]
  answer+=dic_a[v]
  if v+1 in dic_a:
    answer+=dic_a[v+1]
  max_answer=max(answer,max_answer)
  
print(max_answer)

Submission status: Accepted

Solution code:
import math

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

a_dict = {}

for a in a_list:
    for aa in [a - 1, a, a + 1]:
        a_dict[aa] = a_dict.get(aa, 0) + 1

print(max(a_dict.values()))

Submission status: Accepted

Solution code:
import sys
from collections import defaultdict
readline = sys.stdin.buffer.readline
sys.setrecursionlimit(10 ** 8)
INF = float('inf')
MOD = 10 ** 9 + 7

def main():
    N = int(input())
    A = list(map(int, input().split()))
    d = defaultdict(int)
    for i in range(N):
        d[A[i]-1] += 1
        d[A[i]] += 1
        d[A[i]+1] += 1
    print(max(d.values()))
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import collections
N = int(input())
a = list(map(int, input().split()))
d = collections.defaultdict(int)
for i in a:
    for j in range(-1,2):
        if i+j >= 0:
            d[i+j] += 1

print(max(d.values()))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys

readline = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)


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

    amax = max(a)

    from collections import Counter

    counter = Counter(a)

    ans = 0
    for i in range(amax + 1):
        cnt = 0
        cnt = counter[i - 1] + counter[i] + counter[i + 1]
        ans = max(ans, cnt)

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import numpy as np
N = int(input())
a = list(map(int, input().split()))
V = np.zeros((max(a)+4))

for v in a:
    V[v:v+3] += 1 
print(int(np.max(V)))

Submission status: Accepted

Solution code:
from collections import Counter

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

l = []
for i in A:
	l.extend([i-1, i, i+1])

count = Counter(l)
cand = sorted(count.most_common(), key=lambda x: x[1], reverse=True)[:3]
cand = list(filter(lambda x: cand[2][1]<=x[1], count.most_common()))
ans = 0
for tpl in cand:
    n = len(list(filter(lambda x: x == tpl[0] or x+1 == tpl[0] or x-1 == tpl[0], A)))
    ans = max(ans, n)

print(ans)

Submission status: Accepted

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

num = [0]*(max(A)+2)

for i in A:
    num[i] += 1
m = max(A)
ans = 0
for i in range(0,max(A)+2):
    if i == 0:
        tmp = num[i] + num[i+1]
    elif i == m+1:
        tmp = num[i-1] + num[i]
    else:
        tmp = num[i-1] + num[i] + num[i+1]
    ans = max(ans,tmp)

print(ans)

Submission status: Accepted

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

import collections
c = collections.Counter(A)

l_ans = []
for i in c.keys():
    ans = 0
    if not c.get(i-1) == None:
        ans += c[i-1]
    if not c.get(i) == None:
        ans += c[i]
    if not c.get(i+1) == None:
        ans += c[i+1]
    l_ans.append(ans)
print(max(l_ans))

Submission status: Accepted

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

s = [0]*(a[-1]+1)
for i in range(n):
    s[a[i]] += 1
ans = 0
if a[-1] - 1 > 0:
    for i in range(1,a[-1]):
        ans = max(ans, s[i] + s[i-1] + s[i+1])
    print(ans)
else:
    print(max(s))

Submission status: Accepted

Solution code:
n = int(input())
A = list(map(int,input().split()))
start = min(A)
goal = max(A)

from collections import Counter
c = Counter(A)

ans = 0

for i in range(start,goal+1):
    ans = max(ans,c[i]+c[i+1]+c[i+2])
    
print(ans)

Submission status: Accepted

Solution code:
import sys
import numpy as np
# 場合によってはnp.unipueを使うのもあり
# 例えば集計値の範囲が狭い場合
input=sys.stdin.buffer.readline
N = int(input())
A = np.array(input().split(), dtype = np.int32)

counter = np.bincount(A)
B = counter.copy()
B[1:] += counter[:-1]
B[:-1] += counter[1:]
answer = B.max()
print(answer)

Submission status: Accepted

Solution code:
N = int(input())
*A, = map(int, input().split())
d = {}

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

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


table = sorted(d.items())

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

Submission status: Accepted

Solution code:
import sys

sys.setrecursionlimit(10 ** 7)
f_inf = float('inf')
mod = 10 ** 9 + 7


def resolve():
    n = int(input())
    A = sorted(list(map(int, input().split())))

    if n == 1:
        print(1)
    else:
        L = []
        for i in range(n):
            L.append(list(range(A[i] - 1, A[i] + 2)))

        ma = L[-1][-1]
        mi = L[0][0]
        res = [0] * (ma - mi + 1)
        for l in L:
            for i in l:
                res[i - mi] += 1

        print(max(res))


if __name__ == '__main__':
    resolve()

Submission status: Accepted

Solution code:
from collections import defaultdict
N = int(input())
A = list(map(int,(input().split())))
c = defaultdict(lambda:0)
for i in range(1,10**5+1):
    c[i]
for i in A:
    c[i] += 1
l = list(c.items())
l.sort()
p = []
for i in range(0,10**5-2):
    p.append(l[i][1]+l[i+1][1]+l[i+2][1])
print(max(p))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
A = list(map(int,input().split()))
Ap = [a+1 for a in A]
Am = [a-1 for a in A]
label = set(A+Ap+Am)
dict = {l:0 for l in label}
for a in A+Ap+Am:
    dict[a]+=1
print(max(dict.values()))

Submission status: Accepted

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

s={}
for i in range(n):
  if a[i] not in s:
    s[a[i]]=1
  else:
    s[a[i]]+=1
ans=0
for i in s:
  x=s.get(i-1, 0)+s[i]+s.get(i+1, 0)
  if x>ans:
    ans=x
print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict

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

dic = defaultdict(int)
for elem in a:
    dic[elem] += 1
    dic[elem - 1] += 1
    dic[elem + 1] += 1

#max valueを出力
print(max(dic.values()))

Submission status: Accepted

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

num_list = [0 for _ in range(10 ** 5)]
for i in a:
    if i > 0:
        num_list[i - 1] += 1
    num_list[i] += 1
    if i < 10 ** 5 - 1:
        num_list[i + 1] += 1
print(max(num_list))

Submission status: Accepted

Solution code:
#ABC072-C
n = int(input())
a = list(map(int,input().split()))
 
cnt = [0] * 100002


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())
an = list(map(int, input().split()))
ans = 0
cnts = Counter(an)
for i in set(an):
    ans = max(ans, cnts[i] + cnts[i+1] + cnts[i-1])
print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict
n = int(input())
a = list(map(int,input().rstrip().split()))
dic = defaultdict(int)
for x in a:
  dic[x-1]+=1
  dic[x]+=1
  dic[x+1]+=1
m=0
for v in dic.values():
  m = max(m,v)   
print(m)

Submission status: Accepted

Solution code:
from collections import Counter
N=int(input())
A=list(map(int,input().split()))
B=[]
for i in A:
    B.append(i-1)
    B.append(i)
    B.append(i+1)

counter=Counter(B)
count=list(counter.values())
print(max(count))

Submission status: Accepted

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

nn=10**5+1
c=[0]*nn
for i in range(n):
    c[a[i]]=c[a[i]]+1
    
cmax=0
for i in range(2,nn):
    cmax=max(cmax,c[i-2]+c[i-1]+c[i])
print(cmax)

Submission status: Accepted

Solution code:
n=int(input())

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

succession=0
pre=-10000
maxnum=0
check=[0,0,0]

for i in a:
#     print(i)
    if pre<i-2:
        succession=0
        pre=i
        maxnum=max(maxnum,sum(check))
        check=[1,0,0]
    elif pre==i:
        check[succession]+=1
    elif pre==i-1 and succession==0:
        succession+=1
        check[1]+=1
        pre=i
    elif pre==i-1 and succession==1:
        succession+=1
        check[2]+=1
        pre=i
    elif pre==i-1 and succession==2:
        maxnum=max(maxnum,sum(check))
        check=[check[1],check[2],1]
        pre=i
    elif pre==i-2:
        maxnum=max(maxnum,sum(check))
        check=[check[succession],0,1]
        succession=2
        pre=i
#     print(check)
maxnum=max(maxnum,sum(check))

print(maxnum)

Submission status: Accepted

Solution code:
import collections
import sys
sys.setrecursionlimit(2000000)
# import math
# import itertools
# import statistics
# import numpy as np
n = int(input())
a = list(map(int, input().split()))
ans = []
for i in range(n):
    ans.append(a[i])
    ans.append(a[i]-1)
    ans.append(a[i]+1)

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

Submission status: Accepted

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

Submission status: Accepted

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

num = [0] * (max(A) + 1)
for i in range(N):
    num[A[i]] += 1

ans = 0
M = len(num)
for i in range(M):
    m = num[i]
    if i > 0:
        m += num[i - 1]
    if i < M - 1:
        m += num[i + 1]
    ans = max(ans, m)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import collections

N=int(input())
L=list(map(int,input().split()))
L=sorted(L)
M=collections.Counter(L)
M=sorted(M.items())
ans=max(M[i][1] for i in range(len(M)))
if len(M)==2:
	if M[0][0]+1==M[1][0] or M[0][0]+2==M[1][0]:
		ans=M[0][1]+M[1][1]
for i in range(1,len(M)-1):
	tmp=0
	tmp+=M[i][1]
	if M[i-1][0]==M[i][0]-1:
		tmp+=M[i-1][1]
	if M[i+1][0]==M[i][0]+1:
		tmp+=M[i+1][1]
	ans=max(ans,tmp)
print(ans)

Submission status: Accepted

Solution code:
# Problem C - Together

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

# initialization
nums = [0]*(10**5 + 1)
max_count = 0

# count
for a in a_nums:
    nums[a] += 1

# max count
for i in range(10**5 + 1):
    if i==0 or i==10**5:
        continue

    tmp_count = nums[i-1] + nums[i] + nums[i+1]
    max_count = max(max_count, tmp_count)

# output
print(max_count)

Submission status: Accepted

Solution code:
# 2020/04/25
# AtCoder Beginner Contest 072 - C

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

# Calc
alist = list()

for i in range(n):
    alist.append(a[i]-1)
    alist.append(a[i])
    alist.append(a[i]+1)

tlist = [0] * 100002
for i in range(len(alist)):
    tlist[alist[i]] = tlist[alist[i]] + 1

# Output
print(max(tlist))

Submission status: Accepted

Solution code:
import numpy as np
import collections

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

b = a - 1
c = a + 1

x = np.concatenate([a, b, c])

y = collections.Counter(x)
print(y.most_common()[0][1])

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split()))
C = {}
for i in range(N):
    a = A[i]
    if a not in C:
        C[a] = 0
    C[a] += 1
C = sorted(list(C.items()),key=lambda x:x[0])
cmax = 0
for i in range(len(C)):
    if i==0:
        c1 = C[0][0]
        cnt = C[0][1]
        if len(C)>1 and C[1][0]==c1+1:
            cnt += C[1][1]
        cmax = max(cmax,cnt)
    elif i==len(C)-1:
        c1 = C[-1][0]
        cnt = C[-1][1]
        if len(C)>1 and C[-2][0]==c1-1:
            cnt += C[-2][1]
        cmax = max(cmax,cnt)
    else:
        c1 = C[i][0]
        cnt = C[i][1]
        if C[i-1][0]==c1-1:
            cnt += C[i-1][1]
        if C[i+1][0]==c1+1:
            cnt += C[i+1][1]
        cmax = max(cmax,cnt)
print(cmax)

Submission status: Accepted

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

ans = 0
p = 0
for x, y in a.items():
    p = y
    if x-1 in a: p += a[x-1]
    if x+1 in a: p += a[x+1]
    if p > ans: ans = p
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from sys import stdin
import sys
import math

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

dict_count = {}
for i in a:
    dict_count[i] = 0

for j in a:
    dict_count[j] += 1

#print(dict_count)

max_count = 0

for key in dict_count.keys():
    max_count = max(max_count, dict_count.get(key - 1, 0) + dict_count.get(key, 0) + dict_count.get(key + 1, 0))

print(max_count)

Submission status: Accepted

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

cnt = {}
for i, ai in enumerate(a):
    cnt.setdefault(ai, 0)
    cnt.setdefault(ai + 1, 0)
    cnt.setdefault(ai - 1, 0)
    #
    cnt[ai] += 1
    cnt[ai + 1] += 1
    cnt[ai - 1] += 1

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

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
ret = [0] * (max(A) + 3) # -1 から始まる

for i in range(N):
    cur = A[i]
    ret[A[i]] += 1
    ret[A[i] + 1] += 1
    ret[A[i] + 2] += 1

print(max(ret))

Submission status: Accepted

Solution code:
# import bisect
from collections import Counter, deque # https://note.nkmk.me/python-scipy-connected-components/
# from copy import copy, deepcopy
# from functools import reduce
# from heapq import heapify, heappop, heappush
# from itertools import accumulate, permutations, combinations, combinations_with_replacement, groupby, product
# import math
# import numpy as np  # Pythonのみ！
# from operator import xor
# import re
# from scipy.sparse.csgraph import connected_components  # Pythonのみ！
# ↑cf.  https://note.nkmk.me/python-scipy-connected-components/
# from scipy.sparse import csr_matrix
# import string
import sys
sys.setrecursionlimit(10 ** 5 + 10)
def input(): return sys.stdin.readline().strip()

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

    AA=Counter(A)

    ans=0
    for k in AA.keys():
       ans=max(ans,AA[k+1]+AA[k]+AA[k-1])
    print(ans)


resolve()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from sys import stdin
import sys
import math

n = int(input())
#a = list(map(int, stdin.readline().rstrip().split()))
a = sorted(list(map(int, stdin.readline().rstrip().split())))


count_max = 1

count_prev = 0
count_curr = 1
count_next = 0

current_value = a[0]
for i in range(1, n):
    if a[i] == current_value:
        count_curr += 1
    elif a[i] == current_value + 1:
        count_next += 1
    elif a[i] == current_value + 2:
        count_max = max(count_max, (count_prev + count_curr + count_next))
        current_value = current_value + 1
        count_prev = count_curr
        count_curr = count_next
        count_next = 1
    elif a[i] >= current_value + 3:
        count_max = max(count_max, (count_prev + count_curr + count_next))
        current_value = a[i]
        count_prev = 0
        count_curr = 1
        count_next = 0
    else:
        print("Error")

count_max = max(count_max, (count_prev + count_curr + count_next))

print(count_max)

Submission status: Accepted

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

ans = Counter() 
for i in range(n):
    ans[a[i]-1]+=1
    ans[a[i]]+=1
    ans[a[i]+1]+=1
print(ans.most_common(1)[0][1])

Submission status: Accepted

Solution code:
def resolve():
    N = int(input())
    A = list(map(int, input().split()))
    d = {}
    for a in A:
        for i in [-1, 0, 1]:
            if not a+i in d:
                d[a+i] = 1
            else:
                d[a+i] += 1
    print(max(d.values()))

if '__main__' == __name__:
    resolve()

Submission status: Accepted

Solution code:
import sys

def input():
    return sys.stdin.readline().strip()

def main():
    N = int(input())
    a = list(map(int,input().split()))
    cnt = [0] * (max(a) + 2)

    for i in a:
        if i != 0:
            cnt[i-1] += 1
        cnt[i] += 1
        cnt[i+1] += 1
    print(max(cnt))

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
# from math import factorial,sqrt,ceil,gcd
# from itertools import permutations as permus
# from collections import deque,Counter
# import re
# from functools import lru_cache # 簡単メモ化 @lru_cache(maxsize=1000)
# from decimal import Decimal, getcontext
# # getcontext().prec = 1000
# # eps = Decimal(10) ** (-100)

import numpy as np
# import networkx as nx
# from scipy.sparse.csgraph import shortest_path, dijkstra, floyd_warshall, bellman_ford, johnson
# from scipy.sparse import csr_matrix
# from scipy.special import comb

# slist = "abcdefghijklmnopqrstuvwxyz"
N = int(input())
arrA = np.array(input().split(),dtype=np.int64)

arr = np.bincount(arrA)
ans = 0
if len(arr)<=2:
    ans = sum(arr)
else:
    for i in range(len(arr)):
        if i+3>len(arr):
            break
        num = sum(arr[i:i+3])
        ans = max(ans,num)

print(ans)
# print(*ans)   # unpackして出力。間にスペースが入る
# for row in board:
#     print(*row,sep="")    #unpackして間にスペース入れずに出力する
# print("{:.10f}".format(ans))
# print("{:0=10d}".format(ans))

Submission status: Accepted

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

a_l_dict = {}
for val in a_l:
    a_l_dict[val] = 0
for val in a_l:
    a_l_dict[val] += 1

ans = 0
for key in a_l_dict.keys():
    ans = max(ans, a_l_dict[key] + a_l_dict.get(key-1,0) + a_l_dict.get(key+1,0))

print(ans)