Problem:


Score : 300 points

Problem Statement

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

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

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

Maximize this count by making optimal choices.

Constraints

1≤N≤10^5

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

a_i is an integer.

Input

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

N
a_1 a_2 .. a_N

Output

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

Sample Input 1

7
3 1 4 1 5 9 2

Sample Output 1

4

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

Sample Input 2

10
0 1 2 3 4 5 6 7 8 9

Sample Output 2

3

Sample Input 3

1
99999

Sample Output 3

1



Sample input:
7
3 1 4 1 5 9 2


Sample output:
4

Submission status: Accepted

Solution code:
from bisect import*
n = int(input())
*a,=map(int,input().split())
a.sort()
ans = 0
for i in range(1,10**5):
	ans = max(ans,bisect(a,i+1)-bisect_left(a,i-1))
print(ans)

Submission status: Accepted

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

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

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

print(sorted(L)[-1])

Submission status: Accepted

Solution code:
#! /Library/Frameworks/Python.framework/Versions/3.7/bin/python3
N = int(input())
a = list(map(int, input().split()))
b = [0] * 100001  # 0から100000まで b[0]:0,b[1]:1…
c = [0] * 99999
for i in range(N):
    b[a[i]] += 1
for i in range(99999):  # 0から99998まで
    c[i] = b[i] + b[i + 1] + b[i + 2]
print(str(max(c)))

Submission status: Accepted

Solution code:
import bisect
import heapq
import itertools
import math
import operator
import os
import re
import string
import sys
from collections import Counter, deque, defaultdict
from copy import deepcopy
from decimal import Decimal
from fractions import gcd
from functools import lru_cache, reduce
from operator import itemgetter, mul, add, xor

import numpy as np

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(2147483647)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7

N = int(sys.stdin.readline())
A = list(map(int, sys.stdin.readline().split()))

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

print(max(counts.values()))

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
N = int(input())
A = list(map(int, input().split()))
cnt = [0] * (max(A) + 2)


for a in A:
    if a == 0:
        cnt[a] += 1
        cnt[a + 1] += 1
    else:
        cnt[a - 1] += 1
        cnt[a] += 1
        cnt[a+1] += 1

cnt.sort(reverse=True)
print(cnt[0])

Submission status: Accepted

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


for a in A:
    if a == 0:
        cnt[a] += 1
        cnt[a + 1] += 1
    else:
        cnt[a - 1] += 1
        cnt[a] += 1
        cnt[a+1] += 1

cnt.sort()
print(cnt[-1])

Submission status: Accepted

Solution code:
import statistics,collections

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

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

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

Submission status: Accepted

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

d = {}

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

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

d[-1] = 0
d[max(a)+1] = 0

ans = 0

for x in range(max(a)+1):
  if x-1 in d:
    ans = max(ans, d[x-1] + d[x] + d[x+1])

print(ans)

Submission status: Accepted

Solution code:
from collections import Counter


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

ret = 0

for i in cnt.keys():
    ret = max(cnt[i-1] + cnt[i] + cnt[i+1], ret)



print(ret)

Submission status: Accepted

Solution code:
N = int(input())

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

x = {}

for i in range(N):

    ai = a[i]

    temp = x.get(ai-1, 0)
    temp += 1
    x[ai-1] = temp

    temp = x.get(ai, 0)
    temp += 1
    x[ai] = temp

    temp = x.get(ai+1, 0)
    temp += 1
    x[ai+1] = temp

X = max(x.values())
print(X)

Submission status: Accepted

Solution code:
import collections


if __name__ == '__main__':
    n = int(input())
    a_list = list(map(int,input().split()))

    c = collections.Counter(a_list)

    dic_cnt = dict(c.most_common())

    answer = -1
    for k in dic_cnt.keys():
        cnt = dic_cnt[k]
        if k-1 in dic_cnt.keys():
            cnt += dic_cnt[k-1]
        if k+1 in dic_cnt.keys():
            cnt += dic_cnt[k+1]
        answer = max(answer,cnt)
    print(answer)

Submission status: Accepted

Solution code:
import collections


if __name__ == '__main__':
    n = int(input())
    A = list(map(int,input().split()))

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

    c = collections.Counter(tmp_A)
    most_common_element = c.most_common()[0][0]

    answer = 0
    for a in A:
        if a == most_common_element or a-1 == most_common_element or a+1 == most_common_element:
            answer += 1
    print(answer)

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,v in c.items():
    x=v+c[i-1]+c[i+1]
    ans=max(ans, x)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

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

a.sort()
h = {}
for i in range(a[-1]+1):
    h[i] = 0

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

ans = 0
for i in range(len(h)):
    x = h[i]
    if i > 0:
        x += h[i-1]
    if i < len(h)-1:
        x += h[i+1]
    ans = max(ans, x)
print(ans)

Submission status: Accepted

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

numCount = [0 for _ in range(10**5 + 1)]

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

ans = 0
for i in range(10**5 + 1):
    if ans < sum(numCount[i: i + 3]):
        ans = sum(numCount[i: i + 3])

print(ans)

Submission status: Accepted

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

d = {}
for i in range(n):
    if a[i] in d:
        d[a[i]] += 1
    else:
        d[a[i]] = 1 
    if a[i]-1 in d:
        d[a[i]-1] += 1
    else:
        d[a[i]-1] = 1
    if a[i]+1 in d:
        d[a[i]+1] += 1
    else:
        d[a[i]+1] = 1
maxc = 0
for i in d.keys():
    maxc = max(maxc,d[i])

print(maxc)

Submission status: Accepted

Solution code:
from collections import defaultdict

n = int(input())
a_list = list(map(int, input().split()))
dic = defaultdict(int)

for a in a_list:
    dic[a-1] += 1
    dic[a] += 1
    dic[a+1] += 1

print(max(dic.values()))

Submission status: Accepted

Solution code:
n = int(input())
raw = list(map(int,input().split()))
can = []
for i in range(n):
  now = raw[i]
  can.append(now-1)
  can.append(now)
  can.append(now+1)
can.sort()
ans = 0
for j in range(3*n):
  try:
    if can[j-1] == can[j]:
      cnt += 1
    else:
      cnt = 1
  except:
    #i==0
    cnt = 1
  if cnt > ans:
    ans = cnt
#print(can)
print(ans)

Submission status: Accepted

Solution code:
N = int(input())

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

nums = {}
for i in a:
  nums[i] = nums.get(i, 0) + 1
  nums[i+1] = nums.get(i+1, 0) + 1
  nums[i-1] = nums.get(i-1, 0) + 1

ans = 0
for i in nums.values():
  ans = max(ans, i)

print(ans)

Submission status: Accepted

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

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

a_sort = sorted(a_hash.items(), key = lambda x: x[0])
a_sort = list(a_sort)
ans_list = [0] * N

if len(a) == 1:
    print(1)
    exit()
if len(a_sort) == 1:
    print(a_sort[0][1])
    exit()

for i in range(len(a_sort)):
    if i == 0:
        if a_sort[0][0] == a_sort[1][0] - 1:
            ans_list[0] += a_sort[0][1] + a_sort[1][1]
    elif i == len(a_sort) -1:
        if a_sort[i][0] == a_sort[i-1][0] + 1:
            ans_list[i] += a_sort[i][1] + a_sort[i-1][1]
        else:
            ans_list[i] = a_sort[i][1]
    else:
        if a_sort[i][0] == a_sort[i-1][0] + 1:
            ans_list[i] += a_sort[i][1] + a_sort[i-1][1]
        if a_sort[i][0] == a_sort[i+1][0] - 1:
            ans_list[i] += a_sort[i+1][1]


print(max(ans_list))

Submission status: Accepted

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

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

ans = 0
for x in d:
    cand = d[x]
    if x - 1 in d:
        cand += d[x - 1]
    if x + 1 in d:
        cand += d[x + 1]
    
    ans = max(ans, cand)

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
a.sort()
a.append(a[-1] + 1)

b = [a[0]]
c = [1]
ans = 0
for i in range(1, n+1):
    if a[i] == b[-1]:
        c[-1] += 1
    else:
        cand = c[-1]
        if len(b) >= 2 and b[-2] == b[-1] - 1:
            cand += c[-2]
        if len(b) >= 3 and b[-3] == b[-1] - 2:
            cand += c[-3]
        ans = max(ans, cand)
        b.append(a[i])
        c.append(1)

print(ans)

Submission status: Accepted

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

ans = [0 for i in range(max(a)+3)]
for i in range(n):
    ans[a[i]] += 1
    ans[a[i]+1] += 1
    ans[a[i]+2] += 1
print(max(ans))

Submission status: Accepted

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

Submission status: Accepted

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

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

for i in range(n):
    k = lst[i]
    lst.append(k-1)
    lst.append(k+1)

c = Counter(lst)
print(c.most_common()[0][1])

Submission status: Accepted

Solution code:
import collections

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

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

b = collections.Counter(a)
print(b.most_common()[0][1])

Submission status: Accepted

Solution code:
N = int(input())
a = list(map(int, input().split()))
a.sort()
a_dic = {}
for i in range(N):
    n = a[i]
    a_dic.setdefault(n, 0)
    a_dic[n] += 1
maximum = 0
for j in a_dic:
    tmp = a_dic[j] + a_dic.get(j-1,0) + a_dic.get(j+1,0)
    if tmp > maximum:
        maximum = tmp
print(maximum)

Submission status: Accepted

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

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

tmp = l[0] + l[1] + l[2]
ans = tmp
for i in range(3, MAX_a):
    tmp = tmp + l[i] - l[i-3]
    ans = max(tmp , ans)

print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys, math, fractions, itertools, collections


def solve(N: int, a: "List[int]"):
    a.sort()
    c = collections.Counter(a)
    cc = dict(c.items())
    mx = 0
    for i in range(a[0], a[-1]+1):
        counter = 0
        for j in range(i-1, i+2):
            if j in c.keys():
                counter += c[j]
        mx = max(counter, mx)
    print(mx)
    return


# Generated by 1.1.4 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    a = [ int(next(tokens)) for _ in range(N) ]  # type: "List[int]"
    solve(N, a)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import sys
input = sys.stdin.readline
a = int(input())
b = list(map(int,input().split()))
from collections import defaultdict
d = defaultdict(int)
for i in b:
    d[i]+=1
    d[i-1]+=1
    d[i+1]+=1

ret_v = 0
for v in d.values():
    if v > ret_v:
        ret_v = v
print(ret_v)

Submission status: Accepted

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


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

print(max(c))

Submission status: Accepted

Solution code:
N = int(input())
A = [int(x) for x in input().split()]

# N < 10^5
d = {}
for a in A:
    d[a+1] = d.get(a+1,0)+1
    d[a]   = d.get(a,0)+1
    d[a-1] = d.get(a-1,0)+1

print(max(d.values()))

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
from bisect import bisect_left
INF = float("inf")


def solve(N: int, a: "List[int]"):
    a.sort()
    lower, upper = 0, 2
    m = 1
    while lower < 10**5:
        m = max(m, bisect_left(a, upper+1)-bisect_left(a, lower))
        lower += 1
        upper += 1
    print(m)

    return


def main():

    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    a = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    solve(N, a)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

b = [0]*(10**5+1)
for i in a:
    if i == 10**5:
        b[i-1] += 1
        b[i] += 1
    elif i == 0:
        b[i] += 1
        b[i+1] += 1
    else:
        b[i-1] += 1
        b[i] += 1
        b[i+1] += 1

print(max(b))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import sys
import copy
import collections
from bisect import bisect_left
from bisect import bisect_right
from collections import defaultdict
from heapq import heappop, heappush
import math
import itertools
 
# NO, PAY-PAY
#import numpy as np
#import statistics
#from statistics import mean, median,variance,stdev
 
def inputInt(): return int(input())
def inputMap(): return map(int, input().split())
def inputList(): return list(map(int, input().split()))
 
def main():
    N = inputInt()
    a = inputList()
    ans = {}
    
    for i, val in enumerate(a):
        if val in ans:
            ans[val] += 1
        else:
            ans[val] = 1
            
        if val+1 in ans:
            ans[val+1] += 1
        else:
            ans[val+1] = 1
            
        if val-1 in ans:
            ans[val-1] += 1
        else:
            ans[val-1] = 1
            
    tmp = 0
    for i in ans.values():
        if tmp < i:
            tmp = i
            
    print(tmp)
    
    
if __name__ == "__main__":
	main()

Submission status: Accepted

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

for num in A:
  if num == 0:
    nums[0] += 1
    nums[1] += 1
  else:
    nums[num-1] += 1
    nums[num] += 1
    nums[num+1] += 1
print(max(nums))

Submission status: Accepted

Solution code:
import collections


def main():
    N = int(input())
    a = [0] * (N * 3)
    i = 0
    for ele in input().split():
        ele = int(ele)
        a[i] = ele-1
        i += 1
        a[i] = ele
        i += 1
        a[i] = ele+1
        i += 1

    ctr = collections.Counter(a)
    print(max(list(ctr.values())))


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
N = int(input())
A = [int(i) for i in input().split()]

A_dict = {}
for a in A:
  A_dict[a] = A_dict.get(a,0) + 1
  A_dict[a+1] = A_dict.get(a+1,0) + 1
  A_dict[a-1] = A_dict.get(a-1,0) + 1

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

Submission status: Accepted

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

Submission status: Accepted

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

    cnt = [0] * 100002

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

main()

Submission status: Accepted

Solution code:
N  = int(input())
count = [0] * 100001
a = list(map(int, input().split()))

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

ans = max(count)
print(ans)

Submission status: Accepted

Solution code:
N=int(input())
A=list(map(int, input().split()))
B=[0]*100002
for a in A:
  B[a]+=1
ans=0
for i in range(1,100001):
  ans=max(ans,B[i-1]+B[i]+B[i+1])
print(ans)

Submission status: Accepted

Solution code:
import bisect

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

num = []
for i in range(min(a), max(a) - 1):
    num.append(bisect.bisect_right(a, i + 2) - bisect.bisect_left(a, i))
if len(num) == 0:
    print(n)
else:
    print(max(num))

Submission status: Accepted

Solution code:
from bisect import bisect_left, bisect_right

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

A.sort()

ans = 0
for a in range(100000):
    k = bisect_right(A, a+2) - bisect_left(A, a)
    ans = max(ans, k)

print(ans)

Submission status: Accepted

Solution code:
from collections import Counter

N = int(input())
a = [int(i) for i in input().split()]
number_counter = [list(i) for i in sorted(list(Counter(a).most_common()), key=lambda x: x[0])]
if len(number_counter) == 1:
  number_counter = [[10 ** 6, 0]] + number_counter + [[10 ** 6, 0]]
elif len(number_counter) == 2:
  number_counter += [[10 ** 6, 0]]

count = 0 
for i in range(1, len(number_counter) - 1):
  next_count = number_counter[i][1]
  number = number_counter[i][0]
  if number_counter[i - 1][0] + 1 == number:
    next_count += number_counter[i - 1][1]
  if number_counter[i + 1][0] - 1 == number:
    next_count += number_counter[i + 1][1]
  if next_count > count:
    count = next_count

print(count)

Submission status: Accepted

Solution code:
t=eval(input())
l=list(map(int,input().split()))
L=[0 for i in range(1000000+3)]
for i in range(t):
    L[l[i]]+=1
    L[l[i] + 1] += 1
    L[l[i] - 1] += 1
print(max(L))

Submission status: Accepted

Solution code:
#!usr/bin/env python3
from collections import defaultdict
from collections import deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
import itertools
sys.setrecursionlimit(10**5)
stdin = sys.stdin
def LI(): return list(map(int, stdin.readline().split()))
def LF(): return list(map(float, stdin.readline().split()))
def LI_(): return list(map(lambda x: int(x)-1, stdin.readline().split()))
def II(): return int(stdin.readline())
def IF(): return float(stdin.readline())
def LS(): return list(map(list, stdin.readline().split()))
def S(): return list(stdin.readline().rstrip())
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007

#A
def A():
    x, t = LI()
    print(max(0, x-t))
    return

#B
def B():
    s = S()
    print("".join(s[::2]))
    return

#C
def C():
    II()
    a = LI()
    lis = [0] * (max(a) + 3)
    for ai in a:
        lis[ai - 1] += 1
        lis[ai] += 1
        lis[ai + 1] += 1
    print(max(lis))
    return

#D
def D():
    return

#Solve
if __name__ == '__main__':
    C()

Submission status: Accepted

Solution code:
#!usr/bin/env python3
from collections import defaultdict
from collections import deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
import itertools
sys.setrecursionlimit(10**5)
stdin = sys.stdin
def LI(): return list(map(int, stdin.readline().split()))
def LF(): return list(map(float, stdin.readline().split()))
def LI_(): return list(map(lambda x: int(x)-1, stdin.readline().split()))
def II(): return int(stdin.readline())
def IF(): return float(stdin.readline())
def LS(): return list(map(list, stdin.readline().split()))
def S(): return list(stdin.readline().rstrip())
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007

#A
def A():
    x, t = LI()
    print(max(0, x-t))
    return

#B
def B():
    s = S()
    print("".join(s[::2]))
    return

#C
def C():
    II()
    a = LI()
    lis = [0] * (10**5+2)
    for ai in a:
        lis[ai - 1] += 1
        lis[ai] += 1
        lis[ai + 1] += 1
    print(max(lis))
    return

#D
def D():
    return

#Solve
if __name__ == '__main__':
    C()

Submission status: Accepted

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

a=sorted(a)
a.append(a[-1]+1)
arr=[]
tmp=a[0]
cnt=0
for i in range(N+1):
    if a[i]==tmp:
        cnt+=1
    elif a[i]==tmp+1:
        arr.append(cnt)
        tmp=a[i]
        cnt=1
    else:
        arr.append(cnt)
        step=a[i]-tmp
        for _ in range(step-1):
            arr.append(0)
        tmp=a[i]
        cnt=1
num=0
if len(arr)>=3:
    for i in range(len(arr)-2):
        num=max(num,sum(arr[i:i+3]))
else:
    num=sum(arr)
print(num)

Submission status: Accepted

Solution code:
N = int(input())
A = [0]*(10**5+2)
a = [int(i) for i in input().split()]

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
a = list(map(int,input().split()))
A = []
for b in a:
    A.append(b-1)
    A.append(b)
    A.append(b+1)

A.sort()
count = 1
maxcount =1
for i in range(1,len(A)):
    if A[i] == A[i-1]:
        count = count+1
    else:
        maxcount = max(maxcount,count)
        count = 1

print(maxcount)

Submission status: Accepted

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

a_c = Counter(sorted(a))
a_v = list(a_c.values())
a_k = list(a_c.keys())
count_max = a_v[0]
for i in range(1, len(a_v)):
    count = 0
    if a_k[i] - a_k[i - 1] == 1: 
        count += a_v[i - 1]
    if i < (len(a_v) - 1) and a_k[i + 1] - a_k[i] == 1:
        count += a_v[i + 1]
    count += a_v[i]
    if count > count_max:
        count_max = count
print(count_max)

Submission status: Accepted

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

a_sorted = sorted(a)
a_v = list(Counter(a_sorted).values())
a_k = list(Counter(a_sorted).keys())
count_max = a_v[0]
for i in range(1, len(a_v)):
    count = 0
    if a_k[i] - a_k[i - 1] == 1: 
        count += a_v[i - 1]
    if i < (len(a_v) - 1) and a_k[i + 1] - a_k[i] == 1:
        count += a_v[i + 1]
    count += a_v[i]
    if count > count_max:
        count_max = count
print(count_max)

Submission status: Accepted

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

data = [0]*(100010)
for i in l:
  data[i-1] += 1
  data[i] += 1
  data[i+1] += 1

print(max(data))

Submission status: Accepted

Solution code:
# ABC 72, C - together
from collections import Counter

n = int(input())
A = [int(el) for el in input().split(' ')]
C = Counter(A)
max_count = 0
for x in range(min(A), max(A)+1):

    count = C[x-1] + C[x] + C[x+1]

    if count > max_count:
        max_count = count
print(max_count)

Submission status: Accepted

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

from itertools import accumulate

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

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

for i in a:
    x[i] += 1
    x[i+3] -= 1

x = list(accumulate(x))
print(max(x))

Submission status: Accepted

Solution code:
from collections import Counter

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

A = [0] * (3 * N)
for i in range(N):
    A[3 * i] = a[i] - 1
    A[3 * i + 1] = a[i]
    A[3 * i + 2] = a[i] + 1

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import defaultdict

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

D = defaultdict(lambda: 0)
for a in A:
    D[a] += 1

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

Submission status: Accepted

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

b = [0]*100003
for i in a: b[i]+=1
ans = 0
for i in range(100000): ans=max(ans, sum(b[i:i+3]))
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

import sys

sys.setrecursionlimit(10**7)


def read_h(typ=int):
    return list(map(typ, input().split()))


def read_v(n, m=1, typ=int):
    return [read_h() if m > 1 else typ(input()) for _ in range(n)]


def main():
    N, = read_h()
    arr = read_h()

    cnts = [0] * 100000
    for a in arr:
        cnts[a] += 1

    ans = 0
    for i in range(len(cnts) - 2):
        c = cnts[i] + cnts[i + 1] + cnts[i + 2]
        if c > ans:
            ans = c

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

import sys

sys.setrecursionlimit(10**7)


def read_h(typ=int):
    return list(map(typ, input().split()))


def read_v(n, m=1, typ=int):
    return [read_h() if m > 1 else typ(input()) for _ in range(n)]


def main():
    N, = read_h()
    arr = read_h()

    from collections import Counter
    c = Counter(arr)

    cnt = 0
    for X in c.keys():
        cnt = max(cnt, c.get(X - 1, 0) + c.get(X, 0) + c.get(X + 1, 0))

    print(cnt)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N = int(input())
a = list(map(int, input().split()))
a.sort()
max_count = 0
count = 0
i = 0
x_before = -1
while i < N:
    x = a[i]
    j = i
    if x_before != x:
        while j < len(a):
            if x == a[j] or x+1 == a[j] or x+2 == a[j]:
                count += 1
                j += 1
            else:
                break
        if max_count < count:
            max_count = count
    x_before = x
    count = 0
    i += 1
print(max_count)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
input()
a = list(map(int,input().split()))
l = [0] * 100000
for c in a:
    if c-1<0:pass
    else:l[c-1]+=1
    l[c] += 1
    if c+1>99999:pass
    else:l[c+1]+=1
print(max(l))

Submission status: Accepted

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

N = int(input())
L = [int(v) for v in input().split()]
ans = {}

for v in L:
    a, b, c = v - 1, v, v + 1
    for p in [a, b, c]:
        if ans.get(p) == None:
            ans[p] = 0
        ans[p] += 1

print(max(ans.values()))

Submission status: Accepted

Solution code:
import collections

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

a_counter = collections.Counter(a)
ret = 0
for i in range(1,10**5+1):

    tmp = a_counter[i-1] + a_counter[i] + a_counter[i+1]
    ret = max(tmp,ret)

print(ret)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(raw_input())
arr = map(int, raw_input().split())
freq = [0]*(10**5)
for a in arr:
    freq[a] += 1
count = 0
for i in range(10**5):
    if i == 0:
        count = freq[i] + freq[i+1]
    elif i == 10**5-1:
        count = max(count, freq[i] + freq[i-1])
    else:
        count = max(count, freq[i-1]+freq[i]+freq[i+1])
print count

Submission status: Accepted

Solution code:

N = int(input())

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


a.sort()

L2 = -2
L1 = -2
now = -2 #番号

N2 = 0
N1 = 0
N0 = 0 #個数

ans = 0


for i in a:

    if i != now:

        if now+1 == i:

            L2 = L1
            N2 = N1
            L1 = now
            N1 = N0

            now = i
            N0 = 1

        else:

            L2 = -2
            L1 = -2
            N2 = 0
            N1 = 0

            now = i
            N0 = 1

    else:

        N0 += 1

    ans = max(ans , N0 + N1 + N2)

print (ans)

Submission status: Accepted

Solution code:
n=int(input())
l=[0]*(10**5+2)
a=list(map(int,input().split()))
for i in range(len(a)):
    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()))

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

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

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

print(max(ruiseki))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())

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

buff = [0 for i in range(10**5)]

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


ans = 0
# print(buff)
for i in range(N-2):
    ans = max(ans,buff[i]+buff[i+1]+buff[i+2])

ans = max(ans, buff[N-2])
ans = max(ans, buff[N-1])
print(ans)

Submission status: Accepted

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

count = [0] * 200000

for x in a:
    count[x] += 1

y = 0
for i in range(1, 100000):
    x = count[i - 1] + count[i] + count[i + 1]
    if x > y:
        y = x

print(y)

Submission status: Accepted

Solution code:
N = int(input())
a = list(map(int,input().split()))
L = [0]*(10**5+1)
for i in a:
    L[i] += 1
hoge = sum(L[:3])
ans = hoge
for i in range(10**5-2):
    hoge += L[i+3]-L[i]
    ans = max(ans,hoge)
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] += 1
    cnt[v] += 1
    cnt[v+1] += 1

print(max(cnt))