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 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 = list(map(int,input().split()))

cnt = Counter(arrA)
maxnum = max(arrA)
ans = 0
for i in range(maxnum+1):
    num = cnt[i-1] + cnt[i] + cnt[i+1]
    ans = max(ans,num)
    # print(i,num,ans)

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import sys
from collections import defaultdict
sys.setrecursionlimit(10**9)
INF=10**18
MOD=10**9+7
input=lambda: sys.stdin.readline().rstrip()
YesNo=lambda b: bool([print('Yes')] if b else print('No'))
YESNO=lambda b: bool([print('YES')] if b else print('NO'))
int1=lambda x:int(x)-1

def main():
    N=int(input())
    a=list(map(int,input().split()))
    d=defaultdict(int)
    for x in a:
        d[x]+=1
        d[x-1]+=1
        d[x+1]+=1
    ans=0
    for i in range(10**5+1):
        ans=max(ans,d[i])
    print(ans)
    
if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def myAnswer(N:int,A:list) -> int:
   if(N == 1): return 1
   dic ={}
   for a in A:
      if(a == 0):
         for i in range(2):
            if(i in dic.keys()):
               dic[i]+=1
            else:
               dic[i]=1
      else:
         for i in range(a-1,a+2):
            if(i in dic.keys()):
               dic[i]+=1
            else:
               dic[i] = 1
   return max(list(dic.values()))

def modelAnswer():
   return
def main():
   N = int(input())
   A = list(map(int,input().split()))
   print(myAnswer(N,A[:]))
if __name__ == '__main__':
   main()

Submission status: Accepted

Solution code:
def ii():return int(input())
def iim():return map(int,input().split())
def iil():return list(map(int,input().split()))
def ism():return map(str,input().split())
def isl():return list(map(str,input().split()))

n = ii()
A = iil()
ans = 0

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

for i in A:
    if i != 0:
        l[i-1] += 1
    l[i] += 1
    l[i+1] += 1
#print(l)
print(max(l))

Submission status: Accepted

Solution code:
import collections
N = int(input())
A = list(map(int,input().split()))
Am = [a-1 for a in A]
Ap = [a+1 for a in A]
A.extend(Am)
A.extend(Ap)
# print(A)
c = collections.Counter(A)
print(c.most_common()[0][1])

Submission status: Accepted

Solution code:

import sys
import collections
#import numpy as np

readline = sys.stdin.readline

def main():
    N = int(input())
    #A1 = np.array(list(map(int, readline().split())))
    A = list(map(int, readline().split()))

    count = [0] * (max(A) + 3)
    for a in A:
        count[a+1] += 1
        count[a] += 1
        count[a+2] += 1

    print(max(count))


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
def resolve():
    '''
    code here
    '''
    import collections
    N = int(input())
    A_list = [int(item) for item in input().split()]

    new_list = []

    for item in A_list:
        new_list += [item + i for i in [-1, 0, 1]]

    item_dict = collections.Counter(new_list)
    print(max(item_dict.values()))

if __name__ == "__main__":
    resolve()

Submission status: Accepted

Solution code:
n=int(input())
g=list(map(int,input().split()))
f=max(g)
l=[0]*(f+2)
for i in g:
  for j in range(max(0,i-1),min(i+2,f+1)):
    l[j]+=1
print(max(l))

Submission status: Accepted

Solution code:
N = int(input())
A = [int(i) for i in input().split()]
A_count = [0] * (max(A)+1)

for i in A:
    A_count[i] += 1

max_count = 0
if len(A_count) < 4:
    print(sum(A_count))
else:
    for X in range(1, len(A_count)-1):
        count = A_count[X-1] + A_count[X] + A_count[X+1]
        if count > max_count:
            max_count = count
    print(max_count)

Submission status: Accepted

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

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

print(max(cnt.values()))

Submission status: Accepted

Solution code:
def ABC072C(d,i):
    if i not in d:
        d[i]=1
    else:
        d[i]+=1
    return d


n=int(input())
a=map(int,input().split())
d=dict()
for i in a:
    d=ABC072C(d,i-1)
    d=ABC072C(d,i)
    d=ABC072C(d,i+1)
print(max(d.values()))

Submission status: Accepted

Solution code:
from collections import defaultdict
N = int(input())
A = list(map(int,input().split()))
d = defaultdict(int)
for i in A:
    d[i] += 1
ans = 0
for i in range(10 ** 5):
    ans = max(ans, d[i-1] + d[i] + d[i+1])
print(ans)

Submission status: Accepted

Solution code:
# https://atcoder.jp/contests/abc072/tasks/arc082_a
from collections import defaultdict
n = int(input())
nums = [int(i) for i in input().split()]

c = defaultdict(int)
for num in nums:
    c[num - 1] += 1
    c[num] += 1
    c[num + 1] += 1

ans = 0
for v in c.values():
    ans = max(ans, v)
print(ans)

Submission status: Accepted

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

def main():
    N = int(readline())
    A = list(map(int, readline().rstrip().split()))
    c = Counter(A)
    mi = min(A)+1
    m = c[mi-1] + c[mi] + c[mi+1]
    ans = m
    for i in range(mi+1, max(A)):
        m -= c[i-2]
        m += c[i+1]
        ans = max(ans, m)
    
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

cand = [0] * 10**5

for i in range(len(A)):
    cand[A[i]] += 1
    if A[i] < len(A)-1:
        cand[A[i]+1] += 1
    if A[i] > 0:
        cand[A[i]-1] += 1

print(max(cand))

Submission status: Accepted

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

cand = [0] * 10**5

for i in range(len(A)):
    cand[A[i]] += 1
    if A[i] < len(A)-1:
        cand[A[i]+1] += 1
    if A[i] > 0:
        cand[A[i]-1] += 1

print(max(cand))

Submission status: Accepted

Solution code:
# solution
import io
import math

data = int(input())
array = list(map(int,input().split()))
l = [0] * (10**5 + 2)
for x in array:
  l[x-1] += 1
  l[x] += 1
  l[x+1] += 1
print(max(l))

Submission status: Accepted

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

for i in range(min(a)-1,max(a)+2):
    d[i] = 0

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

ans = 0
for i in range(min(a),max(a)+1):
    tmp = d[i-1]+d[i]+d[i+1]
    if ans < tmp:
        ans = tmp
print (ans)

Submission status: Accepted

Solution code:
import numpy as np
N = int(input())
A = np.array(list(map(int, input().split())))
counter = np.append(0, np.bincount(A, minlength=10**5 + 10))
# print(counter)
cumsum = counter.cumsum()
# print(cumsum)
# print((cumsum[3:] - cumsum[:-3]))
print((cumsum[3:] - cumsum[:-3]).max())

Submission status: Accepted

Solution code:
from collections import Counter
N = int(input())
a = [int(i) for i in input().split()]
ans = dict(Counter(a))
ans1 = []
for i in range(min(a),max(a)+1):
    count = 0
    if i-1 in ans:
        count += ans[i-1]
    if i in ans:
        count += ans[i]
    if i+1 in ans:
        count += ans[i+1]
    ans1.append(count)
print(max(ans1))

Submission status: Accepted

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

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

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

print(max(C))

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    A = [int(a) for a in input().split()]
    C = [0]*10**5
    for i in range(N):
        C[A[i]] += 1
    ma = 0
    for i in range(1, len(C)-1):
        count = C[i-1] + C[i] + C[i+1]
        if count > ma:
            ma = count    
    print(ma)
if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import sys
input=sys.stdin.readline
#from collections import defaultdict
#d = defaultdict(int)
#import fractions
#import math
#import collections
#from collections import deque
#from bisect import bisect_left
#N = int(input())
#A = list(map(int,input().split()))
#S = list(input())
#S.remove("\n")
#N,M = map(int,input().split())
#S,T = map(str,input().split())
#A = [int(input()) for _ in range(N)]
#S = [input() for _ in range(N)]
#A = [list(map(int,input().split())) for _ in range(N)]
import collections
N = int(input())
A = list(map(int,input().split()))
Min = min(A)
Max = max(A)
d = {i:0 for i in range(Min,Max+1)}
for ii in A:
    d[ii] += 1
s = [0] * (Max-Min+1)
n = len(s)
s[0] = d[Min]
for k in range(1,n):
    s[k] = s[k-1] + d[Min+k]
ss = 0
if n <= 3:
    ss = s[n-1]
    print(ss)
else:
    mmax = s[2]
    for i in range(3,n):
        mmm = s[i] - s[i-3]
        mmax = max(mmm,mmax)
    print(mmax)

Submission status: Accepted

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

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

d={}

for i in range(n):
    if str(a[i]-1) in d:
        d[str(a[i]-1)]+=1
    else:
        d[str(a[i]-1)]=1
    if str(a[i]) in d:
        d[str(a[i])]+=1
    else:
        d[str(a[i])]=1
    if str(a[i]+1) in d:
        d[str(a[i]+1)]+=1
    else:
        d[str(a[i]+1)]=1

print(max(d.values()))

Submission status: Accepted

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

Submission status: Accepted

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

count = defaultdict(int)

for a in a_list:
    if a > 0:
        count[a-1] += 1
    count[a] += 1
    count[a+1] += 1

print(max(count.values()))

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
from collections import Counter
n = int(input())
a = list(map(int, input().split()))
b = Counter(a)
count = 1
for i in range(min(a)-1, max(a)+1):
    count = max(count, b.get(i-1,0)+b.get(i, 0)+b.get(i+1, 0))
print(count)

Submission status: Accepted

Solution code:
n=int(input());A = list(map(int,input().split(' ')))
x = [0]*100001
for a in A:
    if a != 0:
        x[a-1]+=1;x[a]+=1;x[a+1]+=1
    else:
        x[a]+=1;x[a+1]+=1
print(max(x))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import Counter
n,*a = map(int,open(0).read().split())
c= Counter(a)
print(max([c[i]+c[i+1]+c[i+2] for i in range(min(a),max(max(a)-1,min(a)+1))]))

Submission status: Accepted

Solution code:
import collections

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

c = collections.Counter(A)
ans = 0
for i in range(min(A), max(A)+1):
    ans = max(c[i-1] + c[i] + c[i+1], ans)

print(ans)

Submission status: Accepted

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

def cnt(a):
    ans.setdefault(a,0)
    ans[a]+=1

ans={}
for a in an:
    cnt(a-1)
    cnt(a)
    cnt(a+1)

print(max(ans.values()))

Submission status: Accepted

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

i_a = list(map(lambda x: x+1, a))
d_a = list(map(lambda x: x-1, a))

new_a = a + i_a + d_a
c = collections.Counter(new_a)

sorted_c = sorted(c.values())
print(sorted_c[-1])

Submission status: Accepted

Solution code:

import sys  
import numpy as np
input = sys.stdin.readline
N = int(input())
a = list(map(int, input().split()))
k = [0] * max(3,(max(a)+1))

for p in a:
    k[p] += 1
ans = 0
for q,r,s in zip(k, k[1:] , k[2:]):
    ans = max(ans, q+r+s)

print(ans)

Submission status: Accepted

Solution code:
import collections

n=int(input())
a=list(map(int,input().split()))
a_s=sorted(a)
a_s_p=[]
a_s_m=[]
for i in range(n):
    a_s_p.append(a_s[i]+1)
    a_s_m.append(a_s[i]-1)

all_a=a_s+a_s_p+a_s_m

mode=collections.Counter(all_a).most_common()[0][1]
print(mode)

Submission status: Accepted

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

Submission status: Accepted

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


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

print(max(count.values()))

Submission status: Accepted

Solution code:
import collections
n=int(input())
a=list(map(int,input().split()))
a=collections.Counter(a)
ans=a[0]+a[1]

for i in range(max(a)+1):
    ans=max(ans,a[i]+a[i+1]+a[i+2])

print(ans)

Submission status: Accepted

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

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

print(max(p))

Submission status: Accepted

Solution code:
n, *A = map(int, open(0).read().split())
B = [0] * max(3, (max(A) + 1))
for a in A:
  B[a] += 1
ans = 0
for b0, b1, b2 in zip(B, B[1:], B[2:]):
  ans = max(ans, b0 + b1 + b2)
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int,input().split()))
d = {}
for ai in a:
  for x in [-1,0,1]:
    if ai+x in d:
      d[ai+x] += 1
    else:
      d[ai+x] = 1
d = sorted(d.items(),key = lambda x:x[1], reverse = True)
print(d[0][1])

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import Counter

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

l = Counter(A)
count = [0] * (10**5 + 1)
for c in l.items():
  count[c[0]] = c[1]

ans = 0
for i in range(10**5-1):
  tmp = count[i] + count[i+1] + count[i+2]
  if tmp > ans:
    ans = tmp
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

cnt = [0 for _ in range(100005)]
for a in A:
    cnt[a] += 1

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

print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict

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

for i in a:
    d[i] += 1
    d[i-1] += 1
    d[i+1] += 1
d = sorted(d.items(), key=lambda x:x[1], reverse=True)
print(d[0][1])

Submission status: Accepted

Solution code:
from sys import stdin
import collections

lines = stdin.readlines()
n = int(lines[0])
a_s = [int(x) for x in lines[1].rstrip().split()]


def inc(x): return x + 1


def dec(x): return x - 1


a_s_p = [inc(x) for x in a_s]
a_s_n = [dec(x) for x in a_s]

a_s = a_s + a_s_p + a_s_n
c = collections.Counter(a_s)
print(max(list(c.values())))

Submission status: Accepted

Solution code:
import sys

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

def main():
    m = 10 ** 5
    cnt = [0] * (m + 1)
    for x in a:
        cnt[x] += 1
        if x > 0: cnt[x-1] += 1
        if x < m: cnt[x+1] += 1
    print(max(cnt))

if __name__ ==  '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

r = [0] * (10**5+2)
for i in range(N):
    r[a[i]] += 1
    r[a[i]+1] += 1
    r[a[i]+2] += 1

print(max(r))

Submission status: Accepted

Solution code:
import bisect
N = int(input())
A = sorted(list(map(int,input().split())))
ans = 1

for i in range(A[-1]+1):
    ans = max(ans,bisect.bisect_right(A,i+1) - bisect.bisect_left(A,i-1))
print(ans)

Submission status: Accepted

Solution code:
import bisect
N = int(input())
A = sorted(list(map(int,input().split())))
ans = 1

def search(A,a,b):
    import bisect
    ans = bisect.bisect_right(A,b) - bisect.bisect_left(A,a)
    return ans

for i in range(A[-1]+1):
    ans = max(ans,search(A,i-1,i+1))
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter
N = int(input())
A = list(map(int,input().split()))
ac = Counter(A)
ans = 0
for i in range(max(A)+1):
    now = 0
    if i-1 in ac:
        now += ac[i-1]
    if i in ac:
        now += ac[i]
    if i+1 in ac:
        now += ac[i+1]
    ans = max(ans,now)
print(ans)

Submission status: Accepted

Solution code:
from __future__ import print_function

import sys

import re
import array
import copy
import functools
import operator

import math
import string
import fractions
from fractions import Fraction
from fractions import gcd
# fractions.gcd() を用いること
# math.log2()はatcoderでは対応していない．留意せよ．

import collections
import itertools
import bisect

import random
import time

import heapq
from heapq import heappush
from heapq import heappop
from heapq import heappushpop
from heapq import heapify
from heapq import heapreplace
from queue import PriorityQueue as pq
from queue import Queue

from itertools import accumulate

from collections import deque
from collections import Counter

from operator import mul
from functools import reduce

input = sys.stdin.readline


def eprint(*args, **kwargs):
    print(*args, file=sys.stderr, **kwargs)
    return


def lcm(n, m):
    return int(n * m / gcd(n, m))


def coprimize(p, q):
    common = fractions.gcd(p, q)
    return (p // common, q // common)


def combinations_count(n, r):
    r = min(r, n - r)
    numer = reduce(mul, range(n, n - r, -1), 1)
    denom = reduce(mul, range(1, r + 1), 1)
    return numer // denom


def find_gcd(list_l):
    x = reduce(gcd, list_l)
    return x


def main():
    n = int(input().strip())
    a = list(map(int, input().strip().split()))
    C = Counter()
    ans = 0
    for i in range(n):
        C[a[i]] += 1
        C[a[i]+1] += 1
        C[a[i]-1] += 1
        ans = max([C[a[i]], C[a[i]+1], C[a[i]-1], ans ])
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

ans = 0

if len(numbers) < 3:
    print(sum(numbers))
    exit()

for i in range(len(numbers)-2):
    ans = max(ans,sum(numbers[i:i+3]))

print(ans)

Submission status: Accepted

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

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

print(max(num))

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
C = {}
for a in A:
    for k in range(-1, 2, 1):
        if a + k in C:
            C[a+k] += 1
        else:
            C[a+k] = 1
print(max(C.values()))

Submission status: Accepted

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

# -1, 0, 右端も用意
updown_ls = [0] * (10**5 + 3)

for i in range(n):
    left = a_ls[i] 
    right = a_ls[i] + 3
    updown_ls[left] += 1
    updown_ls[right] -= 1

updown_ls.pop()
num_ls = list(np.cumsum(updown_ls))
print(max(num_ls))

Submission status: Accepted

Solution code:
#
# Written by NoKnowledgeGG @YlePhan
# ('ω')
#
#import math
#mod = 10**9+7
#import itertools
#import fractions
#import numpy as np
#mod = 10**4 + 7
"""def kiri(n,m):
  r_ = n / m
  if (r_ - (n // m)) > 0:
    return (n//m) + 1
  else:
    return (n//m)"""

""" n! mod m 階乗
mod = 1e9 + 7
N = 10000000
fac = [0] * N
def ini():
  fac[0] = 1 % mod
  for i in range(1,N):
    fac[i] = fac[i-1] * i % mod"""

"""mod = 1e9+7
N = 10000000
pw = [0] * N
def ini(c):
  pw[0] = 1 % mod
  for i in range(1,N):
    pw[i] = pw[i-1] * c % mod"""

"""
def YEILD():
  yield 'one'
  yield 'two'
  yield 'three'
generator = YEILD()
print(next(generator))
print(next(generator))
print(next(generator))
"""
"""def gcd_(a,b):
  if b == 0:#結局はc,0の最大公約数はcなのに
    return a
  return gcd_(a,a % b) # a = p * b + q"""
"""def extgcd(a,b,x,y):
  d = a
  if b!=0:
    d = extgcd(b,a%b,y,x)
    y -= (a//b) * x
    print(x,y)
  else:
    x = 1
    y = 0
  return d"""
# 最大公約数 と 最小公倍数
"""def gcd(a,b):
  if b == 0:
    return a
  return gcd(b, a % b)
def lcm(a,b):
  g = gcd(a,b)
  return (a * b) // g"""

def readInts():
  return list(map(int,input().split()))
mod = 10**9 + 7

def main():
  n = int(input())
  LIST = readInts()
  MAX_ = [0] * (10**5 + 10) # 始めに確保しておく
  for a in LIST:
    for i in range(3):
      MAX_[a+i]+=1 # -1,0,1のように、0,1,2で被るものを考える。
  print(max(MAX_))
if __name__ == '__main__':
  main()

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    a = map(int, input().split())
    d = {i: 0 for i in range(-1, 100001)}

    for i in a:
        d[i-1] += 1
        d[i] += 1
        d[i+1] += 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]*pow(10,6)
for a in A:
    B[a]+=1
    B[a+1]+=1
    B[a+2]+=1
print(max(B))

Submission status: Accepted

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

ans = [0 for i in range(2*10**5+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())
list_N = list(map(int, input().split()))
ans = [0]*2*pow(10, 5)
for num in list_N:
    ans[num-1] += 1
    ans[num] += 1
    ans[num+1] += 1

print(max(ans))

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int,input().split()))
a1 = [i+1 for i in a]
a2 = [i-1 for i in a]
a3 = a+a1+a2
d = {}
for i in range(3*n):
  if a3[i] in d:
    d[a3[i]] += 1
  else:
    d[a3[i]] = 1
print(max(d.values()))

Submission status: Accepted

Solution code:
from collections import Counter
n = int(input())
a = list(map(int,input().split()))
a1 = [i+1 for i in a]
a2 = [i-1 for i in a]
a3 = Counter(a+a1+a2)
print(max(a3.values()))

Submission status: Accepted

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

l = [0]*(10**5+10)
for i in range(n):
    l[a[i]] += 1
    
s = 0
for j in range(1, len(l)-1):
    if  s < l[j-1] + l[j] + l[j+1]:
        s = l[j-1] + l[j] + l[j+1]
        
print(s)

Submission status: Accepted

Solution code:
if __name__ == '__main__':
    n = int(input())
    a = list(map(int, input().split()))
    h = [0] * (max(a) + 2)
    minus = 0
    for i in a:
        h[i] += 1
        h[i + 1] += 1
        if i == 0:
            minus += 1
        else:
            h[i - 1] += 1

    print(max(minus, max(h)))

Submission status: Accepted

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

print(max(cnt))

Submission status: Accepted

Solution code:
import collections
def solve(n, a):
    a_list = [int(i) for i in a.split()]
    count = collections.Counter()

    for i in a_list:
        count[i] += 1
        count[i+1] += 1
        count[i-1] += 1

    return max(count.values())

if __name__ == "__main__":
    n = int(input())
    a = input()
    print(solve(n, a))

Submission status: Accepted

Solution code:
from collections import Counter

N = int(input())
a = list(map(int, input().split()))
c = Counter(a)
ans = 0
for k, v in c.items():
    tmp = v + c.get(k - 1, 0) + c.get(k + 1, 0)
    if tmp > ans:
        ans = tmp

print(ans)

Submission status: Accepted

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

for num in a:
    if num != 0:
        b[num - 1] += 1
    b[num] += 1
    b[num + 1] += 1

print(max(b))

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int,input().split()))
cnt = [0] * ((10**5) + 1)
for i in range(n):
         cnt[a[i]] += 1
sumcnt = [0]
for i in range(len(cnt)):
         sumcnt.append(sumcnt[i]+cnt[i])
ans = 0
for i in range(len(sumcnt) - 3):
         if ans < sumcnt[i+3] - sumcnt[i]:
                  ans = sumcnt[i+3] - sumcnt[i]
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
ary = list(map(int,input().split()))
counter = [0]*100000

for i in ary:
  counter[i] += 1
  
sum = 0
max = 0
for i in range(100000):
  if i == 0:
    sum = counter[i] + counter[i+1]
  elif i == 1:
    sum += counter[i+1]
  elif i == 99999:
    sum += -counter[i-2] 
  else:
    sum += counter[i+1] - counter[i-2]
  if sum > max:
    max = sum
print(max)

Submission status: Accepted

Solution code:
import sys
import heapq, math
from itertools import zip_longest, permutations, combinations, combinations_with_replacement
from itertools import accumulate, dropwhile, takewhile, groupby
from functools import lru_cache
from copy import deepcopy

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

cnt = [0] * 100010
for a in A:
    if a > 0:
        cnt[a - 1] += 1
    cnt[a] += 1
    cnt[a + 1] += 1

print(max(cnt))

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int,input().split()))
b = {}
for i in a:
    if i in b.keys():
        b[i] += 1
    else:
        b[i] = 1
c = sorted(b.items())
m = len(c)
if m == 1:
    print(c[0][1])
elif m == 2:
    if c[1][0] - c[0][0] < 3:
        print(c[0][1] + c[1][1])
    else:
        print(max(c[0][1],c[1][1]))
else:
    s = 0
    for i in range(m-2):
        t = c[i][1]
        if c[i+2][0] - c[i][0] < 3:
            t += (c[i+1][1] + c[i+2][1])
        elif  c[i+1][0] - c[i][0] < 3:
            t += c[i+1][1]
        if t > s:
            s = t
    print(s)

Submission status: Accepted

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

def calculate(n,arr):

    dict = {}

    for ar in arr:
        a1 = ar - 1
        a2 = ar
        a3 = ar + 1

        if dict.get(a1) == None:
            dict.__setitem__(a1,1)
        else:
            dict.__setitem__(a1,dict.get(a1)+1)

        if dict.get(a2) == None:
            dict.__setitem__(a2, 1)
        else:
            dict.__setitem__(a2, dict.get(a2) + 1)

        if dict.get(a3) == None:
            dict.__setitem__(a3, 1)
        else:
            dict.__setitem__(a3, dict.get(a3) + 1)


    print(max(dict.values()))
calculate(N,ARR)

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
    cnt[i+1]+=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**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:
from collections import Counter

n = int(input())
a = map(int, input().split())
c = Counter(a)

ans = max(c[k]+c[k+1]+c[k-1] for k in c.keys())
print(ans)

Submission status: Accepted

Solution code:
import collections
n = int(input())
a = list(map(int,input().split()))
maxVal = max(a)
minVal = min(a)
a = collections.Counter(a)
ans = 0
for i in range(max(0, minVal-1), maxVal+1):
  temp = a[i - 1] + a[i] + a[i + 1]
  ans = max(ans, temp)
print(ans)

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
nmax=10**5+1
n=int(input())
a=list(map(int,input().split()))

if n<=1:
    print(n)
else:
    c=[0]*nmax

    for i in range(n):
        c[a[i]]=c[a[i]]+1
        
    sum3=c[0]+c[1]+c[2]
    sum3max=sum3
    for i in range(1,nmax-2):
        sum3=c[i]+c[i+1]+c[i+2]
        sum3max=max(sum3max,sum3)
    print(sum3max)

Submission status: Accepted

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

Submission status: Accepted

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

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

import collections
L=collections.Counter(li)
print(max(L.values()))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import Counter


N = int(input())
C = Counter()

for a in map(int, input().split()):
    C[a] += 1
    C[a - 1] += 1
    C[a + 1] += 1

print(max(C.values()))

Submission status: Accepted

Solution code:
N = int(input())
a_list = list(map(int, input().split()))
max_a = max(a_list)
if max_a==0:
    print(N)
    exit()
ans = [0]*(max_a+1)
for i in a_list:
    if i==0:
        ans[0] += 1
        ans[1] += 1
    elif i==max_a:
        ans[-1] += 1
        ans[-2] += 1
    else:
        ans[i-1] += 1
        ans[i] += 1
        ans[i+1] += 1
print(max(ans))

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    a = list(map(int, input().split()))
    mx = max(a)
    ans = [0] * (mx + 2)
    for i, aa in enumerate(a):
        if aa != 0:
            ans[aa - 1] += 1
        ans[aa] += 1
        ans[aa + 1] += 1
    print(max(ans))

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
ans=[]
for i in a:
  ans+=(i-1),
  ans+=(i),
  ans+=(i+1),
  
from collections import*

c=Counter(ans)

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