Problem:


Score : 300 points

Problem Statement

You are given an integer sequence of length N, a_1,a_2,...,a_N.

For each 1≤i≤N, you have three choices: add 1 to a_i, subtract 1 from a_i or do nothing.

After these operations, you select an integer X and count the number of i such that a_i=X.

Maximize this count by making optimal choices.

Constraints

1≤N≤10^5

0≤a_i<10^5 (1≤i≤N)

a_i is an integer.

Input

The input is given from Standard Input in the following format:

N
a_1 a_2 .. a_N

Output

Print the maximum possible number of i such that a_i=X.

Sample Input 1

7
3 1 4 1 5 9 2

Sample Output 1

4

For example, turn the sequence into 2,2,3,2,6,9,2 and select X=2 to obtain 4, the maximum possible count.

Sample Input 2

10
0 1 2 3 4 5 6 7 8 9

Sample Output 2

3

Sample Input 3

1
99999

Sample Output 3

1



Sample input:
7
3 1 4 1 5 9 2


Sample output:
4

Submission status: Accepted

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

num = [0]*(10**5+1)
for i in a:
    if i-1 >= 0:
        num[i-1] += 1
    num[i] += 1
    if i+1 <= N:
        num[i+1] += 1

print(max(num))

Submission status: Accepted

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

l=[0]*(10**5+5)
for i in a:
    l[i]+=1
p=0
for j in range(10**5):
    p=max(p,l[j]+l[j+1]+l[j+2])
print(p)

Submission status: Accepted

Solution code:
n=int(input())
b=[0 for i in range(100002)]
a=list(map(int,input().split()))
a.sort()
for i in range(n):
    b[a[i]]+=1
ans=0
for i in range(1,100001):
    tmp=b[i-1]+b[i]+b[i+1]
    if tmp>ans:
        ans=tmp
print(ans)

Submission status: Accepted

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

print(max(cnt))

Submission status: Accepted

Solution code:
import sys
import collections

input_methods=['clipboard','file','key']
using_method=0
input_method=input_methods[using_method]

tin=lambda : map(int, input().split())
lin=lambda : list(tin())
mod=1000000007

#+++++

def main():
	a = int(input())
	#b , c = tin()
	#s = input()
	al=lin()
	bl=[v+1 for v in al]
	cl=[v-1 for v in al]
	zl = al+bl+cl
	cc = collections.Counter(zl)
	ret = max(cc.values())
	print(ret)
	
	
	
#+++++
isTest=False

def pa(v):
	if isTest:
		print(v)
		
def input_clipboard():
	import clipboard
	input_text=clipboard.get()
	input_l=input_text.splitlines()
	for l in input_l:
		yield l

if __name__ == "__main__":
	if sys.platform =='ios':
		if input_method==input_methods[0]:
			ic=input_clipboard()
			input = lambda : ic.__next__()
		elif input_method==input_methods[1]:
			sys.stdin=open('inputFile.txt')
		else:
			pass
		isTest=True
	else:
		pass
		#input = sys.stdin.readline
			
	ret = main()
	if ret is not None:
		print(ret)

Submission status: Accepted

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

Submission status: Accepted

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

MinatoYukina = [0]*100003

for i in range (0, N):
	MinatoYukina[L[i]]+=1
	MinatoYukina[L[i]+1]+=1
	MinatoYukina[L[i]+2]+=1

print(max(MinatoYukina))

Submission status: Accepted

Solution code:
import collections

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

l = [None] * n * 3
for i in range(n):
    l[i] = a[i]
    l[n+i] = a[i]-1
    l[2*n+i] = a[i]+1
    
c = collections.Counter(l)
    
print(c.most_common()[0][1])

Submission status: Accepted

Solution code:
import numpy as np
n = int(input())
a = np.array([int(i) for i in input().split()])
x = np.hstack([a - 1, a, a + 1])
x[x < 0] = 0
r = np.bincount(x)
print(r[1:].max())

Submission status: Accepted

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

#遅延セグ木で解く
class segtree:
    x_unit=0 # 要素として入れられる最大値
    def __init__(self,N):
        self.n=(1<<len(bin(N-1)[2:]))
        self.x=[self.x_unit]*(2*self.n)

    def build(self, seq):
        for i,j in enumerate(seq, start=self.n):  # n番目からseqをx配列に移していく
            self.x[i] = j
        for i in range(self.n-1, 0, -1):
            self.x[i] = 0
            # セグ木トーナメント表の左右を比較
            # 例えばn=8の時、seq[4]はx[12]に、seq[5]はx[13]に入っている
            # さらにx[14]とx[15]の間に来るのはx[7]、x[12]とx[13]の間に来るのはx[6]

    def fold(self,l,r): # 区間[l, r)値に+1 
        l+=self.n
        r+=self.n-1
        while l<r:
            if l%2==1: # lが奇数
                self.x[l]+=1
                l+=1 # 偶数に調節
            if r%2==0: # rが偶数
                self.x[r]+=1
                r-=1 # 開区間なので1個前は奇数番目の要素
            l //= 2
            r //= 2
        if l==r:
            self.x[l]+=1
        #print(self.x)
    def get(self,i): # seq[i]に対する値を取得 
        i+=self.n
        ret=self.x[i]
        while i>1:
            i //= 2
            ret+=self.x[i]
        return ret

seq=[0]*(10**5+2)
q=[(max(0, a[i]-1), min(10**5,a[i]+1)) for i in range(n)]
seg=segtree(10**5+2)
seg.build(seq)
for i in range(n):
    l,r=q[i]
    seg.fold(l,r+1)
ans=[]
for i in range(10**5+2):
    ans.append(seg.get(i))
print(max(ans))

Submission status: Accepted

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

seq=[0]*(10**5+3)
for i in range(n):
    l=max(a[i]-1,0)
    r=a[i]+2
    seq[l]+=1
    seq[r]-=1
from itertools import accumulate
seq=list(accumulate(seq))
print(max(seq))

Submission status: Accepted

Solution code:
from collections import Counter

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

min_v=min(a)
max_v=max(a)

c=Counter(a)

ans=0
for i in range(min_v,max_v+1):
  t_ans=c[i-1]+c[i]+c[i+1]
  ans=max(ans,t_ans)

print(ans)

Submission status: Accepted

Solution code:
import collections

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

CNT_A = collections.Counter(A_s).most_common()

ans_list = []

if (len(CNT_A) >= 3):
    CNT_A.sort()

    for i in range(len(CNT_A) - 2):
        val = CNT_A[i][0]
        next_val = CNT_A[i + 1][0]
        nnnn_val = CNT_A[i + 2][0]
        
        
        if (abs(val - next_val) <= 2):
            cnt = CNT_A[i][1] + CNT_A[i + 1][1]
            if (abs(val - nnnn_val) <= 2):
                cnt += CNT_A[i + 2][1]
                
        else:
            cnt = CNT_A[i][1]
        
        ans_list.append(cnt)
    
    ans = max(ans_list)

elif (len(CNT_A) == 2):
    val = CNT_A[0][0]
    n_val = CNT_A[1][0]
    
    if (abs(val - n_val) <= 2):
        ans = CNT_A[0][1] + CNT_A[1][1]
    else:
        ans = CNT_A[0][1]
    
else:
    ans=CNT_A[0][1]

print(ans)

Submission status: Accepted

Solution code:
from collections import Counter


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

nc = Counter(A)
C = []
for i in range(max(A) + 1):
    c = nc[i - 1] + nc[i] + nc[i + 1]
    C.append(c)

print(max(C))

Submission status: Accepted

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

Submission status: Accepted

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

for i in range(N):
    counter[a[i]] += 1
    counter[a[i]+1] += 1
    counter[a[i]+2] += 1

print(max(counter))

Submission status: Accepted

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

b=map(lambda x: x+1, a)
c=map(lambda x: x-1, a)
b=list(b)
c=list(c)

for num in range(len(a)):
    a.append(b[num])
    a.append(c[num])

d=collections.Counter(a)
values,counts=zip(*d.most_common())
print(counts[0])

Submission status: Accepted

Solution code:
import collections

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

aa=[]

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

Submission status: Accepted

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

l=[0 for i in range(10**5+2)]

for i in range(n):
    l[a[i]-1]+=1
    l[a[i]]+=1
    l[a[i]+1]+=1
print(max(l))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from sys import stdin, setrecursionlimit
from collections import defaultdict


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

    ans = 0

    dic = defaultdict(int)
    for ai in a:
        dic[ai - 1] += 1
        dic[ai] += 1
        dic[ai + 1] += 1
        ans = max(ans, dic[ai - 1], dic[ai], dic[ai + 1])

    print(ans)


if __name__ == "__main__":
    setrecursionlimit(10000)
    main()

Submission status: Accepted

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

Submission status: Accepted

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

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


if __name__ == '__main__':
    main()

Submission status: Accepted

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

    res_list = [0] * 100001

    for num in A:
        res_list[num] += 1
        
        if num != 0:
            res_list[num-1] += 1
        if num != 10**5:
            res_list[num+1] += 1
    
    print(max(res_list))

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    a = list(map(int, input().split()))
    num_dict = {}
    a.sort()
    a_max = a[N-1]
    temp = a[0]
    sum = 1

    for i in range(1,N):
        if temp == a[i]:
            sum += 1
        else:
            num_dict[temp] = sum
            sum = 1
            temp = a[i]
    num_dict[a[N-1]] = sum

    ans = 0
    judge = 0
    for j in range(1, a_max):
        #j=1のときは特別
        if j == 1 and j in num_dict:
            if j+1 in num_dict:
                judge = num_dict[j] + num_dict[j+1]
                ans = judge
                continue
            else:
                judge = num_dict[j]
                ans = judge
                continue

        if (j in num_dict) and (j-1 in num_dict) and (j+1 in num_dict):
            if judge <= (num_dict[j-1] + num_dict[j] + num_dict[j+1]):
                judge = num_dict[j-1] + num_dict[j] + num_dict[j+1]
                ans = judge
                continue
            else:
                continue

        elif (j in num_dict) and (j-1 in num_dict):
            if judge <= (num_dict[j-1] + num_dict[j]):
                judge = num_dict[j-1] + num_dict[j]
                ans = judge
                continue
            else:
                continue

        elif (j in num_dict) and (j+1 in num_dict):
            if judge <= (num_dict[j+1] + num_dict[j]):
                judge = num_dict[j+1] + num_dict[j]
                ans = judge
                continue
            else:
                continue

        elif (j-1 in num_dict) and (j+1 in num_dict):
            if judge <= (num_dict[j+1] + num_dict[j-1]):
                judge = num_dict[j+1] + num_dict[j-1]
                ans = judge
                continue
            else:
                continue

        elif j in num_dict:
            if judge <= (num_dict[j]):
                judge = num_dict[j]
                ans = judge
                continue
            else:
                continue

        else:
            continue

    if a_max-1 in num_dict:
        if judge <= num_dict[a_max-1] + num_dict[a_max]:
            ans = num_dict[a_max-1] + num_dict[a_max]
    else:
        if judge <= num_dict[a_max]:
            ans = num_dict[a_max]
    return ans

print(main())

Submission status: Accepted

Solution code:
from collections import Counter

N = int(input())
a = list(map(int,input().split()))
c = Counter(a)
max_num = 0

for x in range(max(a)+1):
      max_num = max(max_num,(c[x] + c[x+1] + c[x+2]))
print(max_num)

Submission status: Accepted

Solution code:
from collections import Counter

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

c=Counter(a)

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

print(ans)

Submission status: Accepted

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

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

for i in a:
    if i == 1:
        point[1] += 1
        point[2] += 1
    else:
        point[i] += 1
        point[i-1] += 1
        point[i+1] += 1

print(max(point))

Submission status: Accepted

Solution code:
from collections import defaultdict
d = defaultdict(int)
n = int(input())
a = list(map(int, input().split()))
for e in a:
  d[e + 1] += 1
  d[e] += 1
  d[e - 1] += 1
d = dict(d)
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()))

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

for aa in a:
    ans[aa] += 1
    ans[aa+1] += 1
    ans[aa+2] += 1

# prt = 0
# for i,v in enumerate(ans):
#     if v > prt:

print(max(ans))

Submission status: Accepted

Solution code:
from collections import defaultdict
n = int(input())
a = list(map(int,input().split()))
store = defaultdict(lambda:0)
for i in a:
  store[i+1]+=1
  store[i-1]+=1
  store[i]+=1
print(max(store.values()))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math
import string
import collections
from collections import Counter
from collections import deque
from decimal import Decimal
import sys
import fractions


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


def nCr(n, r):
    return math.factorial(n)//(math.factorial(n-r)*math.factorial(r))


def has_duplicates2(seq):
    seen = []
    for item in seq:
        if not(item in seen):
            seen.append(item)
    return len(seq) != len(seen)


def divisor(n):
    divisor = []
    for i in range(1, n+1):
        if n % i == 0:
            divisor.append(i)
    return divisor


# coordinates
dx = [-1, -1, -1, 0, 0, 1, 1, 1]
dy = [-1, 0, 1, -1, 1, -1, 0, 1]
n = int(input())
a = readints()
# print(a)
x = [0]*(10**5+10)
# print(x)
for i in range(n):
    x[a[i]-1] += 1
    x[a[i]] += 1
    x[a[i]+1] += 1
max_x = max(x)
print(max_x)

Submission status: Accepted

Solution code:
# yorikatu_58_b.py

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 itertools import accumulate
n = int(input())
a = list(map(int, input().split()))
a = [i+1 for i in a]

imos = [0] * (10**5 + 3)

for i in a:
    imos[i-1] += 1
    imos[i+2] -= 1

i = accumulate(imos)
print(max(i))

Submission status: Accepted

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

cnt = [0] * (max(a) + 2)

for i in range(n):
    if a[i] != 0:
        cnt[a[i]-1] += 1
    cnt[a[i]] += 1
    cnt[a[i]+1] += 1
ans = 0
for c in cnt:
    ans = max(ans, c)

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
a = list(map(int, input().split()))
a.sort()
oldi = 0
m = a[0]
M = 0
ans = 0
if N == 1:
  ans = 1
for i in range(1, N):
  M = a[i]
  while M - m > 2:
    oldi += 1
    m = a[oldi]
  if i - oldi + 1 > ans:
    ans = i - oldi + 1
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split()))
MAX = max(A)
dic = {}
for i in range(N):
  if A[i] not in dic:
    dic[A[i]] = 1
  else:
    dic[A[i]] += 1

ans = []
for i in range(MAX+1):
  mae = 0;ima=0;tsugi = 0
  if i in dic:
    ima = dic[i]
  if i-1 in dic:
    mae = dic[i-1]
  if i+1 in dic:
    tsugi = dic[i+1]
  temp = mae + ima + tsugi
  ans.append(temp)
print(max(ans))

Submission status: Accepted

Solution code:
from bisect import bisect_left, bisect_right

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

ans = 1
for i in range(N):
    X = a[i]
    res = bisect_right(a, X+1) - bisect_left(a, X-1)
    ans = max(res, ans)

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def resolve():
  n = int(input())
  a = list(map(int, input().split()))
  c = [0] * (max(a) + 3)
  for x in a:
    x += 1
    c[x-1] += 1
    c[x] += 1
    c[x+1] += 1
  ans = max(c)

  print(ans)
  return

if __name__ == "__main__":
  resolve()

Submission status: Accepted

Solution code:
import sys
from collections import Counter

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


def resolve():
    n = int(input())
    A = list(map(int, input().split()))
    B = []
    for a in A:
        B.append(a - 1)
        B.append(a)
        B.append(a + 1)
    D = Counter(B)
    res = max([v for v in D.values()])
    print(res)


if __name__ == '__main__':
    resolve()

Submission status: Accepted

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

cnt = {}
for i in range(n):
    if a[i] not in cnt:
        cnt[a[i]] = 0
    cnt[a[i]] += 1
    
ans = 0
for i in range(n):
    tmp = cnt[a[i]]
    if a[i] - 1 in cnt:
        tmp += cnt[a[i] - 1]
    if a[i] + 1 in cnt:
        tmp += cnt[a[i] + 1]
    ans = max(ans, tmp)
print(ans)

Submission status: Accepted

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

maxX = 0
maxV = 0

for i in range(10**5+1):
    val = count[i-1] + count[i] + count[i+1]
    if val > maxV:
        maxV = val
        maxX = i
print(maxV)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import Counter

n = int(input())
a = list(map(int, input().split()))
sa =set(a)
ca = Counter(a)
ans = 0

for x in sa:
    # カウンタオブジェクトは存在しない要素に０を返す
    tmp = ca[x-1] + ca[x] + ca[x+1]
    if ans < tmp:
        ans = tmp

print(ans)

Submission status: Accepted

Solution code:
from collections import Counter as C
n=int(input())
a=list(map(int,input().split()))
a.sort()
aset=list(set(a))
ca = C(a)
ans=0
for i in aset:
    if ans<ca[i-1]+ca[i]+ca[i+1]:
        ans=ca[i-1]+ca[i]+ca[i+1]
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import copy
import collections
N = int(input())
seq = [int(i) for i in input().split()]
seqP1 = [i + 1 for i in copy.copy(seq)]
seqM1 = [i - 1 for i in copy.copy(seq)]
mixed = seq + seqM1 + seqP1
print(mixed.count(collections.Counter(mixed).most_common()[0][0]))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
*a, = map(int,input().split())
c = [0 for _ in range(10**5+1)]
for A in a:
    c[A] += 1
ans = 0
l = len(c)
for i in range(l):
    z = c[i]

    if 0 <= i - 1 <l:
        z += c[i-1]

    if 0 <= i+1 <l:
        z += c[i+1]

    ans = max(ans, z)
print(ans)

Submission status: Accepted

Solution code:
N=int(input())
*A,=map(int,input().split())
A.sort()
i_min=A[0]
i_max=A[-1]

import collections
C=collections.Counter(A)
count=[]
i=i_min
while i_min<=i<=i_max:
    count.append(C[i]+C[i-1]+C[i+1])
    i+=1

print(max(count))

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
if max(a)<3:
    ans=sum(num)
else:
    for i in range(1,max(a)):
        num2=num[i-1]+num[i]+num[i+1]
        if ans<num2:
            ans=num2
print(ans)

Submission status: Accepted

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

for i in range(n):
  base[a[i]] += 1
  base[a[i]+1] += 1
  base[a[i]+2] += 1
  
print(int(max(base)))

Submission status: Accepted

Solution code:
from collections import Counter

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

counter = Counter(A)

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

print(max(counter.values()))

Submission status: Accepted

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

## Library

import sys
from fractions import gcd
import math
from math import ceil,floor
import collections
from collections import Counter

## input

# n=int(input())
# A,B,C,D=map(int, input().split())
# string = input()
# yoko = list(map(int, input().split()))
# tate = [int(input()) for _ in range(N)]
# N, M = map(int,input().split()) 
#    P = [list(map(int,input().split())) for i in range(M)]

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

## Logic

C = Counter(A)
X = 0
for i in range(10**5):
    X = max(X,C[i]+C[i+1]+C[i+2])
print(X)

Submission status: Accepted

Solution code:
def main():
  n = int(input())
  a = tuple(map(int ,input().split()))

  ans = [0]*(10**6)
  for x in a:
    ans[x-1] += 1
    ans[x] += 1
    ans[x+1] += 1
  print(max(ans))
if __name__ == '__main__':
  main()

Submission status: Accepted

Solution code:
from collections import defaultdict
N=int(input())
A=list(map(int,input().split()))
B=defaultdict(int)
for a in A:
    B[a-1]+=1
    B[a]+=1
    B[a+1]+=1
print(max(B.items(),key=lambda B:B[1])[1])

Submission status: Accepted

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

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

ans = max(count)
print(ans)

Submission status: Accepted

Solution code:
# For taking integer inputs.
import math


def inp():
    return(int(input()))


# For taking List inputs.
def inlist():
    return(list(map(int, input().split())))


# For taking string inputs. Actually it returns a List of Characters, instead of a string, which is easier to use in Python, because in Python, Strings are Immutable.
def instr():
    s = input()
    return(list(s[:len(s)]))


# For taking space seperated integer variable inputs.
def invr():
    return(map(int, input().split()))


n = inp()

a = inlist()
count = [0] * (100001)

for i in a:

    count[i] += 1
    if i > 0:
        count[i-1] += 1
    count[i+1] += 1

m = 0
ind = 0
print(max(count))

Submission status: Accepted

Solution code:
N=int(input())
an=sorted(list(map(int,input().split())))
dic={}

for ai in an:
  if ai not in dic:dic[ai]=1
  else:dic[ai]+=1
  pass

max_v=0
for x in range(an[0],an[-1]+1):
  v=0
  if x-1 in dic: v+=dic[x-1]
  if x   in dic: v+=dic[x]
  if x+1 in dic: v+=dic[x+1]
  max_v=max(max_v,v)
print(max_v)

Submission status: Accepted

Solution code:
N = input()
a = map(int, input().split())
count = {}
for v in a:
    for i in [v-1, v, v+1]:
        if i in count.keys():
            count[i] += 1
        else:
            count[i] = 1

print(max(count.values()))

Submission status: Accepted

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

s=list(range(a_min,a_max+1))
t=[0]*(a_max+1)

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

x=sum(t[:2]) #initialize
for j in range(1,len(t)-1):
  x=max(x,t[j-1]+t[j]+t[j+1])

print(x)

Submission status: Accepted

Solution code:
from collections import Counter

c = Counter()  
N = int(input())
for x in input().split():
    c[int(x)] += 1

A_uniq = sorted(c.keys())

def gen_candiate(seq):
    s = set()
    for n in seq:
        s.add(n-1)
        s.add(n)
        s.add(n+1)
    return sorted(s)

result = 0
for n in gen_candiate(A_uniq):
    tmp = c.get(n-1, 0) + c.get(n, 0) + c.get(n+1, 0)
    if tmp > result:
        result = tmp

print(result)

Submission status: Accepted

Solution code:
from collections import Counter as C

n  =  int(input())
a= list(input().split())
al = list([int(i) for i in a])
aset = set([int(i) for i in a])

cnt=0
ca = C(al)
#print(C(al)[0])
for i in aset:
  #print(i)
  #print(al.count(i-1),al.count(i),al.count(i+1))
  if cnt < ca[i-1] + ca[i] + ca[i+1]:
    cnt = ca[i-1] + ca[i] + ca[i+1]

print(cnt)

Submission status: Accepted

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

#for i in a:
#i=0
b=[0]*(10**5+10)
r=0
#while i<=n:
for i in a:
    b[i]+=1
    b[i+1]+=1
    b[i+2]+=1

print(max(b))

Submission status: Accepted

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

dic = collections.Counter(A)
for a in A:
    if (a-1 in dic) & (a+1 in dic): 
        dic[a-1] += 1
        dic[a+1] += 1

    elif (a+1 in dic):
        dic[a+1] += 1
        dic[a-1] = 1
    elif (a-1 in dic):
        dic[a-1] += 1
        dic[a+1] = 1
    else:
        dic[a+1] = 1
        dic[a-1] = 1

print(max(dic.values()))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import Counter

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

# print('C', c)

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

Submission status: Accepted

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

Submission status: Accepted

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

l={}
for i in range(n):
    if a[i] in l:
        l[a[i]]+=1
    else:
        l[a[i]]=1

ans=0

for i in l:
    cnt=l[i]
    if i-1 in l:
        cnt+=l[i-1]
    if i+1 in l:
        cnt+=l[i+1]
    ans=max(cnt,ans)

print(ans)

Submission status: Accepted

Solution code:
from collections import Counter

N = int(input())
a = list(map(int, input().split()))
c = dict(Counter(a))
cx = {i: 0 for i in range(10 ** 5 + 1) if i not in c.keys()}
c.update(cx)
c = sorted(c.items(), key=lambda x: x[0])
ans = 0
for i in range(10 ** 5 - 1):
  ans = max(sum((c[i][1], c[i+1][1], c[i+2][1])), ans)
  
print(ans)

Submission status: Accepted

Solution code:
import collections

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

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

c = collections.Counter(ans)

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

Submission status: Accepted

Solution code:
from collections import Counter

c = Counter()  

N = int(input())
for x in input().split():
    c[int(x)] += 1

A_uniq = sorted(c.keys())

def gen_candiate(seq):
    s = set()
    for n in seq:
        s.add(n-1)
        s.add(n)
        s.add(n+1)
    return sorted(s)

#print(gen_candiate(A_uniq))

result = 0
for n in gen_candiate(A_uniq):
    tmp = c.get(n-1, 0) + c.get(n, 0) + c.get(n+1, 0)
    if tmp > result:
        result = tmp

print(result)

Submission status: Accepted

Solution code:
from collections import Counter

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

a_all = []
for a in A:
  a_all.append(a)
  a_all.append(a - 1)
  a_all.append(a + 1)

a_counter = Counter(a_all)
print(a_counter.most_common()[0][1])

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from itertools import accumulate


N = int(input())
acc = [0] * (10 ** 5 + 3)
for a in map(int, input().split()):
    acc[max(0, a - 1)] += 1
    acc[a + 2] -= 1
acc = list(accumulate(acc))
print(max(acc))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import defaultdict

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

d = defaultdict(int)

m = -1

for a in A:
    d[a] += 1
    d[a+1] += 1
    d[a-1] += 1
    m = max(d[a], d[a+1], d[a-1], m)

print(m)

Submission status: Accepted

Solution code:
n = int(input())
A = list(map(int, input().split()))
from collections import Counter
C = Counter(A)
maxv = 0
for i in range(10 ** 5):
	maxv = max(maxv, C[i] + C[i+1] + C[i+2])
print(maxv)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import Counter as CC
N = int(input())
a = list(map(int, input().split()))
b = CC(a)
X1, X2, X3 = 0, b[min(b)], b[min(b)+1]
count = X1 + X2 + X3
ans = count
for i in range(min(b) + 2, max(b) + 1):
  count -= X1
  X1, X2, X3 = X2, X3, b[i]
  count += X3
  ans = max(ans, count)
else:
  print(ans)

Submission status: Accepted

Solution code:
import collections
N=int(input())
A=sorted(list(map(int,input().split())))
p=collections.Counter(A)
B=list(set(A))
ans=0
l=len(B)
if l==1:
  ans = p[B[0]]
elif l==2:
  if B[1]-B[0] <= 2:
    ans = p[B[0]]+p[B[1]]
  else:
    ans = max(p[B[0]],p[B[1]])
else:  
  for i in range(l-2):
    sum_a = p[B[i]]+p[B[i+1]]+p[B[i+2]]
    if sum_a > ans:
      ans = sum_a
print(ans)

Submission status: Accepted

Solution code:
import collections
N = int(input())
A = list(map(int,input().split()))
B = [i-1 for i in A]
C = [j+1 for j in A]
A += B + C
D = collections.Counter(A)
print(D.most_common()[0][1])

Submission status: Accepted

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

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

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

Submission status: Accepted

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

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)
c=collections.Counter(a)
print(c.most_common()[0][1])