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 defaultdict


def main():
    _ = input()
    a = [int(an) for an in input().split()]
    cnt = defaultdict(int)
    for an in a:
        cnt[an - 1] += 1
        cnt[an] += 1
        cnt[an + 1] += 1
    print(max(cnt.values()))


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
from collections import defaultdict


def main():
    n = int(input())
    a = [int(an) for an in input().split()]
    cnt = defaultdict(int)
    for an in a:
        for val in {an - 1, an, an + 1}:
            cnt[val] += 1
    print(max(cnt.values()))


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
from collections import defaultdict


def main():
    n = int(input())
    a = [int(an) for an in input().split()]
    cnt = defaultdict(int)
    for an in a:
        cnt[an - 1] += 1
        cnt[an] += 1
        cnt[an + 1] += 1
    print(max(cnt.values()))


if __name__ == "__main__":
    main()

Submission status: Accepted

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

result_num_list = [0] * (10 ** 5 + 5)
for i in a:
    result_num_list[i-1] += 1
    result_num_list[i] += 1
    result_num_list[i+1] += 1

print(max(result_num_list))

Submission status: Accepted

Solution code:
import collections

N = int(input())
As = sorted(list(map(int, input().split())))
c = collections.Counter(As)

L = [0] * (10 ** 5 + 1)
M = [0] * (10 ** 5 + 1)
for (x, y) in c.items():
    L[x] = y

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

print(max(M))

Submission status: Accepted

Solution code:
import collections
n = int(input())
l = sorted(list(map(int, input().split())) )
c = collections.Counter(l)
L = [0]*(10**5+1)
M = [0]*(10**5+1)
 
for x,y in c.items():
    L[x] += y
 
M[0] = L[0]
M[10**5] = L[10**5]
for i in range(1,10**5):
    M[i] = L[i-1] + L[i] + L[i+1]
print(max(M))

Submission status: Accepted

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

c = [0 for _ in range(100005)]

for i in range(n):
    val = a[i]
    c[val - 1] += 1
    c[val] += 1
    c[val + 1] += 1

ans = 0
for i in range(100005):
    ans = max(ans, c[i])

print(ans)

Submission status: Accepted

Solution code:
from collections import Counter
N = int(input())
A = list(map(int, input().split()))
A.sort()
C = Counter(A)
ma = max(2, A[-1])
L = [0]*(ma+1)
for i in range(ma+1):
    l = C.get(i)
    if l == None:
        l = 0
    L[i] = l
# print(L)
ans = [0]*(ma-1)
ans[0] = L[0]+L[1]+L[2]
for i in range(1, ma-1):
    ans[i] = ans[i-1]-L[i-1]+L[i+2]
# print(ans)
print(max(ans))

Submission status: Accepted

Solution code:
from collections import Counter

n = int(input())
A = [int(i) for i in input().split()]
counts = Counter(A)

ans = 0
for k, v in counts.items():
    tmp = v
    if k + 1 in counts.keys():
        tmp += counts[k + 1]
    if k - 1 in counts.keys():
        tmp += counts[k - 1]
    ans = max(tmp, ans)

print(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)
if len(counter) <= 2:
  answer = counter.sum()
else:
  answer = (counter[2:] + counter[1:-1] + counter[:-2]).max()
print(answer)

Submission status: Accepted

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

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

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

Submission status: Accepted

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

B = {}
for i in range(N):
    for j in [-1,0,1]:
        if A[i] + j in B:
            B[A[i]+j] += 1
        else:
            B[A[i]+j] = 1
print(max(B.values()))

Submission status: Accepted

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

print(max(s))

Submission status: Accepted

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

ans = 0
numcnt = [0]*100001

for i in a:
    numcnt[i]+=1
    
for x in range(1,100000):
    subans = numcnt[x-1]+numcnt[x]+numcnt[x+1]
        
    ans = max(ans,subans)
    
print(ans)

Submission status: Accepted

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

ans = 0
numcnt = {i:0 for i in range(100001)}

for i in a:
    numcnt[i]+=1
    
for x in range(1,100000):
    subans = numcnt[x-1]+numcnt[x]+numcnt[x+1]
        
    ans = max(ans,subans)
    
print(ans)

Submission status: Accepted

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

res = {}
for i in range(-1, 10**5+1):
    res[i] = 0

for ai in A:
    for i in [ai-1, ai, ai+1]:
        res[i] += 1

print(max(res.values()))

Submission status: Accepted

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

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

cc=collections.Counter(a)
ans=max(list(cc.values()))

print(ans)

Submission status: Accepted

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

cnt=dict()
# a内の数とその±1をdict型の配列に入れながら個数をカウント
for a in A:
  cnt[a] = cnt.setdefault(a,0) + 1
  cnt[a-1] = cnt.setdefault(a-1,0) + 1
  cnt[a+1] = cnt.setdefault(a+1,0) + 1
  
  
# カウントした数字で昇順にソート  
cnt_sort = sorted(cnt.items(), key=lambda x:x[1])
cnt_sort.reverse()

print(cnt_sort[0][1])

Submission status: Accepted

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

Submission status: Accepted

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

b=list(np.array(a)+1)
c=list(np.array(a)+2)
a=a+b+c

d=np.argmax(np.bincount(a))
print(a.count(d))

Submission status: Accepted

Solution code:
d = dict()

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

for x in a:
  d[x - 1] = d.setdefault(x - 1, 0) + 1
  d[x] = d.setdefault(x, 0) + 1
  d[x + 1] = d.setdefault(x + 1, 0) + 1

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

Submission status: Accepted

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

print(max(b))

Submission status: Accepted

Solution code:
import collections as c
input();a1=list(map(int,input().split()));a2,a3=[i-1 for i in a1],[i+1 for i in a1]
print(c.Counter(c.Counter(a1)+c.Counter(a2)+c.Counter(a3)).most_common(1)[0][1])

Submission status: Accepted

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

nums = [0]*(max(a)+1)
for i in a: nums[i] += 1
ans = sum(nums[:3])
t = sum(nums[:3])
for i in range(len(nums)-3):
  t = t + nums[i+3] - nums[i]
  if ans < t: ans = t
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter

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

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

Submission status: Accepted

Solution code:
import collections

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

b=[]
for i in range(n):
  c=a[i]
  d=a[i]-1
  e=a[i]+1
  b.append(c)
  b.append(d)
  b.append(e)
  
f=collections.Counter(b)

g=f.most_common()
print(g[0][1])

Submission status: Accepted

Solution code:
def tow_pointers(Q=1):

    # Q回分のクエリを処理
    for i in range(Q):

        # 合計値
        res = 0

        # 区間の左端で場合分け
        right = 0
        cnt = 0

        for left in range(n):
            while right < len(a) and a[left] + 2 >= a[right]:
                cnt += 1
                right += 1

            res = max(res, cnt)

            # leftをインクリメントする準備
            if right == left: right += 1
            else: cnt -= 1

        print(res)


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

Submission status: Accepted

Solution code:
from collections import defaultdict
d = defaultdict(int) 

n = int(input()) 
A = list(map(int, input().split())) 
for i in range(n):
    d[A[i]] += 1
    d[A[i]+1] += 1
    d[A[i]-1] += 1
ans = 0
for j,k  in d.items():
    ans = max(k,ans) 
print(ans)

Submission status: Accepted

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

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:
N = int(input())
a = list(map(int, input().split()))
cnt = [0]*(10**7)
for i in a:
    cnt[i-1] += 1
    cnt[i] += 1
    cnt[i+1] += 1
print(max(cnt))

Submission status: Accepted

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

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

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

print(max(cnt))

Submission status: Accepted

Solution code:
kouho = [0]*(10**5+3)#-1は+2に格納

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

for i in range(0,N,1):
    kouho[a[i]]+=1
    kouho[a[i]-1]+=1
    kouho[a[i]+1]+=1

print(max(kouho))

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int,input().split()))
for i in range(n):
    a.append(a[i] + 1)
    a.append(a[i] - 1)
a = sorted(a)
ans = []
c = 1
for i in range(len(a)-1):
    if a[i] == a[i+1]:
        c += 1
    else:
        ans.append(c)
        c = 1
print(max(ans))

Submission status: Accepted

Solution code:
N = int(input())
a = list(map(int, input().split()))
dp = [0] * 100001
for num in a:
    if num == 0:
        dp[num] += 1
        dp[num + 1] += 1
    elif a == 100000:
        dp[num] += 1
        dp[num - 1] += 1
    else:
        dp[num] += 1
        dp[num + 1] += 1
        dp[num - 1] += 1
print(max(dp))

Submission status: Accepted

Solution code:
#
# abc072 c
#

import sys
from io import StringIO
import unittest


class TestClass(unittest.TestCase):
    def assertIO(self, input, output):
        stdout, stdin = sys.stdout, sys.stdin
        sys.stdout, sys.stdin = StringIO(), StringIO(input)
        resolve()
        sys.stdout.seek(0)
        out = sys.stdout.read()[:-1]
        sys.stdout, sys.stdin = stdout, stdin
        self.assertEqual(out, output)

    def test_入力例_1(self):
        input = """7
3 1 4 1 5 9 2"""
        output = """4"""
        self.assertIO(input, output)

    def test_入力例_2(self):
        input = """10
0 1 2 3 4 5 6 7 8 9"""
        output = """3"""
        self.assertIO(input, output)

    def test_入力例_3(self):
        input = """1
99999"""
        output = """1"""
        self.assertIO(input, output)


def resolve():
    N = int(input())
    A = list(map(int, input().split()))
    AN = [0] * 100002  # -1 <= X <= 10^5+1

    for a in A:
        AN[a] += 1
        AN[a+1] += 1
        AN[a+2] += 1
    print(max(AN))


if __name__ == "__main__":
    # unittest.main()
    resolve()

Submission status: Accepted

Solution code:
def main():
	N = int(input())
	a = list(map(int, input().split()))
	
	a_lst = [0] * (10 ** 5 + 1)
	for num in a:
		a_lst[num] += 1

	ans = 0
	for X in range(10 ** 5 + 1):
		cnt = 0

		if X == 0:
			cnt += a_lst[X]
			cnt += a_lst[X + 1]
		elif X == 10 ** 5:
			cnt += a_lst[X - 1]
			cnt += a_lst[X]
		else:
			cnt += a_lst[X - 1]
			cnt += a_lst[X]
			cnt += a_lst[X + 1]

		if cnt > ans:
			ans = cnt

	print(ans)



 
if __name__ == "__main__":
  	main()

Submission status: Accepted

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

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

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

    print(max(lists))    

if __name__ == "__main__":
    solve()

Submission status: Accepted

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

for a in A:
  B[a+1]+=1
  B[a+2]+=1
  B[a]+=1
  
print(max(B))

Submission status: Accepted

Solution code:
n = int(input())
num = list(map(int,input().split()))
lis = [0]*(max(num)+4)
for i in num:
    if i == 0:
        lis[i] +=1
        lis[i+1] +=1
    else:
        lis[i-1] +=1
        lis[i] +=1
        lis[i+1] +=1
    
print(max(lis))

Submission status: Accepted

Solution code:
n = int(input())

a = list(map(int, input().split()))
ans = [0] * (max(a) + 2)

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import collections

def main():
  N = int(input())
  A = [int(a) for a in input().split(" ")]
  CA = collections.Counter(A)
  minA = min(A)
  maxA = max(A)
  X = [CA[i] for i in range(minA, maxA + 1)]
  if len(X) == 1:
    print(X[0])
    return 0
  if len(X) == 2:
    print(X[0] + X[1])
    return 0
    
  Y = [X[j] + X[j+1] + X[j+2] for j in range(len(X) - 2)]
  print(max(Y))

main()

Submission status: Accepted

Solution code:
from collections import Counter
import numpy as np
n = int(input())
a = np.array([int(i) for i in input().split()])
b = a+1
c = a-1
d = np.concatenate([a,b,c])
e = Counter(d)
print(max(e.values()))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from sys import stdin

n = int(stdin.readline().strip())
a_lst = [int(x) for x in stdin.readline().strip().split()]
counter_dict = {}

# create counter dict
for a in a_lst:
  counter_dict[a] = counter_dict.get(a, 0) + 1

# check
max_val = -1
for x in range(min(a_lst), max(a_lst)+1):
  sum_val = counter_dict.get(x-1, 0) + counter_dict.get(x, 0) + counter_dict.get(x+1, 0)
  if sum_val > max_val:
    max_val = sum_val

print(max_val)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
B=[]
C=[]
for i in range(N):
    B.append(A[i]-1)
for i in range(N):
    C.append(A[i]+1)
D=[]
D=A+B+C
mx=0
E=[0]*10**6
for i in D:
    E[i]+=1
    
print(max(E))

Submission status: Accepted

Solution code:
def chk_num(myList,i,n):
    tmp = 0
    if 0<=i-1<n:
        if myList[i-1][0]+1==myList[i][0]:
            tmp += myList[i-1][1]
    if 0<=i<n:
        tmp += myList[i][1]
    if 0<=i+1<n:
        if myList[i][0]+1==myList[i+1][0]:
            tmp += myList[i+1][1]
    return tmp

n = int(input())
a = list(map(int,input().split()))
dic = {}
for i in range(n):
    dic.setdefault(a[i],0)
    dic[a[i]] += 1
#print(dic)
sortedDict = sorted(dic.items())
#print(sortedDict)
ans = 0
for i in range(len(sortedDict)):
    ans = max(ans,chk_num(sortedDict,i,len(sortedDict)))
print(ans)

Submission status: Accepted

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

dic = {i: 0 for i in range(10 ** 5 + 1)}

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

ans = 0

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

Submission status: Accepted

Solution code:
n = int(input())
#a, b = map(int,input().split())
al = list(map(int, input().split()))
#l = [list(map(int,input().split())) for i in range(n)]

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

mx = max(al)
mn = min(al)
ans = 0
for x in range(mn, mx+1):
    ans = max(ans, dic.get(x+1, 0)+dic.get(x, 0)+dic.get(x-1, 0))
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
input()
*A,=map(int,input().split())
D=[0]*(10**7)

for a in A:
    D[a-1]+=1
    D[a]+=1
    D[a+1]+=1
print(max(D))

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import collections

N = int(input())
a = list(map(int,input().split()))
c = collections.Counter(a)
ans = 0
tmp = c.most_common()[0][1]
tmp2 = c.most_common(999999)
for i in range(len(tmp2)):
    tmp3 = tmp2[i][1] + c[tmp2[i][0] + 1] + c[tmp2[i][0] + 2]
    tmp4 = tmp2[i][1] + c[tmp2[i][0] - 1] + c[tmp2[i][0] - 2]
    ans = max(ans,tmp3,tmp4)
print(str(ans))

Submission status: Accepted

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

max_a, min_a = max(a), min(a)
cnt = [0] * (max_a + 1)
for i in range(n):
    cnt[a[i]] += 1
    if a[i] > min_a:
        cnt[a[i]-1] += 1
    if a[i] < max_a:
        cnt[a[i]+1] += 1

print(max(cnt))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import defaultdict

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

count=defaultdict(int)
for i in range(N):
  n = a[i]
  count[n-1] += 1
  count[n] += 1
  count[n+1] += 1

print(sorted(count.values())[-1])

Submission status: Accepted

Solution code:
import collections
n,cc = raw_input(),collections.Counter(map(int, raw_input().split()))
print max([cc[k] + cc[k+1] + cc[k-1] for k in cc] or [0])

Submission status: Accepted

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

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

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

print(ans)

Submission status: Accepted

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

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

t = sum(nums[:3])
ans = t
for i in range(3,10**5+1):
  t = t - nums[i-3] + nums[i]
  if ans < t: ans = t
print(ans)

Submission status: Accepted

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

cnt = [0] * (10 ** 5 + 1)

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

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

Submission status: Accepted

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

ans = 0

for v in a:
    for offset in range(-1, 2):
        val = v + offset
        if (val in cnt):
            cnt[val] += 1
        else:
            cnt[val] = 1

        if cnt[val] > ans:
            ans = cnt[val]

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
A.sort()
ans = 0
now = A[0]
start_idx = 0
while now <= A[-1] + 1:
    tmp_max = 0
    flag = True
    for i in range(start_idx, N):
        if flag and now - 1 == A[i]:
            flag = False
            start_idx = i
        if now - 1 <= A[i] <= now + 1:
            tmp_max += 1
        if now + 1 < A[i]:
            break
    ans = max(tmp_max, ans)
    now += 1
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#from statistics import median
#import collections
#aa = collections.Counter(a) # list to list || .most_common(2)で最大の2個とりだせるお a[0][0]
from fractions import gcd
from itertools import combinations,permutations,accumulate # (string,3) 3回
#from collections import deque
from collections import deque,defaultdict,Counter
import decimal
import re
#import bisect
#
#    d = m - k[i] - k[j]
#    if kk[bisect.bisect_right(kk,d) - 1] == d:
#
#
#
# pythonで無理なときは、pypyでやると正解するかも！！
#
#
# my_round_int = lambda x:np.round((x*2 + 1)//2)
# 四捨五入
import sys
sys.setrecursionlimit(10000000)
mod = 10**9 + 7
#mod = 9982443453
def readInts():
  return list(map(int,input().split()))
def I():
  return int(input())
n = I()
A = readInts()
maxl = [0] * (10**5 + 5)
for a in A:
    for i in range(3):
        maxl[a+i] += 1
print(max(maxl))

Submission status: Accepted

Solution code:
import collections
n = int(input())
a = list(map(int,input().split()))
lst = collections.defaultdict(int)
for i in a:
  lst[i] += 1
  lst[i-1] += 1
  lst[i+1] += 1
  
lst = sorted(lst.items(),reverse=True,key=lambda x:x[1])
print(lst[0][1])

Submission status: Accepted

Solution code:
from collections import Counter
n = int(input())
a = list(map(int, input().split()))
A = Counter(a)
ans = 0
for x,y in A.items():
    z = y + A[x-1] + A[x+1]
    ans = max(z, ans)
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split()))
C = {}
for i in range(N):
    if A[i] not in C:
        C[A[i]] = 0
    C[A[i]] += 1
cmax = 0
for a in A:
    cnt = C[a]
    if a-1 in C:
        cnt += C[a-1]
    if a+1 in C:
        cnt += C[a+1]
    cmax = max(cmax,cnt)
print(cmax)

Submission status: Accepted

Solution code:
import sys
from collections import Counter

sys.setrecursionlimit(10 ** 8)
ini = lambda: int(sys.stdin.readline())
inl = lambda: [int(x) for x in sys.stdin.readline().split()]
ins = lambda: sys.stdin.readline().rstrip()
debug = lambda *a, **kw: print("\033[33m", *a, "\033[0m", **dict(file=sys.stderr, **kw))

N = ini()
A = inl()


def solve():
    c = Counter()
    for a in A:
        c[a - 1] += 1
        c[a] += 1
        c[a + 1] += 1

    x, k = c.most_common()[0]

    return k


print(solve())

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
cnt=[0]*(max(A)+2)
for a in A:
  if a-1>=0:
    cnt[a-1]+=1
  cnt[a]+=1
  cnt[a+1]+=1

print(max(cnt))

Submission status: Accepted

Solution code:
import collections
n=int(input())
a=sorted(list(map(int,input().split())))
c=collections.Counter(a)
counter=0
for i in range(a[0],a[n-1]+1):
    sum=c[i-1]+c[i]+c[i+1]
    if sum>counter:
        counter=sum

print(counter)

Submission status: Accepted

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

ans=a.copy()


#-1したもの
for i in range(n):
    if a[i]>=1:
        ans.append(a[i]-1)

for j in range(n):
    if a[j]<10**5:
        ans.append(a[j]+1)

from collections import Counter

c=Counter(ans)

v=max(c.values())

print(v)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
result = {}
for a in A:
    result[a-1] = result.get(a-1, 0) + 1
    result[a] = result.get(a, 0) + 1
    result[a+1] = result.get(a+1, 0) + 1
print(max((result.values())))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
from collections import Counter
readline=sys.stdin.readline

n=int(readline().strip())
a=list(map(int,readline().split()))

ea=Counter(a+list(map(lambda x:x+1,a)) + list(map(lambda x:x-1,a)))
print(max(list(ea.values())))

Submission status: Accepted

Solution code:
import collections
N = int(input())
Num,Count = zip(*sorted(collections.Counter(int(T) for T in input().split()).most_common()))
DisN = ((min(Num)-1),)+Num+((max(Num)+1),)
DisC = (0,)+Count+(0,)
MAX = 0
for T in range(1,len(Num)+1):
    Dis = DisC[T]
    if DisN[T-1]==DisN[T]-1:
        Dis += DisC[T-1]
    if DisN[T+1]==DisN[T]+1:
        Dis += DisC[T+1]
    if MAX<Dis:
        MAX = Dis
print(MAX)

Submission status: Accepted

Solution code:
from collections import defaultdict
n=int(input())
a=list(map(int,input().split()))
dd=defaultdict(lambda:0)
mina,maxa=0,10**5
for aa in a:
  mina=min(mina,aa)
  maxa=max(maxa,aa)
  dd[aa]+=1
maxsum=0
for i in range(mina, maxa-1):
  maxsum=max(maxsum, dd[i]+dd[i+1]+dd[i+2])
print(maxsum)

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+1):
  ans = max(ans, C[i-1]+C[i]+C[i+1])

print(ans)

Submission status: Accepted

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

c = collections.Counter(a)

for x in range(max(a) + 2):
	d = c[x] + c[x-1] + c[x+1]
	if d >= M:
		M = d

print(M)

Submission status: Accepted

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

import collections

d=collections.Counter(L)
S=d[0]+d[1]
for i in range(1, 10**5+1):
	s=d[i-1]+d[i]+d[i+1]
	S=max(S,s)
print(S)

Submission status: Accepted

Solution code:
from collections import defaultdict

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

for x in A:
  D[x-1] += 1
  D[x] += 1
  D[x+1] += 1

print(max(D.values()))

Submission status: Accepted

Solution code:
import sys
from collections import Counter

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


def main():
    N, *A = map(int, read().split())

    counter = Counter()
    for a in A:
        counter[a - 1] += 1
        counter[a] += 1
        counter[a + 1] += 1

    ans = max(counter.values())

    print(ans)
    return


if __name__ == '__main__':
    main()

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

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
n = int(input())
A = list(map(int, input().split()))
c = [0]*100000
ans = 0
for a in A:
    c[a] += 1

for i in range(1, 99999):
    cnt = c[i-1] + c[i] +c[i+1]
    ans = max(ans, cnt)

print(ans)

Submission status: Accepted

Solution code:
maxa=0
N=int(input())
L=list(map(int,input().split()))
c=[0]*100000
for i in L:
  c[i]+=1
for i in range(1,99999):
  s=c[i]+c[i-1]+c[i+1]
  maxa=max(maxa,s)
print(maxa)

Submission status: Accepted

Solution code:
N = int(input())
A = [int(i) for i in input().split()]
count = {}
for a in A:
    count.setdefault(a, 0)
    count[a] += 1
m = 0
for k in count:
    c = count[k]
    if k-1 in count:
        c += count[k-1]
    if k+1 in count:
        c += count[k+1]
    m = max(m, c)
print(m)

Submission status: Accepted

Solution code:

#!/usr/bin/python
# -*- coding: utf-8 -*-

import numpy as np

def int_mtx(N):
    x = []
    for _ in range(N):
        x.append(input())
    return x

def int_map():
    return map(int,input().split())

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

import collections as col

N = int(input())
a = int_list()

c = col.Counter(a)

ans = 0

for i in range(1,100000):
    ans = max(ans,c[i-1]+c[i]+c[i+1])

print(ans)

Submission status: Accepted

Solution code:
import sys, re
from math import ceil, floor, sqrt, pi, factorial, gcd
from copy import deepcopy
from collections import Counter, deque
from heapq import heapify, heappop, heappush
from itertools import accumulate, product, combinations, combinations_with_replacement
from bisect import bisect, bisect_left, bisect_right
from functools import reduce
from decimal import Decimal, getcontext
# input = sys.stdin.readline 
def i_input(): return int(input())
def i_map(): return map(int, input().split())
def i_list(): return list(i_map())
def i_row(N): return [i_input() for _ in range(N)]
def i_row_list(N): return [i_list() for _ in range(N)]
def s_input(): return input()
def s_map(): return input().split()
def s_list(): return list(s_map())
def s_row(N): return [s_input for _ in range(N)]
def s_row_str(N): return [s_list() for _ in range(N)]
def s_row_list(N): return [list(s_input()) for _ in range(N)]
def lcm(a, b): return a * b // gcd(a, b)
sys.setrecursionlimit(10 ** 6)
INF = float('inf')
MOD = 10 ** 9 + 7
num_list = []
str_list = []

def main():
    n = i_input()
    num_list = i_list()

    tmp_list = [0] * 10 ** 5

    for i in range(n):
        tmp_list[num_list[i]] += 1

    count = 0

    for i in range(10**5-2):
        tmp = tmp_list[i] + tmp_list[i+1] + tmp_list[i+2]
        count = max(count, tmp)

    print(count)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

ans = 0
A = sorted(A)
#print(A)
l = 0
for i in range(1, N):
  while A[i] - A[l] > 2:
    l += 1
  now = i - l + 1
  ans = max(ans, now)
  #print(i, l, ans, now)
  
print(max(ans, 1))

Submission status: Accepted

Solution code:
from collections import Counter

n = int(input())
l = list(map(int, input().split()))
c = Counter(l)
s = set(l)
ans = 0

for i in s:
    ans = max(c[i-1] + c[i] + c[i+1], ans)
print(ans)