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:
import sys

stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().strip()

n = ni()
a = na()

t = []
for i in a:
    t.append(i)
    t.append(i+1)
    t.append(i-1)

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

Submission status: Accepted

Solution code:
from collections import Counter

def max_count(nums):
    nums_decr = map(lambda x: x - 1, nums)
    nums_incr = map(lambda x: x + 1, nums)
    counter = Counter(nums)
    counter.update(nums_decr)
    counter.update(nums_incr)
    return counter.most_common(1)[0][1]


if __name__ == '__main__':
    _ = input()
    nums = list(map(int, input().split()))
    print(max_count(nums))

Submission status: Accepted

Solution code:
def c_together(N, A):
    from collections import defaultdict
    count = defaultdict(int)
    for i in A:
        count[i] += 1
    ans = 0
    for i in range(max(A) + 1):
        # ある整数iにするには、i-1に+1する、何もしない、i+1に-1する3パターン
        tmp = count[i - 1] + count[i] + count[i + 1]
        ans = max(ans, tmp)
    return ans

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

Submission status: Accepted

Solution code:
from collections import OrderedDict

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

number_hash = OrderedDict()

for i in sorted(nums):
    if i not in number_hash:
        number_hash[i] = 1
    else:
        number_hash[i] += 1

ans = 0
for i in number_hash:
    cnt = 0
    # 今とある定数をハッシュから引き出した
    # ハッシュの中から今取り出した数プラマイ１の数字が多い物を見つける
    i_d = i - 1
    i_u = i + 1
    if i_d in number_hash:
        cnt += number_hash[i_d]
    if i_u in number_hash:
        cnt += number_hash[i_u]
    cnt += number_hash[i]

    if ans < cnt:
        ans = cnt

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
a = list(map(int, input().split()))
d = {-1:0,max(a)+1:0}
num_X = 0
for i in range(max(a)+1):
    d[i] = 0
for i in a:
    d[i] += 1
for i in range(max(a)+1):
    num_X = max(num_X,d[i-1]+d[i]+d[i+1])
print(num_X)

Submission status: Accepted

Solution code:
import collections
n = int(input())
a = [int(i) for i in input().split()]
x,answer = collections.Counter(a),0
for i in range(min(a),max(a)+1):
	num = 0
	for j in range(i-1,i+2):
		try:
			num += x[j]
		except:
			pass
	if num>answer:
		answer = num
print(answer)

Submission status: Accepted

Solution code:
N = int(input())
num = list(1+ int(x) for x in input().split())
L = max(num)
sumR = [0 for x in range(L+3)]
for i in range(N):
  sumR[num[i]-1] +=1
  sumR[num[i]+2] -=1
for j in range(len(sumR)-1):
  sumR[j+1] += sumR[j]
print(max(sumR))

Submission status: Accepted

Solution code:
from collections import Counter

N=int(input())

counter=Counter(list(map(int,input().split())))
res=0

for i in counter:
    cnt=counter[i-1]+counter[i]+counter[i+1]
    if res<cnt:res=cnt
print(res)

Submission status: Accepted

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

num = 0
for i in range(1, 100000):
    num = max(num, sum(p[i - 1: i + 2]))

print(num)

Submission status: Accepted

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

maximum = max(A)
count = [0]*(maximum + 2)

for i in range(N):
    if A[i] == 0:
        count[0] += 1
        count[1] += 1
        
    elif A[i] == maximum:
        count[A[i]] += 1
        count[A[i] - 1] += 1
    
    else:
        count[A[i] + 1] += 1
        count[A[i]] += 1
        count[A[i] - 1] += 1

print(max(count))

Submission status: Accepted

Solution code:
import numpy as np
N = int(input())
a = list(input().split())
a = [int(i) for i in a]
res = np.zeros(max(a)+2)
for i in range(len(a)):
    res[a[i]-1:a[i]+2] += 1
print(int(max(res)))

Submission status: Accepted

Solution code:
from collections import *
n=int(input())
a=list(map(int,input().split()))
b=set(a)
d=defaultdict(int)
mx=0
for i in a:
  d[i]+=1
for i in b:
  mx=max(mx,d[i-1]+d[i]+d[i+1])
print(mx)

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
count=0
a_plus=[]
a_minu=[]
for i in range(n):
    a_plus.append(a[i]+1)
    a_minu.append(a[i]-1)
import collections
a.extend(a_plus)
a.extend(a_minu)
data=collections.Counter(a)
data=sorted(data.items(),key=lambda x:x[1],reverse=True)
print(data[0][1])

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import Counter
n = int(input())
a = sorted([int(i) for i in input().split()])
c = Counter(a)
d =[]
for k,v in c.items():
    d.append([k,v])

    

def count(a):
    na = len(a)
    count = 0
    cmax = 0
    if n == 1:
        return 1
    else:
        for i in range(na):
            j = i
            while j <= na-1  and a[j][0]-a[i][0] <= 2:
                count += a[j][1]
                j += 1
            if count > cmax:
                cmax = count
            count = 0
        return cmax

print(count(d))

Submission status: Accepted

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

Submission status: Accepted

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

dic = {}
for num in l:
    dic.setdefault(num, 0)
    dic[num] += 1
max_num = 0
for k, v in dic.items():
    prev = dic[k - 1] if k - 1 in dic else 0
    next = dic[k + 1] if k + 1 in dic else 0

    if (prev + v + next) > max_num:
        max_num = prev + v + next
print(max_num)

Submission status: Accepted

Solution code:
import numpy as np
n = int(input())
j = np.array([0 for i in range(int(10e5 + 1))])
m = list(map(int, input().split()))
for i in m:
    j[max(0, i-1):  i+2] += 1
print(np.max(j))

Submission status: Accepted

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

dp = [0]*(int(1e5) + 1)
for e in a:
    dp[e] += 1

ans = 0
for i in range(1, int(1e5)):
    ans = max(ans, dp[i-1] + dp[i] + dp[i+1])
print(ans)

Submission status: Accepted

Solution code:
n=input()
n=int(n)
nums=input()
co=[0]*100010
nums=list(map(int,nums.split(" ")))
for i in nums:
  co[i]+=1
  co[i-1]+=1
  co[i+1]+=1
print(max(co))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
input_lines = input()
alist = list(map(int, input_lines.split(' ')))
sum_list = [0 for i in range(max(alist) + 3)]

for a in alist:
	sum_list[a] += 1
	sum_list[a + 1] += 1
	sum_list[a + 2] += 1

print(max(sum_list))

Submission status: Accepted

Solution code:
n = int(input())
a = list(int(i) for i in input().split())

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

    if a[i]+1 in dic:
        dic[a[i]+1]+=1
    else:
        dic[a[i]+1]=1

    if a[i]-1 in dic:
        dic[a[i]-1]+=1
    else:
        dic[a[i]-1]=1

print(max(dic.values()))

Submission status: Accepted

Solution code:
from collections import Counter
n = int(input())

cnt = Counter()
for x in map(int, input().split()):
    cnt[x] += 1
    cnt[x-1]+=1
    cnt[x+1]+=1

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

Submission status: Accepted

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

C = [0 for _ in range(100000+2)]
for a in A:
    C[a] += 1
    C[a+1] += 1
    C[a+2] += 1
C.sort()
print(C[-1])

Submission status: Accepted

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

Submission status: Accepted

Solution code:
num = int(input())
val = [int(x) for x in input().split()]

numbers = [int(x) for x in range(-1, 100001)]
numbers_dict = dict()

for number in numbers:
    numbers_dict[number] = numbers_dict.get(number, 0) + 1

for i in val:
    numbers_dict[i - 1] += 1
    numbers_dict[i] += 1
    numbers_dict[i + 1] += 1

print(max(numbers_dict.values())-1)

Submission status: Accepted

Solution code:

from collections import Counter


input()
xs = list(map(int, input().split()))


c = Counter(xs)


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

print(res)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split(" ")))
B = {}

for center in A:
	for num in [center-1,center,center+1]:
		if num in B:
			B[num] += 1
		else:
			B[num] = 1

print(max(B.values()))

Submission status: Accepted

Solution code:

from collections import Counter

N = input()
line = input().split(" ")

cnt = Counter()
for i in line:
    x = int(i)
    cnt[x] += 1
    cnt[x-1] += 1
    cnt[x+1] += 1
print(cnt.most_common(1)[0][1])

Submission status: Accepted

Solution code:
n = int(input())
#k = int(input())
arr = list(map(int, input().split()))
a = [0 for i in range(0,max(arr)+2)]
for ar in arr:
  if ar is not 0:
    a[ar-1] += 1
  a[ar] += 1
  #if ar is not max(arr)+1:
  a[ar+1] += 1 
ans = max(a)

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
inlis = list(map(int,input().split()))
donelis =[]
numlis=[]

for i in inlis:
    donelis.append(i+1)
    donelis.append(i)
    donelis.append(i-1)

estc =0
donelis.sort()
a = -2
count=0
for i in donelis:
    if a != i:
        if count > estc:
            estc = count
        count = 1
        a = i
    else:
        count +=1
if count >estc:
    estc =count
print(estc)

Submission status: Accepted

Solution code:
n = int(input())
inlis = list(map(int,input().split()))
donelis =[]
numlis=[]

for i in inlis:
    donelis.append(i+1)
    donelis.append(i)
    donelis.append(i-1)

estc =0
donelis.sort()
a = -2
count=0
for i in donelis:
    if a != i:
        if count > estc:
            estc = count
        count = 1
        a = i
    else:
        count +=1
if count >estc:
    estc =count
print(estc)

Submission status: Accepted

Solution code:
from collections import Counter


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

c = Counter(a)
a=list(set(a))
a.sort()
x=0
for i in a:
  a_1=0
  a_2=0
  if i-2 in c:
    a_2=c[i-2]
  if i-1 in c:
    a_1=c[i-1]
  x0 = a_2 + a_1 + c[i]
  x=max(x,x0)
print(x)

Submission status: Accepted

Solution code:
N = int(input())
xs = list(map(int, input().split()))
counts = [0]*100005

for x in xs:
  if x > 0: counts[x-1] += 1
  counts[x] += 1
  counts[x+1] += 1

print(max(*counts))

Submission status: Accepted

Solution code:
from collections import defaultdict

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

cnt = defaultdict(int)
for ai in a:
	cnt[ai - 1] += 1
	cnt[ai] += 1
	cnt[ai + 1] += 1

res = max(cnt.values())

print(res)

Submission status: Accepted

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

def solve(cand):
	global a
	res = 0
	for ai in a:
		if ai - 1 == cand or ai == cand or ai + 1 == cand:
			res += 1
	return res

cnt = {}
for ai in a:
	for i in range(-1,2):
		b = ai + i
		if b >= 0:
			if b not in cnt:
				cnt[b] = 1
			else:
				cnt[b] += 1

candX = max(cnt.items(), key=lambda x:x[1])[0]
res = solve(candX);

print(res)

Submission status: Accepted

Solution code:
from collections import defaultdict

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

d = defaultdict(int)
for a in A:
    d[a - 1] += 1
    d[a] += 1
    d[a + 1] += 1

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

Submission status: Accepted

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

from collections import Counter

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

countDict = Counter(aList)

maxC = 0

for i in range(0, 100001):
    
    maxCandidate = countDict[i]+countDict[i+1]+countDict[i+2]
    
    if maxCandidate > maxC:
        maxC = maxCandidate
        
print(maxC)

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

Submission status: Accepted

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

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

distribution = {}

def set(key):
    global distribution
    if not key in distribution:
        distribution[key] = 1
    else:
        distribution[key] += 1

for a in alist:
    set(a - 1)
    set(a)
    set(a + 1)

ret = -1
for value in distribution.values():
    if ret < value:
        ret = value
print(ret)

Submission status: Accepted

Solution code:
n = int(input())
a_n = list(map(int, input().split()))
a_n.sort()
a_n.reverse()
def check(idx):
    global a_n
    base = a_n[idx]
    count = 0
    dif = 0
    while dif <= 2:
        idx += 1
        count += 1
        if(idx == n):
            return count
        dif = base - a_n[idx]
    return count

ans = 1
i = 0
c = check(i)
tmp = a_n[0]
while(i != n):
    if(tmp != a_n[i]):
        tmp = a_n[i]
        c = check(i)
    if(ans < c):
        ans = c
    i += 1
print(ans)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-       
from collections import Counter

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

if(n==1):
    print('1')
else:
    ans = 0
    cnt = Counter(a)
    for i in range(1,max(a)+2):
        tmpsum = cnt[i-1]+cnt[i]+cnt[i+1]
        ans = max(tmpsum,ans)
    print(ans)

Submission status: Accepted

Solution code:
from collections import Counter

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

C = Counter(A)

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

print(ans)

Submission status: Accepted

Solution code:
from collections import Counter

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

C = Counter(A)

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

print(ans)

Submission status: Accepted

Solution code:
def i1():
 return int(input())
def i2():
 return [int(i) for i in input().split()]
n=i1()
a=i2()
d=[0 for i in range(10**5)]
for i in range(n):
 d[a[i]]+=1
 if a[i]<n-1:d[a[i]+1]+=1
 if a[i]>0:d[a[i]-1]+=1
print(max(d))

Submission status: Accepted

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

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

print(max(map(sum,zip(c , c [1:], c [2:]))))

Submission status: Accepted

Solution code:

N = int(input())
a = list(map(int, input().split()))
 
set_a = set(a)
cnt = [0] * (max(a)+1)
ans = 0
 
for i in a:
    cnt[i] += 1
 
if len(cnt) == 1:
    print(cnt[0])
elif len(cnt) == 2:
    print(sum(cnt))
else:
    for i in range(1, max(a)):
        ans = max(ans, cnt[i-1]+cnt[i]+cnt[i+1])

    print(ans)

Submission status: Accepted

Solution code:
dic={}
for i in range(100002):
    dic[i]=0
input()
for n in map(int,input().split()):
    dic[n]+=1
m=0
for i in range(0,100000):
    m=max(m,dic[i]+dic[i+1]+dic[i+2])
print(m)

Submission status: Accepted

Solution code:
def C_Together():

    try:
        LOCAL_FLAG
        N = 3
        a = [6, 2, 6]
        # N = 4
        # numbers = [2, 5, 5, 2]
        N = 6
        a = [12, 22, 16, 22, 13, 12]

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

    counter = {}
    # for i in range(N):
    #     if a[i] in counter:
    #         counter[a[i]] += 1
    #     else:
    #         counter[a[i]] = 1
    #
    # print(counter)
    #
    # results = 0
    # key = -1
    #
    # for k, v in counter.items():
    #     temp = v
    #     if k-1 in counter:
    #         temp += counter[k-1]
    #     if k+1 in counter:
    #         temp += counter[k+1]
    #     if(temp > results):
    #         results = temp
    #         key = k
    for i in range(N):
        counter[a[i]] = counter.get(a[i], 0) + 1

    # print(counter)

    results = 0
    key = -1

    for k, v in counter.items():
        temp = v + counter.get(k-1, 0) + counter.get(k+1, 0)
        if(temp > results):
            results = temp
            key = k

    print(results)
    


C_Together()

Submission status: Accepted

Solution code:
def C_Together():

    try:
        LOCAL_FLAG
        N = 3
        a = [6, 2, 6]
        # N = 4
        # numbers = [2, 5, 5, 2]
        N = 6
        a = [12, 22, 16, 22, 13, 12]

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

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

    results = 0
    key = -1

    for k, v in counter.items():
        temp = v
        if k-1 in counter:
            temp += counter[k-1]
        if k+1 in counter:
            temp += counter[k+1]
        if(temp > results):
            results = temp
            key = k

    print(results)
    # print(key)

C_Together()

Submission status: Accepted

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

for i in range(N):
    ans[A[i]-1] +=1
    ans[A[i]] +=1
    ans[A[i]+1] +=1

print(max(ans))

Submission status: Accepted

Solution code:
cnt = [0]*100002

n = int(input())

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



for x in a:

	cnt[x] += 1

	if x > 0 : cnt[x-1] += 1

	cnt[x+1] += 1

	

answer = 0

for n in cnt:

	answer = max(answer, n)



print(answer)

Submission status: Accepted

Solution code:
n = int(input())
l = []
for i in range(100002):
  l.append(0)

def sum(i):
  l[i] += 1
  l[i+1] += 1
  if i != 0:
    l[i-1] += 1

m = map(int , input().split())

for i in list(m):
  sum(i)

ans = 0

for i in range(100002):
  if ans < l[i]:
    ans = l[i]

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
cnt_a = [0] * 200000
A = [int(n) for n in input().split()]

for a in A:
  if a != 0:
    cnt_a[a-1] += 1
  cnt_a[a] += 1
  cnt_a[a+1] += 1

print(max(cnt_a))

Submission status: Accepted

Solution code:
import numpy as np
from collections import Counter

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

    ans = 0
    counter = Counter(A)
    for a, cnt in counter.most_common():
        tmp = counter[a-1] + cnt + counter[a+1]
        if ans < tmp:
            ans = tmp


    print(ans) 


main()

Submission status: Accepted

Solution code:
from collections import defaultdict, Counter
from itertools import product, groupby
from math import pi
from collections import deque
from bisect import bisect, bisect_left, bisect_right
INF = 10 ** 10


def main():
    N = int(input())
    c = Counter(map(int, input().split()))
    ans = 0
    for x in c:
        ans = max(ans, c[x - 1] + c[x] + c[x + 1])
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
# -*- encoding: utf-8 -*-
n = int(input().rstrip())
array = sorted(list(map(int, input().split(' '))))
count = [0] * 100002

for a in array:
    count[a - 1] += 1
    count[a    ] += 1
    count[a + 1] += 1

print(max(count))

Submission status: Accepted

Solution code:
N = int(input())
a = list(map(int, input().split()))
xCnt = dict()
ans = 0
for v in a:
    for i in range(-1,2):
        if v+i not in xCnt:
            xCnt[v+i] = 0
        xCnt[v+i] += 1
        ans = max(ans, xCnt[v+i])

print(ans)

Submission status: Accepted

Solution code:
import math
n = input()
ai = list(int(i) for i in input().split())
li = [0] * (max(ai)+10)
for i in range(len(ai)):
    p = ai[i]
    p = p+1
    li[p-1] = li[p - 1] + 1
    li[p] = li[p] + 1
    li[p + 1] = li[p + 1] + 1
print(max(li))

Submission status: Accepted

Solution code:
N = int(input())
nums = [0] * 100002
data = list(map(int, input().split()))
for A in data:
    nums[A-1] += 1
    nums[A] += 1
    nums[A+1] += 1
print(max(nums))

Submission status: Accepted

Solution code:
from collections import Counter


def main():
    """
    1<=N<=10^5: a_1 ... a_n
    0<=a<=10^5
    +1, -1, +0
    連続する3つの数値に対して、
    minに+1,中央に何もせず,maxに+1各操作をすると
    中央の個数が最大になる
    3つ揃わない場合もあるので、注意

    10^5で初期化 + 各数値の個数
    10^5とおりの数値についてa, a+1, a+2を想定して
        存在するものの個数をsum
    """
    N = int(input())
    counter = Counter(map(int, input().split()))
    n_max = 0

    def sort_func(t):
        return t[0]
    for a, n in sorted(counter.items(),
                       key=sort_func):

        temp_max = sum([
            counter.get(a - 1, 0),
            counter[a],
            counter.get(a + 1, 0)
        ])
        n_max = max(n_max, temp_max)
    print(n_max)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
input()
nums = [int(i) for i in input().split()]
col = {
	i : 0 for i in range(min(nums)-1, max(nums)+2)
}

for i in nums:
	col[i-1] += 1
	col[i] += 1
	col[i+1] +=1

print(max(col.values()))

Submission status: Accepted

Solution code:
import sys

N=input()
a=map(int, raw_input().split())
#print a
#print max(a)

dict={}


for i in range(len(a)):
	if a[i] in dict:
		dict[ a[i] ]+=1 
	else:
		dict[ a[i] ]=1

#print dict

max_sum=0
for key,value in dict.items():
	#print key,value
	sum=dict[key]
	if key-1 in dict:
		sum+=dict[key-1]
	if key+1 in dict:
		sum+=dict[key+1]

	if max_sum<sum:
		max_sum=sum

print max_sum

Submission status: Accepted

Solution code:
N = int(input())
a = list(map(int, input().split()))
 
res = [0] * 100000
 
for i in a:
	res [i] += 1
	
print(max(map(sum,zip(res , res [1:], res [2:]))))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
c=[0]*10**5;input()
for i in map(int,input().split()):
    c[i]+=1
print(max([sum(c[i:i+3]) for i in range(10**5)]))

Submission status: Accepted

Solution code:
import numpy as np;c=[0]*10**5;input()
for i in map(int,input().split()):
    c[i]+=1
print(max([sum(c[i:i+3]) for i in range(10**5)]))

Submission status: Accepted

Solution code:
n  = int(input())
ls = list(map(int, input().split()))
ls_plus1  = list(map(lambda x: x + 1, ls))
ls_minus1 = list(map(lambda x: x - 1, ls))
dic = {}
for a in ls_minus1 + ls + ls_plus1:
    dic[a] = dic.get(a, 0) + 1
print(max(dic.values()))

Submission status: Accepted

Solution code:
n  = int(input())
ls = list(map(int, input().split()))
ls_plus1  = list(map(lambda x: x + 1, ls))
ls_minus1 = list(map(lambda x: x - 1, ls))
dic = {}
for a in ls_minus1 + ls + ls_plus1:
    dic[a] = dic.get(a, 0) + 1
print(sorted(dic.values())[-1])

Submission status: Accepted

Solution code:
n  = int(input())
ls = list(map(int, input().split()))
ls_plus1  = list(map(lambda x: x + 1, ls))
ls_minus1 = list(map(lambda x: x - 1, ls))
dic = {}
for a in (ls_minus1 + ls + ls_plus1):
    if a in dic: 
        dic[a] += 1
    else: 
        dic[a] = 1
print(sorted(dic.values())[-1])

Submission status: Accepted

Solution code:
n  = int(input())
ls = list(map(int, input().split()))
ls_plus1  = list(map(lambda x: x + 1, ls))
ls_minus1 = list(map(lambda x: x - 1, ls))
dic = {}
for a, b, c in zip(ls_minus1, ls, ls_plus1):
    if a in dic: 
        dic[a] += 1
    else: 
        dic[a] = 1
    if b in dic:
        dic[b] += 1
    else:
        dic[b] = 1
    if c in dic:
        dic[c] += 1
    else:
        dic[c] = 1
print(sorted(dic.values())[-1])

Submission status: Accepted

Solution code:
# coding: utf-8

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

counter = {}
for a in ai:
    counter.setdefault(a-1, 0)
    counter.setdefault(a,   0)
    counter.setdefault(a+1, 0)
    
    counter[a-1] = counter[a-1] + 1
    counter[a  ] = counter[a  ] + 1
    counter[a+1] = counter[a+1] + 1

ans = 0
for key in counter.keys():
    ans = max(ans, counter[key])

print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict
def read(): return list(map(int, input().split()))


n = int(input())
a = read()

cnt = defaultdict(int)
for i in a:
    cnt[i - 1] += 1
    cnt[i] += 1
    cnt[i + 1] += 1

print(max(cnt.values()))

Submission status: Accepted

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

set_a=set(a)
cnt=[0]*(max(a)+1)
ans=0

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

if len(cnt) ==1:
    print(cnt[0])

elif len(cnt)==2:
    ans= cnt[0]+cnt[1]
    print(ans)

else:
    for i in range(1,max(a)):
        ans=max(ans,cnt[i-1]+cnt[i]+cnt[i+1])
    
    print(ans)

Submission status: Accepted

Solution code:
import sys

test =False
if test:
    N = 1
    items = [9999]
else:
    N = int(raw_input())
    items = raw_input().split()
    items = [int(item) for item in items]
max_count = 0
min_val = min(items)
max_val = max(items)
counts = {}
for i in items:
    counts[i - 1] = counts.get(i - 1, 0) + 1
    counts[i] = counts.get(i, 0) + 1
    counts[i + 1] = counts.get(i + 1, 0) + 1
print max(counts.values())

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# -*- coding:utf-8 -*-
N  = int(input())
a = list(map(int, input().split()))
b = [ 0 for _ in range((10**5)+1) ]
ans = float('-inf')
for tmp in a:
    if tmp != 0:
        b[tmp-1] += 1
    b[tmp] += 1
    b[tmp+1] += 1
for tmp in b:
    ans = max(ans, tmp)
print(ans)

Submission status: Accepted

Solution code:
N = input()
a = map(int, raw_input().split())
D = {}
for i in range(N):
    for d in range(-1, 2):
        k = a[i] + d
        if not k in D:
            D[k] = 0
        D[k] += 1
print max(D.values())

Submission status: Accepted

Solution code:
# encoding: "utf-8"

from numpy import array

class Stdin:
    @staticmethod
    def read_line(converter=str):
        return [converter(x) for x in input().split()]
    
    @staticmethod
    def read_lines(n, converter=str):
        result = list()
        for _ in range(n):
            result.append(Stdin.read_line(converter))
        return result
    
    @staticmethod
    def convert(data, converter):
        assert(len(data) == len(converter))
        return tuple(map(lambda x, f: f(x), data, converter))

    @staticmethod
    def convert_lines(datas, n, converter):
        return [converter(data, n, converter) for data in datas]


def main():
    _ = input()
    xs = Stdin.read_line(converter=int)
    ys = array(list(map(lambda x: [x-1, x, x+1], xs))).flatten()
    ys.sort()
    cnt = 0
    m = 0
    prev = ys[0]
    for y in ys:
        if y == prev:
            cnt += 1
        else:
            if m < cnt:
                m = cnt
            prev = y
            cnt = 1
    print(m)

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
# encoding: "utf-8"

import numpy as np

class Stdin:
    @staticmethod
    def read_line(converter=str):
        return [converter(x) for x in input().split()]
    
    @staticmethod
    def read_lines(n, converter=str):
        result = list()
        for _ in range(n):
            result.append(Stdin.read_line(converter))
        return result
    
    @staticmethod
    def convert(data, converter):
        assert(len(data) == len(converter))
        return tuple(map(lambda x, f: f(x), data, converter))

    @staticmethod
    def convert_lines(datas, n, converter):
        return [converter(data, n, converter) for data in datas]


def main():
    _ = input()
    xs = Stdin.read_line(converter=int)
    ys = np.array(list(map(lambda x: [x-1, x, x+1], xs))).flatten()
    ys.sort()
    cnt = 0
    m = 0
    prev = ys[0]
    for y in ys:
        if y == prev:
            cnt += 1
        else:
            if m < cnt:
                m = cnt
            prev = y
            cnt = 1
    print(m)

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
# encoding: "utf-8"

from collections import defaultdict
from functools import reduce
from itertools import groupby, chain

class Stdin:
    @staticmethod
    def read_line(converter=str):
        return [converter(x) for x in input().split()]
    
    @staticmethod
    def read_lines(n, converter=str):
        result = list()
        for _ in range(n):
            result.append(Stdin.read_line(converter))
        return result
    
    @staticmethod
    def convert(data, converter):
        assert(len(data) == len(converter))
        return tuple(map(lambda x, f: f(x), data, converter))

    @staticmethod
    def convert_lines(datas, n, converter):
        return [converter(data, n, converter) for data in datas]

def main():
    _ = input()
    xs = Stdin.read_line(converter=int)
    ys = chain.from_iterable([[x-1, x, x+1] for x in xs])
    ys = sorted(list(ys))
    m = max(map(len, group(ys)))
    print(m)

def group(xs):
    result = list()
    prev = xs[0]
    temp = list()
    for x in xs:
        if x == prev:
            temp.append(x)
        else:
            result.append(temp)
            prev = x
            temp = [x]
    return result

if __name__ == "__main__":
    main()