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:
# coding: utf-8
import re
import math
from collections import defaultdict
from collections import deque
import itertools
from copy import deepcopy
import random
import time
import os
import queue
import sys
import datetime
from functools import lru_cache
#@lru_cache(maxsize=None)
readline=sys.stdin.readline
sys.setrecursionlimit(2000000)
#import numpy as np
alphabet="abcdefghijklmnopqrstuvwxyz"
mod=int(10**9+7)
inf=int(10**20)
def yn(b):
    if b:
        print("yes")
    else:
        print("no")
def Yn(b):
    if b:
        print("Yes")
    else:
        print("No")
def YN(b):
    if b:
        print("YES")
    else:
        print("NO")
class union_find():
    def __init__(self,n):
        self.n=n
        self.P=[a for a in range(N)]
        self.rank=[0]*n
 
    def find(self,x):
        if(x!=self.P[x]):self.P[x]=self.find(self.P[x])
        return self.P[x]
 
    def same(self,x,y):
        return self.find(x)==self.find(y)
 
    def link(self,x,y):
        if self.rank[x]<self.rank[y]:
            self.P[x]=y
        elif self.rank[y]<self.rank[x]:
            self.P[y]=x
        else:
            self.P[x]=y
            self.rank[y]+=1
 
    def unite(self,x,y):
        self.link(self.find(x),self.find(y))
 
    def size(self):
        S=set()
        for a in range(self.n):
            S.add(self.find(a))
        return len(S)
def is_pow(a,b):#aはbの累乗数か
    now=b
    while now<a:
        now*=b
    if now==a:return True
    else:return False
def bin_(num,size):
    A=[0]*size
    for a in range(size):
        if (num>>(size-a-1))&1==1:
            A[a]=1
        else:
            A[a]=0
    return A
def get_facs(n,mod_=0):
    A=[1]*(n+1)
    for a in range(2,len(A)):
        A[a]=A[a-1]*a
        if(mod_>0):A[a]%=mod_
    return A
def comb(n,r,mod,fac):
    if(n-r<0):return 0
    return (fac[n]*pow(fac[n-r],mod-2,mod)*pow(fac[r],mod-2,mod))%mod
def next_comb(num,size):
    x=num&(-num)
    y=num+x
    z=num&(~y)
    z//=x
    z=z>>1
    num=(y|z)
    if(num>=(1<<size)):return False
    else:return num
def get_primes(n,type="int"):
    if n==0:
        if type=="int":return []
        else:return [False]
    A=[True]*(n+1)
    A[0]=False
    A[1]=False
    for a in range(2,n+1):
        if A[a]:
            for b in range(a*2,n+1,a):
                A[b]=False
    if(type=="bool"):return A
    B=[]
    for a in range(n+1):
        if(A[a]):B.append(a)
    return B
def is_prime(num):
    if(num<=1):return False
    i=2
    while i*i<=num:
       if(num%i==0):return False
       i+=1
    return True
def ifelse(a,b,c):
    if a:return b
    else:return  c
def join(A,c=""):
    n=len(A)
    A=list(map(str,A))
    s=""
    for a in range(n):
        s+=A[a]
        if(a<n-1):s+=c
    return s
def factorize(n,type_="dict"):
    b = 2
    list_ = []
    while b * b <= n:
        while n % b == 0:
            n //= b
            list_.append(b)
        b+=1
    if n > 1:list_.append(n)
    if type_=="dict":
        dic={}
        for a in list_:
            if a in dic:
                dic[a]+=1
            else:
                dic[a]=1
        return dic
    elif type_=="list":
        return list_
    else:
        return None
def floor_(n,x=1):
    return x*(n//x)
def ceil_(n,x=1):
    return x*((n+x-1)//x)
    return ret
def seifu(x):
    return x//abs(x)
######################################################################################################
def main():
    n=int(input())
    A=list(map(int,input().split()))
    D=defaultdict(int)
    for a in A:
        D[a]+=1
    mmax=0
    for a in A:
        mmax=max(mmax,D[a]+D[a-1]+D[a+1])
    print(mmax)
main()

Submission status: Accepted

Solution code:
from collections import Counter

input()
values = tuple(map(int, input().split(' ')))
counter = Counter(values)

max_count = 0
best_x = None

for x in counter:
    count = counter[x - 1] + counter[x] + counter[x + 1]
    if count > max_count:
        max_count = count
        best_x = x

print(max_count)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
maxim = max(A)
counter = [0]*(maxim+1)
dif = [-1, 0, 1]
for i in A:
    for d in dif:
        j = i + d
        if j < 0 or maxim < j:
            continue
        else:
            counter[j] += 1
print(max(counter))

Submission status: Accepted

Solution code:
import math
import queue
from collections import defaultdict

def readInt():
	return int(input())
def readInts():
	return list(map(int, input().split()))
def readChar():
	return input()
def readChars():
	return input().split()
def factorization(n):
	res = []
	if n%2==0:
		res.append(2)
	for i in range(3,math.floor(n//2)+1,2):
		if n%i==0:
			c = 0
			for j in res:
				if i%j==0:
					c=1
			if c==0:
				res.append(i)
	return res
def fact2(n):
	p = factorization(n)
	res = []
	for i in p:
		c=0
		z=n
		while 1:
			if z%i==0:
				c+=1
				z/=i
			else:
				break
		res.append([i,c])
	return res
def fact(n):#階乗
	ans = 1
	m=n
	for _i in range(n-1):
		ans*=m
		m-=1
	return ans
def comb(n,r):#コンビネーション
	if n<r:
		return 0
	l = min(r,n-r)
	m=n
	u=1
	for _i in range(l):
		u*=m
		m-=1
	return u//fact(l)
def printQueue(q):
	r=qb
	ans=[0]*r.qsize()
	for i in range(r.qsize()-1,-1,-1):
		ans[i] = r.get()
	print(ans)
def dq():
	return queue.deque()
class UnionFind():
	def __init__(self, n):
		self.n = n
		self.parents = [-1]*n

	def find(self, x): # root
		if self.parents[x]<0:
			return x
		else:
			self.parents[x] = self.find(self.parents[x])
			return self.parents[x]

	def union(self,x,y):
		x = self.find(x)
		y = self.find(y)

		if x==y:
			return

		if self.parents[x]>self.parents[y]:
			x,y = y,x

		self.parents[x]+=self.parents[y]
		self.parents[y]=x

	def size(self,x):
		return -1*self.parents[self.find(x)]

	def same(self,x,y):
		return self.find(x)==self.find(y)

	def members(self,x): # much time
		root = self.find(x)
		return [i for i in range(self.n) if self.find(i)==root]

	def roots(self):
		return [i for i,x in enumerate(self.parents) if x<0]

	def group_count(self):
		return len(self.roots())

	def all_group_members(self):
		return {r: self.members(r) for r in self.roots()} # 1~n
def bitArr(n):#ビット全探索
	x = 1
	zero = "0"*n
	ans = []
	ans.append([0]*n)
	for i in range(2**n-1):
		ans.append(list(map(lambda x:int(x),list((zero+bin(x)[2:])[-1*n:]))))
		x+=1
	return ans;
def arrsSum(a1,a2):
	for i in range(len(a1)):
		a1[i]+=a2[i]
	return a1
def maxValue(a,b,v):
	v2 = v
	for i in range(v2,-1,-1):
		for j in range(v2//a+1): #j:aの個数
			k = i-a*j
			if k%b==0:
				return i
	return -1

n = readInt()
a = readInts()

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

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

ans = 1

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

print(ans)

Submission status: Accepted

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

dictA = {}

for x in a:
    dictA.setdefault(x, 0)
    dictA[x] += 1
    if x - 1 > 0:
        dictA.setdefault(x - 1, 0)
        dictA[x - 1] += 1
    dictA.setdefault(x + 1, 0)
    dictA[x + 1] += 1
print(max(dictA.values()))

Submission status: Accepted

Solution code:
def main():
    from collections import Counter
    n, *a = map(int, open(0).read().split())
    b = Counter(a)
    *c, = map(lambda x: tuple(x), b.items())
    c.sort(key=lambda x: x[0])
    ans = 0
    c = [c[0]] + c + [c[-1]]
    for x, y, z in zip(c, c[1:], c[2:]):
        v = y[0]
        tmp = y[1]
        if v - x[0] == 1:
            tmp += x[1]
        if v - z[0] == -1:
            tmp += z[1]
        ans = max(ans, tmp)

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def main():
    from collections import Counter
    n, *a = map(int, open(0).read().split())
    b = Counter(a)
    c = [(k, v) for k, v in b.items()]
    c.sort(key=lambda x: x[0])
    ans = 0
    c = [c[0]] + c + [c[-1]]
    for x, y, z in zip(c, c[1:], c[2:]):
        v = y[0]
        tmp = y[1]
        if v - x[0] == 1:
            tmp += x[1]
        if v - z[0] == -1:
            tmp += z[1]
        ans = max(ans, tmp)

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
#print#!/usr/bin/env python3
#%% for atcoder uniittest use
import sys
input= lambda: sys.stdin.readline().rstrip()
sys.setrecursionlimit(10**9)
def pin(type=int):return map(type,input().split())
def tupin(t=int):return tuple(pin(t))
def lispin(t=int):return list(pin(t))
#%%code
from fractions import gcd
import math
def resolve():
    N=pin()
    X=sorted(lispin())
    d=[0]*(10**5+1)
    for x in X:
        d[x]+=1
    ans=0
    for i in range(10**5+1):
        a,b,c=0,0,0
        try:
            a=d[i]
            b=d[i+1]
            c=d[i+2]
        except IndexError:pass
        ans=max(ans,a+b+c)
    print(ans)
        
#%%submit!      
resolve()

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
maxim = max(A)
counter = [0]*(maxim+1)
dif = [-1, 0, 1]
for a in A:
    for d in dif:
        i = a + d
        if i < 0 or maxim < i:
            continue
        else:
            counter[i] += 1
print(max(counter))

Submission status: Accepted

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

Submission status: Accepted

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

def main():
    n = int(input())
    a = list(map(int, input().split()))
    l = [0]*(10**5 + 10)


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

main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import collections

N = int(input())
counter = collections.defaultdict(int)
for i in list(map(int, input().split())):
    counter[i - 1] += 1
    counter[i] += 1
    counter[i + 1] += 1

print(max(counter.values()))

Submission status: Accepted

Solution code:
import collections
N = int(input())
a = list(map(int, input().split()))
if N == 1:
    print(1)
    exit()
if N == 2:
    if abs(a[0]-a[1]) <= 2:
        print(2)
    else:
        print(1)
    exit()
c = collections.Counter(a)
c_key = list(c.keys())
c_value = list(c.values())
res = 0
if len(c) == 2:
    if int(c_key[1])-int(c_key[0]) <= 2:
        print(c_value[0]+c_value[1])
        exit()
for i in range(1,len(c)-1):
    temp = c_value[i]
    temp_2 = 0
    temp_3 = 0
    flag = True
    if int(c_key[i])-int(c_key[i-1]) == 1:
        temp += c_value[i-1]
        flag = False
    if int(c_key[i+1])-int(c_key[i]) == 1:
        temp += c_value[i+1]
        flag = False
    if flag and int(c_key[i+1])-int(c_key[i]) == 2:
        temp_2 = c_value[i+1]+c_value[i]
        flag = False
    if flag and int(c_key[i])-int(c_key[i-1]) == 2:
        temp_3 = c_value[i-1]+c_value[i]
    if max(temp,temp_2,temp_3) > res:
        res = max(temp,temp_2,temp_3)
print(max(res,c_value[0],c_value[-1]))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!
from collections import defaultdict

n=int(input())
a=[int(x) for x in input().split()]
d=defaultdict(int)
for i in a:
    d[i]+=1
#print(d)
b=[[-2,0],[-1,0]]
for i in d:
    b.append([i,d[i]])
b.sort()

ans=1
m=len(b)
for i in range(m):
    ans=max(ans,b[i][1])
    if i+1<m and b[i][0]+1==b[i+1][0]:
        ans=max(ans,b[i][1]+b[i+1][1])
    if i+2<m and b[i][0]+1==b[i+1][0] and b[i+1][0]+1==b[i+2][0]:
        ans=max(ans,b[i][1]+b[i+1][1]+b[i+2][1])
print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import collections,sys
def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
N = I()
a = LI()
ans = 0
num_count = collections.defaultdict(int)
for x in a:
    num_count[x-1] += 1
    num_count[x] += 1
    num_count[x+1] += 1
print(max(num_count.values()))

Submission status: Accepted

Solution code:
import sys
def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
N = I()
a = LI()
ans = 0
num_count = [0]*(10**5+2)
for x in a:
    num_count[x-1] += 1
    num_count[x] += 1
    num_count[x+1] += 1
print(max(num_count))

Submission status: Accepted

Solution code:
import sys
def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
N = I()
a = LI()
ans = 0
num_count = [0]*(10**5+1)
for x in a:
    num_count[x] += 1
for i in range(10**5+1):
    if i+2<=10**5:
        ans = max(ans,sum(num_count[i:i+3]))
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter
N = int(input())
A = list(map(int, input().split()))
dic = Counter(A)
cnt = 1
Mcnt = 1
for i in range(0,N):
  if (i-1) in dic and (i+1) in dic:
      cnt = dic[i-1] + dic[i] + dic[i+1]
  elif (i-1) in dic:
      cnt = dic[i-1] + dic[i]
  elif (i+1) in dic:
      cnt = dic[i] + dic[i+1]
  else:
      cnt = dic[i]
  if Mcnt < cnt:
    Mcnt = cnt
print(Mcnt)

Submission status: Accepted

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

dic={}
for i in range(n):
    dic[a_input[i]]=dic.get(a_input[i],0)+1
    dic[a_input[i]-1]=dic.get(a_input[i]-1,0)+1
    dic[a_input[i]+1]=dic.get(a_input[i]+1,0)+1
print(max(dic.values()))

Submission status: Accepted

Solution code:
n=int(input())
from collections import Counter as co
var=co(list(map(int,input().split())))
ans=0
for k in var:
  p=var[k]
  if k-1 in var:p+=var[k-1]
  if k+1 in var:p+=var[k+1]
  
  ans=max(ans,p)
print(ans)

Submission status: Accepted

Solution code:
import sys
from collections import Counter
from collections import deque
def input(): return sys.stdin.readline().strip()
def mp(): return map(int,input().split())
def lmp(): return list(map(int,input().split()))
n=int(input())
a=mp()
l=[0]*(10**5+10)
for x in a:
    l[x]+=1
    l[x-1]+=1
    l[x+1]+=1
print(max(l))

Submission status: Accepted

Solution code:
import sys
## io ##
def IS(): return sys.stdin.readline().rstrip()
def II(): return int(IS())
def MII(): return list(map(int, IS().split()))
def MIIZ(): return list(map(lambda x: x-1, MII()))
## dp ##
def DD2(d1,d2,init=0): return [[init]*d2 for _ in range(d1)]
def DD3(d1,d2,d3,init=0): return [DD2(d2,d3,init) for _ in range(d1)]
## math ##
def to_bin(x: int) -> str: return format(x, 'b') # rev => int(res, 2)
def to_oct(x: int) -> str: return format(x, 'o') # rev => int(res, 8)
def to_hex(x: int) -> str: return format(x, 'x') # rev => int(res, 16)
MOD=10**9+7
def divc(x,y) -> int: return -(-x//y)
def divf(x,y) -> int: return x//y
def gcd(x,y):
    while y: x,y = y,x%y
    return x
def lcm(x,y): return x*y//gcd(x,y)
def enumerate_divs(n):
    """Return a tuple list of divisor of n"""
    return [(i,n//i) for i in range(1,int(n**0.5)+1) if n%i==0]
def get_primes(MAX_NUM=10**3):
    """Return a list of prime numbers n or less"""
    is_prime = [True]*(MAX_NUM+1)
    is_prime[0] = is_prime[1] = False
    for i in range(2, int(MAX_NUM**0.5)+1):
        if not is_prime[i]: continue
        for j in range(i*2, MAX_NUM+1, i): is_prime[j] = False
    return [i for i in range(MAX_NUM+1) if is_prime[i]]
def prime_factor(n):
    """Return a list of prime factorization numbers of n"""
    res = []
    for i in range(2,int(n**0.5)+1):
        while n%i==0: res.append(i); n //= i
    if n != 1: res.append(n)
    return res
## libs ##
from itertools import accumulate as acc, combinations as combi, product, combinations_with_replacement as combi_dup
from collections import deque, Counter
from heapq import heapify, heappop, heappush
from bisect import bisect_left
#======================================================#

def main():
    n = II()
    aa = MII()
    x = {i: 0 for i in range(-1, 10**5+2)}
    for a in aa:
        x[a-1] += 1
        x[a] += 1
        x[a+1] += 1
    print(max(x.values()))

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from collections import Counter
n=int(input())
a=list(map(int,input().split()))
sa=set(a)
dic=Counter(a)
hantei=0
for i in dic:
  if (i-1) in dic and (i+1) in dic:
    x=dic[i]+dic[i-1]+dic[i+1]
  elif (i-1) in dic:
    x=dic[i]+dic[i-1]
  elif (i+1) in dic:
    x=dic[i]+dic[i+1]
  else:
    x=dic[i]
  if hantei < x:
    hantei=x
print(hantei)

Submission status: Accepted

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

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

for i in A:
    dd[i+1]+=1
    dd[i]+=1
    dd[i-1]+=1
print(max(dd.values()))

Submission status: Accepted

Solution code:
from collections import Counter
N = int(input())
L = list(map(int, input().split()))
c = Counter(L)
# print(c)
# 先にxを選んでおく！
ans = 0
m = max(L) + 1
for i in range(m):
    ans = max(ans, c[i - 1] + c[i] + c[i + 1])

print(ans)

Submission status: Accepted

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

  c = [0] * (max(a)+3)

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

  ans = 0

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

  print(ans)

if __name__ == "__main__":
  main()

Submission status: Accepted

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

Submission status: Accepted

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

from collections import defaultdict
dict = defaultdict(int)
for a in A:
    dict[a-1] += 1
    dict[a] += 1
    dict[a+1] += 1

print(max(dict.values()))

Submission status: Accepted

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

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

for a in al:
    num[a] += 1

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

print(ans)

Submission status: Accepted

Solution code:
import numpy as np
import sys
import math

n=int(input())
A=np.array(sorted(list(map(int,input().split()))))
C=np.bincount(A)
D=C.copy()
D[1:]+=C[:-1]
D[:-1]+=C[1:]
print(np.max(D))

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
print(max(cnt))

Submission status: Accepted

Solution code:
from sys import stdin
from collections import Counter


if __name__ == "__main__":
    _in = [_.rstrip() for _ in stdin.readlines()]
    N = int(_in[0])  # type:int
    a_arr = list(map(int,_in[1].split(' ')))  # type:list(int)
    # vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
    count = Counter(a_arr)
    cnt = 0
    for key in count.keys():
        _ = count[key]
        try:
            _ += count[key+1]
        except:
            pass
        try:
            _ += count[key-1]
        except:
            pass
        cnt = max(cnt,_)
    # ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    print(cnt)

Submission status: Accepted

Solution code:
from sys import stdin
from collections import Counter


if __name__ == "__main__":
    _in = [_.rstrip() for _ in stdin.readlines()]
    N = int(_in[0])  # type:int
    a_arr = list(map(int,_in[1].split(' ')))  # type:list(int)
    # vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
    count = Counter(a_arr)
    cnt = 0
    keys = sorted(count.keys())
    for key in keys:
        _ = count[key]
        try:
            _ += count[key+1]
        except:
            pass
        try:
            _ += count[key-1]
        except:
            pass
        cnt = max(cnt,_)
    # ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    print(cnt)

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
    cnt[a[i] + 1] += 1

ans = 0
for i in cnt:
    ans = max(ans, cnt[i])

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

from collections import defaultdict
cnts = defaultdict(int)
for a in A:
    if a != 0:
        cnts[a-1] += 1
    cnts[a] += 1
    cnts[a+1] += 1

print(max(cnts.values()))

Submission status: Accepted

Solution code:
#!/usr/bin/env python
# coding: utf-8

# In[37]:


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


# In[38]:


a_list = a
a_plus = [item+1 for item in a]
a_minus = [item-1 for item in a]
# print(a)
# print(a_plus)
# print(a_minus)


# In[39]:


mylist = a_list + a_plus + a_minus


# In[40]:


import collections


# In[41]:


counted = collections.Counter(mylist)
ans = max(counted.values())
print(ans)


# In[ ]:

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
up = 100001
l = [0 for i in range(up)]
for i in range(n):
    l[a[i]] += 1
ma = -1
for i in range(1, up-1):
    if ma < l[i-1] + l[i] + l[i+1]:
        ma = l[i-1] + l[i] + l[i+1]
print(ma)

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

Submission status: Accepted

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

m = []

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


d ={}
for i in m:
    if i not in d:
        d[i] =0
    d[i] += 1



b = 0
for i in d.keys():
    if b <= d[i]:
        b = d[i]


print(b)

Submission status: Accepted

Solution code:
import itertools
import sys
import numpy as np
import collections
n=int(input())
a=np.array(list(map(int,input().split())))
add_a=a+1
al=list(add_a)
minus_a=a-1
ml=list(minus_a)
l=list(a)+al+ml
print(collections.Counter(l).most_common()[0][1])

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

Submission status: Accepted

Solution code:
import collections

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

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

Submission status: Accepted

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

N = int(input())
# S = input()
# N, K = map(int, input().split())
A = list(map(int, input().split()))
from collections import Counter

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

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import sys
from io import StringIO
import unittest
from collections import Counter

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

    counter = Counter(a)

    ans = 0

    for key, value in counter.items():
        if key == 0:
            tmp = counter[key + 1] + value
        else:
            tmp = counter[key + 1] + counter[key] + counter[key - 1]

        if ans < tmp:
            ans = tmp

    print(ans)


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)

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

Submission status: Accepted

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

ans=0
now=0
for i,l in enumerate(L):
    if now<l:
        now=l
        ans=i

print(now)

Submission status: Accepted

Solution code:
def main():
    from collections import deque

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

    b = [0] * (10 ** 5 + 10)
    for x in a:
        b[x] += 1

    dq = deque()
    b_iter = iter(b)
    dq.append(next(b_iter))
    dq.append(next(b_iter))
    dq.append(next(b_iter))
    t = sum(dq)
    ans = t
    for x in b:
        dq.append(x)
        y = dq.popleft()
        t = t + x - y
        if ans < t:
            ans = t
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from collections import Counter
N,*a = map(int, open(0).read().split())
if N == 1:
    print(1)
    exit()
lst = a[:]
for i in a:
    lst += [i+1,i-1]
c = Counter(lst)
print(c.most_common()[0][1])

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**6) #再帰関数の上限
import math
from copy import copy, deepcopy
from operator import itemgetter
from bisect import bisect_left, bisect, bisect_right#2分探索
#bisect_left(l,x), bisect(l,x)#aはソート済みである必要あり。aの中からx未満の要素数を返す。rightだと以下
from collections import deque 
#deque(l), pop(), append(x), popleft(), appendleft(x)
##listでqueの代用をするとO(N)の計算量がかかってしまうので注意
from collections import Counter#文字列を個数カウント辞書に、
#S=Counter(l),S.most_common(x),S.keys(),S.values(),S.items()
from itertools import accumulate#累積和
#list(accumulate(l))
from heapq import heapify,heappop,heappush
#heapify(q),heappush(q,a),heappop(q) #q=heapify(q)としないこと、返り値はNone
#import fractions#古いatcoderコンテストの場合GCDなどはここからimportする

def input(): return sys.stdin.readline()[:-1]
def printl(li): print(*li, sep="\n")
def argsort(s, return_sorted=False): 
    inds=sorted(range(len(s)), key=lambda k: s[k])
    if return_sorted: return inds, [s[i] for i in inds]
    return inds

def main():
    mod = 10**9+7
    #w.sort(key=itemgetter(1),reversed=True)  #二個目の要素で降順並び替え

    N = int(input())
    #N, K = map(int, input().split())
    A = tuple(map(int, input().split())) #1行ベクトル
    #L = tuple(int(input()) for i in range(N)) #改行ベクトル
    #S = tuple(tuple(map(int, input().split())) for i in range(N)) #改行行列
    count=0
    l=[0]*(10**6)
    for a in A:
        l[a]+=1
        l[a+1]+=1
        l[a+2]+=1
    print(max(l))




if __name__ == "__main__":
    main()

Submission status: Accepted

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

def solve(N,A):
    ans = 0
    cnts = [0]*10**5
    for a in A:
        cnts[a] += 1
    ans = 0
    for i in range(10**5-2):
        ans = max(ans,sum(cnts[i:i+3]))
    return ans
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 e in C:
    ans = max(ans,C[e]+C[e-1]+C[e+1])
print(ans)

Submission status: Accepted

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

c = collections.defaultdict(int)
ans = 0
for x in a:
    c[x] += 1
    c[x-1] += 1
    c[x+1] += 1
    ans = max(ans, c[x], c[x-1], c[x+1])

print(ans)

Submission status: Accepted

Solution code:
from collections import Counter
N = int(input())
a = list(map(int, input().split()))
c = Counter(a)
d = [c[i]+c[i+1]+c[i+2] for i in range(99998)]
print(max(d))

Submission status: Accepted

Solution code:
from collections import Counter

N = int(input())
a = list(map(int, input().split()))
b = Counter(a)
ans = 0
X = 0
for i in b.keys():
    if X < b[i-1] + b[i] + b[i+1]:
        X = b[i-1] + b[i] + b[i+1]
print(X)

Submission status: Accepted

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

A=[0]*(max(a)+1)
ans=0

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

if len(A)>3:
    for j in range(1,max(a)):
        ans=max(ans,A[j-1]+A[j]+A[j+1])
else:
    ans=sum(A)

print(ans)

Submission status: Accepted

Solution code:
from collections import Counter
n=int(input())
a=list(map(int, input().split()))
dic=Counter(a)
ans=0
if n==1:
  print('1')
elif n==2:
  if any([a[0]==a[1],a[0]==a[1]-1,a[0]==a[1]+1]):
    print('2')
  else:
    print('1')
else:
  for i in range(n-2):
    ans=max(ans,dic[i]+dic[i+1]+dic[i+2])
  print(ans)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(1 << 25)
read = sys.stdin.readline
ra = range
enu = enumerate


def read_ints():
    return list(map(int, read().split()))


def read_a_int():
    return int(read())


def read_tuple(H):
    '''
    H is number of rows
    '''
    ret = []
    for _ in range(H):
        ret.append(tuple(map(int, read().split())))
    return ret


def read_col(H):
    '''
    H is number of rows
    A列、B列が与えられるようなとき
    ex1)A,B=read_col(H)    ex2) A,=read_col(H) #一列の場合
    '''
    ret = []
    for _ in range(H):
        ret.append(list(map(int, read().split())))
    return tuple(map(list, zip(*ret)))


def read_matrix(H):
    '''
    H is number of rows
    '''
    ret = []
    for _ in range(H):
        ret.append(list(map(int, read().split())))
    return ret
    # return [list(map(int, read().split())) for _ in range(H)] # 内包表記はpypyでは遅いため


MOD = 10**9 + 7
INF = 2**31  # 2147483648 > 10**9
# default import
from collections import defaultdict, Counter, deque
from operator import itemgetter
from itertools import product, permutations, combinations
from bisect import bisect_left, bisect_right  # , insort_left, insort_right

N = read_a_int()
A = read_ints()

# どの数字を一番多くできるか
n_X = [0] * (10 ** 5 + 2)  # =Xにできる個数

for a in A:
    for da in [-1, 0, 1]:
        n_X[a + da + 1] += 1
print(max(n_X))
# https://atcoder.jp/contests/abc072/tasks/arc082_a