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: Runtime Error

Solution code:
// 072-c
// +1,0,-1を全てmapに足し、最大値を出す


#include <iostream>
#include <string>
#include <vector>
#include <math.h>
#include <stdio.h>
#include <algorithm>
#include <utility>
#include <functional>
#include <map>
#include <set>
#include <queue>
#include <list>
#define rep(i,n) for(int i=0;i<(n);i++)
#define REP(i,a,b) for(int i=int(a);i<int(b);++i)
#define crep(i) for(char i='a';i<='z';i++)
#define psortsecond(A,N) sort(A,A+N,[](const pii &a, const pii &b){return a.second<b.second;});
#define psortfirst(A,N) sort(A,A+N,[](const pii &a, const pii &b){return a.first<b.first;});
#define ALL(x) (x).begin(),(x).end()
int ctoi(const char c){
  if('0' <= c && c <= '9') return (c-'0');
  return -1;
}
using namespace std;
using pii  = pair<int,int>;
long long gcd(long long a, long long b){return (b == 0 ? a : gcd(b, a%b));}
long long lcm(long long a, long long b){return a*b/gcd(a,b);}
typedef long long ll;
#define MOD 1000000007
#define EPS 10e-8

map<ll,ll> M;

int main(){
  ll N,A[100007],ma=0,kazu;cin >> N;
  rep(i,N){
    cin >> A[i];
    if(A[i]!=0){
      M[A[i]]++;
      M[A[i]-1]++;
      M[A[i]+1]++;
    }
    else{
      M[A[i]]++;
      M[A[i]+1]++;
    }
  }
  for(auto P :M){
    if(ma<P.second){
      ma=P.second;
    }
  }
  cout << ma << endl;
}

Submission status: Runtime Error

Solution code:
C=[0]*100002
for i in map(int,input().split()):
  C[i-1],C[i],C[i+1]+=1
print(max(C))

Submission status: Runtime Error

Solution code:
from collections import*
import sys
sys.stdin=input
n=int(input())
a=list(map(int,input().split()))
a=Counter(a)
cnt=1
for i in range(min(a),max(a)+1):
	cnt_=a[i-1]+a[i]+a[i+1]
	if cnt_>cnt:cnt=cnt_
print(cnt)

Submission status: Runtime Error

Solution code:
import collections

N = int(input())
a = list(map(int,input().split()))
b = collections.Counter(a)
dp = [0] * (max(a)+1)
dp[0] = b[0] + b[1]
dp[1] = dp[0] + b[2]
for i in range(2, max(a)+1):
    dp[i] = dp[i-1] + b[i+1] - b[i-2]
print(max(dp))

Submission status: Runtime Error

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

def main():
    N = int(input())
    a = list(map(int,input().split()))
    max_a = max(a)
    count = [0 for _ in range(max_a)]
    c = collections.Counter(a)
    c = list(c.items())

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

    elif len(c) == 2:
        if abs(c[0][0]-c[1][0]) <= 2:
            print(c[0][1]+c[1][1])
        else:
            print(max(c[0][1],c[1][1]))

    else:
        for i in range(len(c)):
            count[c[i][0]-1] = c[i][1]

        sum_list = [0 for _ in range(len(c)-2)]
        for i in range(len(c)-2):
            sum_list[i] = count[i]+count[i+1]+count[i+2]
        print(max(sum_list))

if __name__ == "__main__":
    main()

Submission status: Runtime Error

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

def main():
    N = int(input())
    a = list(map(int,input().split()))
    max_a = max(a)
    count = [0 for _ in range(max_a)]
    c = collections.Counter(a)
    c = list(c.items())

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

    elif len(c) == 2:
        if abs(c[0][0]-c[1][0]) <= 2:
            print(c[0][1]+c[1][1])
        else:
            print(max(c[0][1],c[1][1]))

    else:
        for i in range(len(c)):
            count[c[i][0]-1] = c[i][1]

        sum_list = [0 for _ in range(len(c)-2)]
        for i in range(len(c)-2):
            sum_list[i] = count[i]+count[i+1]+count[i+2]
        print(sum_list)

if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
N=int(input())
a=list(map(int,input().split()))
ans=0
for i in a:
    ans=max(ans,sa.count(i-1)+sa.count(i)+sa.count(i+1))
print(ans)

Submission status: Runtime Error

Solution code:
import collections
N = int(input())
a = list(map(int, input().split()))
cand = collections.Counter(a)
cand_list = cand.most_common(len(set(a)))
ans = 0
n = min(len(a),5)
for i in range(n):
  num = cand_list[i][0]
  count = cand[num]
  if num + 1 in a:
    count += cand[num + 1]
  if num - 1 in a:
    count += cand[num - 1]
  if ans < count:
    ans = count

print(ans)

Submission status: Runtime Error

Solution code:
n = int(input())
A = list(map(int,input().split()))
l = []
for i in range(min(A),max(A)):
  cnt = 0
  for a in A:
    if i == a or i == a-1 or i == a+1:
      cnt += 1
  l.append(cnt)
print(max(l))

Submission status: Runtime Error

Solution code:
import collections
N =  int(input())
a = list(map(int,input().split()))
a = collections.Counter(a)
a = sorted(a.items())
li = [0]*N
tmp = 0
if N==1:
    print(1)
    exit()

for i in range(len(a)-1):
    tmp += a[i][1]
    if a[i][0]-1==a[i-1][0]:
        tmp += a[i-1][1]
    if a[i][0]+1==a[i+1][0]:
        tmp += a[i+1][1]
    li[i] = tmp
    tmp = 0

if a[-1][0]-1==a[-2][0]:
    li.append(a[-1][1]+a[-2][1])
print(max(li))

Submission status: Runtime Error

Solution code:
import collections
N =  int(input())
a = list(map(int,input().split()))
a = collections.Counter(a)
a = sorted(a.items())
li = [0]*N
tmp = 0
if N==1:
    print(1)
    exit()

for i in range(len(a)-1):
    tmp += a[i][1]
    if a[i][0]-1==a[i-1][0]:
        tmp += a[i-1][1]
    if a[i][0]+1==a[i+1][0]:
        tmp += a[i+1][1]
    li[i] = tmp
    tmp = 0
if a[-1][0]-1==a[-2][0]:
    li[-1] = a[-1][1]+a[-2][1]
print(max(li))

Submission status: Runtime Error

Solution code:
n = int(input())
num_list = [ int(v) for v in input().split() ]

ans_list = [ 0 for i in range(n+2) ]

for i in num_list:
    ans_list[i] += 1
    ans_list[i+1] += 1
    ans_list[i+2] += 1

print(max(ans_list))

Submission status: Runtime Error

Solution code:
import statistics
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)

print(ans.count(statistics.mode(ans)))

Submission status: Runtime Error

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()

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: Runtime Error

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

b = [0]*(10**5)
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: Runtime Error

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

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

print(max(data))

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
N = int(input())

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

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

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])

print(ans)

Submission status: Runtime Error

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

Submission status: Runtime Error

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

print(max(cnt))

Submission status: Runtime Error

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

print(max(cnt))

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
# ハッシュで数えてキーでソートして+-1を全探索
n = int(input())
arr = list(map(int, input().split()))
hash = dict()
for a in arr:
    if a in hash:
        hash[a] += 1
    else:
        hash[a] = 1

lis = list(sorted(hash.items(), key=lambda x: x[0]))

ans = 0
for i in range(1, n - 2):
    temp = lis[i][1]
    if lis[i - 1][0] + 1 == lis[i][0]:
        temp += lis[i - 1][1]
    if lis[i + 1][0] - 1 == lis[i][0]:
        temp += lis[i + 1][1]
    ans = max(ans, temp)

print(ans)

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
import numpy as np
N = int(input())
A = np.array(input().split(), dtype = np.int32)

counter = np.bincount(A)
answer = (counter[2:] + counter[1:-1] + counter[:-2]).max()
print(answer)

Submission status: Runtime Error

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

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

  
print(max(ans))

Submission status: Runtime Error

Solution code:
N = int(input())

tmp = input().split(" ")

als = [0]*(10**5)

for n in tmp:
    nowNum = int(n)
    als[nowNum - 1] += 1
    als[nowNum] += 1
    als[nowNum + 1] += 1


    
print(max(als))

Submission status: Runtime Error

Solution code:
from collections import Counter
N = int(input())
a = list(map(int, input().split()))
i_list = []
#return tuple list: Counter().most_common()                                                                          for i in a:
    i_list.append(i-1)
    i_list.append(i)
    i_list.append(i+1)
print(Counter(i_list).most_common()[0][1])

Submission status: Runtime Error

Solution code:
from collections import Counter
N = int(input())
a = list(map(int, input().split()))
i_list = []
#return tuple list: Counter().most_common()                                                                          for i in a:
    i_list.append(i-1)
    i_list.append(i)
    i_list.append(i+1)
print(i_list)
print(Counter(i_list).most_common()[0][1])

Submission status: Runtime Error

Solution code:
N = int(input())
a = list(map(int, input().split()))
i_list = []
for x in range(max(a)):
    cnt = 0
    for i in range(N):
        if abs(x - a[i]) == 1 or x == a[i]:
            cnt += 1
    i_list.append(cnt)
print(max(i_list))

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
n=int(input())
a=list(map(int,input().split()))
m=max(a)
cou=[0]*m
for i in range(m):
  cou[i]=a.count(i-1)+a.count(i)+a.count(i+1)
print(max(cou))

Submission status: Runtime Error

Solution code:
# 空の配列を用意して各要素の-1,0,+1を試して
# 値のインデックスに+1して最大値をとる

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

cnt = [0] * 100000

for i in a:
    for j in [-1,0,1]:
        cnt[i+j] += 1

print(max(cnt))

Submission status: Runtime Error

Solution code:
n = int(input())

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

unique = []

seq = sorted(seq)
c = 0
k = seq[0]

for i in seq:
    if not i in unique:
        unique.append(i)

unique_n = [0]*len(unique)

for i in seq:
    if i == k:
        unique_n[c] += 1
    
    else:
        c += 1
        unique_n[c] += 1





if n == 1:
    print(1)

elif n == 2:
    if max(seq) - min(seq) <= 2:
        print(2)
    
    else:
        print(1)

else:
    max_n = 0
    
    for i in range(1,len(unique)-2):
        cnt = 0
        cnt += unique_n[i-1]
        cnt += unique_n[i]
        cnt += unique_n[i+1]
    
        if cnt > max_n:
            max_n = cnt

    
    print(max_n)

Submission status: Runtime Error

Solution code:
from collections import Counter

N, *a = map(int, open(0).read().split())
c = [y for _, y in sorted(Counter(a).most_common())]
print(max(sum(c[i:i + 3]) for i in range(len(c) - 2)))

Submission status: Runtime Error

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

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

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

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

print(ans)

Submission status: Runtime Error

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

imode = statistics.mode(a)
num_ofmode = a.count(imode)

print(max(a.count(imode-2) + a.count(imode-1) + num_ofmode, a.count(imode-1) + num_ofmode + a.count(imode+1), num_ofmode + a.count(imode+1) + a.count(imode+2)))

Submission status: Runtime Error

Solution code:
#ABC 072 C
N=int(input())
A=[int(j) for j in input().split()]
cnt=[0]*(10**5)
for a in A:
    cnt[a]+=1
    cnt[a+1]+=1
    cnt[a+2]+=1
print(max(cnt))

Submission status: Runtime Error

Solution code:
import statistics
n = int(input())
A = list(map(int,input().split()))
B = [A[i]-1 for i in range(n)] +  A + [A[i]+1 for i in range(n)]
ans = statistics.mode(B)
print(B.count(ans))

Submission status: Runtime Error

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

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

for i in a:
    c[i] += 1
    if i != 0 and i != 10 ** 5:
        c[i-1] += 1
        c[i+1] += 1
    elif i == 0:
        c[i+1] += 1
    else:
        c[i-1] += 1

print(max(c))

Submission status: Runtime Error

Solution code:
n=int(input())
a=list(map(int,input().split()))
cntans=cntnow=0
for i in range(min(a),max(a)):
  for j in range(n):
    if abs(a[j]-i)<=1:
      cntnow+=1
      if cntnow==n:
        print(cntnow)
        sys.exit()
  cntans=max(cntans,cntnow)
  cntnow=0
print(cntans)

Submission status: Runtime Error

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

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

a=collections.Counter(A)

X=sorted(a.items(), key=lambda x:x[0])

K=[]
V=[]

RES=[]
for i in range(len(X)):
    K.append(X[i][0])
    V.append(X[i][1])
    
for j in range(1,len(X)-1):
    res=V[j]
    if K[j]-1==K[j-1]:
        res+=V[j-1]
    if K[j]+1==K[j+1]:
        res+=V[j+1]
    RES.append(res)

print(max(RES))

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
n = int(input())

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

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

for i in range(n):
    if a[i] == 0:
        b[a[i]] += 1
        b[a[i]+1] += 1
    else:
        b[a[i]-1] += 1
        b[a[i]] += 1
        b[a[i]+1] += 1
print(max(b))

Submission status: Runtime Error

Solution code:
n=int(input())
a=list(map(int,input().split()))
a.sort()
ans = [1] * len(set(a))
index=0
for i in range(n):
    ans[index] = a.count(a[i]-1)+a.count(a[i])+a.count(a[i]+1)
    index+=1
print(max(ans))

Submission status: Runtime Error

Solution code:
#072-C
n=I()
a=IL()
cnt=[0 for i in range(pow(10,5))]
for i in range(n):
    cnt[a[i]]+=1
m=0
for i in range(2,pow(10,5)):
    m=max(cnt[i]+cnt[i-1]+cnt[i-2],m)
print(m)

Submission status: Runtime Error

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

Submission status: Runtime Error

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

count = {}
for ai in a:
  count[ai] = count.get(ai, 0) + 1

ans = 0
for key in count.keys():
  ans = max(ans, count.get(key - 1) + count.get(key) + count.get(key + 1))
print(ans)

Submission status: Runtime Error

Solution code:
from collections import Counter
n=int(input())
A=Counter(list(map(int,input().split()))).most_common()
if A[0][0]==0:
  if A[0][1]==A[1][1]:
    max_num=A[1][0]
else:
  max_num=A[0][0]
kouho=[max_num-1,max_num,max_num+1]
cnts={max_num-1:0,max_num:0,max_num+1:0}
for i in A:
  if i[0]-1 in cnts:
    cnts[i[0]-1]+=i[1]
  if i[0] in cnts:
    cnts[i[0]]+=i[1]
  if i[0]+1 in cnts:
    cnts[i[0]+1]+=i[1]
print(max(cnts.values()))

Submission status: Runtime Error

Solution code:
#愚直にやる方法
#C問題
N=int(input())
a=list(map(int,input().split()))
Xmax=max(a)
print(max([a.count(X-1)+a.count(X)+a.count(X+1) for X in range(1,Xmax)]))

Submission status: Runtime Error

Solution code:
#C問題
N=int(input())
a=list(map(int,input().split()))
a.sort()
a_sabun=[a[i+1]-a[i] for i in range(0,N-1)]
b=1
B=[]
C=[]
for i in range(0,N-1):
    if a_sabun[i]==0:
        b=b+1
    else:
        B.append(b)
        C.append(a_sabun[i])
        b=1
B.append(b)
#print(B)
#print(C)
D=[]
e=len(C)
for i in range(0,e-1):
    if C[i]>2:
        D.append(B[i])
    elif C[i]+C[i+1]>2:
        D.append(B[i]+B[i+1])
    else:
        D.append(B[i]+B[i+1]+B[i+2])
#print(D)
if N==1:
    print(1)
elif C[e-1]<=2:
    D.append(B[e-1]+B[e])
    print(max(D))
else:
    D.append(B[e-1])
    D.append(B[e])
    print(max(D))
    

#時間的には問題ないはず
#これでもダメ

Submission status: Runtime Error

Solution code:
#C問題
N=int(input())
a=list(map(int,input().split()))
a.sort()
a_sabun=[a[i+1]-a[i] for i in range(0,N-1)]
b=1
B=[]
C=[]
for i in range(0,N-1):
    if a_sabun[i]==0:
        b=b+1
    else:
        B.append(b)
        C.append(a_sabun[i])
        b=1
B.append(b)
#print(B)
#print(C)
D=[]
#e=len(C)
for i in range(0,e-1):
    if C[i]>2:
        D.append(B[i])
    elif C[i]+C[i+1]>2:
        D.append(B[i]+B[i+1])
    else:
        D.append(B[i]+B[i+1]+B[i+2])
#print(D)
if N==1:
    print(1)
elif C[e-1]<=2:
    D.append(B[e-1]+B[e])
    print(max(D))
else:
    D.append(B[e-1])
    D.append(B[e])
    print(max(D))
    

#時間的には問題ないはず
#これでもダメ

Submission status: Runtime Error

Solution code:
#C問題
N=int(input())
a=list(map(int,input().split()))
a.sort()
a_sabun=[a[i+1]-a[i] for i in range(0,N-1)]
b=1
B=[]
C=[]
for i in range(0,N-1):
    if a_sabun[i]==0:
        b=b+1
    else:
        B.append(b)
        C.append(a_sabun[i])
        b=1
B.append(b)
#print(B)
#print(C)
D=[]
e=len(C)
for i in range(0,e-2):
    if C[i]>2:
        D.append(B[i])
    elif C[i]+C[i+1]>2:
        D.append(B[i]+B[i+1])
    else:
        D.append(B[i]+B[i+1]+B[i+2])
#print(D)
if N==1:
    print(1)
elif C[e-1]<=2:
    D.append(B[e-1]+B[e])
    print(max(D))
else:
    D.append(B[e-1])
    D.append(B[e])
    print(max(D))
    

#時間的には問題ないはず
#これでもダメ

Submission status: Runtime Error

Solution code:
#C問題
N=int(input())
a=list(map(int,input().split()))
a.sort()
a_sabun=[a[i+1]-a[i] for i in range(0,N-1)]
b=1
B=[]
C=[]
for i in range(0,N-1):
    if a_sabun[i]==0:
        b=b+1
    else:
        B.append(b)
        C.append(a_sabun[i])
        b=1
B.append(b)
D=[]
e=len(C)
for i in range(0,e-1):
    if C[i]>2:
        D.append(B[i])
    elif C[i]+C[i+1]>2:
        D.append(B[i]+B[i+1])
    else:
        D.append(B[i]+B[i+1]+B[i+2])
if N==1:
    print(1)
elif C[e-1]>2:
    D.append(B[e-1]+B[e])
    print(max(D))
else:
    D.append(B[e-1])
    D.append(B[e])
    print(max(D))
    

#時間的には問題ないはず

Submission status: Runtime Error

Solution code:
#C問題
N=int(input())
a=list(map(int,input().split()))
a.sort()
a_sabun=[a[i+1]-a[i] for i in range(0,N-1)]
b=1
B=[]
C=[]
for i in range(0,N-1):
    if a_sabun[i]==0:
        b=b+1
    else:
        B.append(b)
        C.append(a_sabun[i])
        b=1
B.append(b)
D=[]
e=len(C)
for i in range(0,e-1):
    if C[i]>2:
        D.append(B[i])
    elif C[i]+C[i+1]>2:
        D.append(B[i]+B[i+1])
    else:
        D.append(B[i]+B[i+1]+B[i+2])
if C[e-1]>2:
    D.append(B[e-1]+B[e])
else:
    D.append(B[e-1])
    D.append(B[e])
print(max(D))
    
#3つの数が連続している場合しか有効ではない
#明らかにダメ
#時間的には問題ないはず

Submission status: Runtime Error

Solution code:
N=int(input())
a=list(map(int,input().split()))
a.sort()
a_sabun=[a[i+1]-a[i] for i in range(0,N-1)]
b=0
B=[]
for i in range(0,N-1):
    if a_sabun[i]==0:
        b=b+1
    else:
        B.append(b)
        b=0
print(max([B[i]+B[i+1]+B[i+2] for i in range(0,len(B)-2)])+3)

Submission status: Runtime Error

Solution code:
import collections
N = int(input())
alist = list(map(int,input().split()))
acount = collections.Counter(alist)
a = acount.most_common()
a.sort()
alen = len(a)
MAX = 0
if alen == 1:
    print(a[0][1])
elif alen == 2:
    if abs(a[0][1]-a[1][1])<=2:
        print(a[0][1]+a[1][1])
    else:
        print(max(a[0][1],a[1][1]))
else:
    for i in range(alen-2):
        if abs(a[i][0]-a[i+1][0]) == 1 and abs(a[i+2][0]-a[i+1][0]) == 1:
            kari = a[i][1]+a[i+1][1]+a[i+2][1]
        elif abs(a[i][0]-a[i+1][0]) <= 2 or abs(a[i+2][0]-a[i+1][0]) <= 2:
            if abs(a[i][0]-a[i+1][0]) <= 2:
                karia = a[i][1]+a[i+1][1]
            if abs(a[i+1][0]-a[i+2][0]) <= 2:
                karib = a[i+1][1]+a[i+2][1]
            kari = max(karia,karib)
        else:
            kari = max(a[i][1],a[i+1][1],a[i+2][1])
        if MAX <= kari:
            MAX = kari
    print(MAX)

Submission status: Runtime Error

Solution code:
import collections
N = int(input())
alist = list(map(int,input().split()))
acount = collections.Counter(alist)
a = acount.most_common()
a.sort()
alen = len(a)
MAX = 0
if alen == 1:
    print(a[0][1])
elif alen == 2:
    if abs(a[0][1]-a[1][1])<=2:
        print(a[0][1]+a[1][1])
    else:
        print(max(a[0][1],a[1][1]))
else:
    for i in range(alen-2):
        if abs(a[i][0]-a[i+1][0]) == 1 and abs(a[i+2][0]-a[i+1][0]) == 1:
            kari = a[i][1]+a[i+1][1]+a[i+2][1]
        elif abs(a[i][0]-a[i+1][0]) <= 2 or abs(a[i+2][0]-a[i+1][0]) <= 2:
            if abs(a[i][0]-a[i+1][0]) <= 2:
                kari1 = a[i][1]+a[i+1][1]
            if abs(a[i+1][0]-a[i+2][0]) <= 2:
                kari2 = a[i+1][1]+a[i+2][1]
            kari = max(kari1,kari2)
        else:
            kari = max(a[i][1],a[i+1][1],a[i+2][1])
        if MAX <= kari:
            MAX = kari
    print(MAX)

Submission status: Runtime Error

Solution code:
import collections
N = int(input())
alist = list(map(int,input().split()))
acount = collections.Counter(alist)
a = acount.most_common()
a.sort()
MAX = 0
if N == 1:
    print(1)
elif N == 2:
    if abs(alist[0]-alist[1])<=2:
        print(2)
    else:
        print(1)
else:
    for i in range(N-2):
        if abs(a[i][0]-a[i+1][0]) == 1 and abs(a[i+2][0]-a[i+1][0]) == 1:
            kari = a[i][1]+a[i+1][1]+a[i+2][1]
        elif abs(a[i][0]-a[i+1][0]) == 1:
            kari = a[i][1]+a[i+1][1]
        elif abs(a[i+2][0]-a[i+1][0]) == 1:
            kari = a[i+1][1]+a[i+2][1]
        if MAX <= kari:
            MAX = kari
    print(MAX)

Submission status: Runtime Error

Solution code:
import collections
import math
import re
import numpy


def ai():return input()#s
def a():return int(input())#a,n or k
def ma():return map(int, input().split())#a,b,c,d or n,k
def ms():return map(str, input().split())#,a,b,c,d
def lma():return list(map(int, input().split()))#x or y
def lms():return list(map(str, input().split()))#x or y
k= []
l = []
f = 0
s=0
c=0
e =1
def say(i):return print("Yes" if i == 0 else "No")
def Say(i):return print("YES" if i == 0 else "NO")
#def rep(i):return for i in range()
a = a()
x = lma()
l = set(x)
m=1
tn = 0
for i in l:
    if i- tn==1:
        ta = tb
        tb = tc
        tc = x.count(i+1)
    else:
        ta = x.count(i-1)
        tb = x.count(i)
        tc = x.count(i+1)
    t = ta+tb+tc
    m=max(m,t)
    if m>=len(x):break
    tn = i
print(m)

Submission status: Runtime Error

Solution code:
import collections
import math
import re
import numpy


def ai():return input()#s
def a():return int(input())#a,n or k
def ma():return map(int, input().split())#a,b,c,d or n,k
def ms():return map(str, input().split())#,a,b,c,d
def lma():return list(map(int, input().split()))#x or y
def lms():return list(map(str, input().split()))#x or y
k= []
l = []
f = 0
s=0
c=0
e =1
def say(i):return print("Yes" if i == 0 else "No")
def Say(i):return print("YES" if i == 0 else "NO")
#def rep(i):return for i in range()
a = a()
x = lma()
l = set(x)
m=1
tn = 0
for i in l:
    if i- tn==1:
        ta = tb
        tb = tc
        tc = x.count(i+1)
    else:
        ta = x.count(i-1)
        tb = x.count(i)
        tc = x.count(i+1)
    t = ta+tb+tc
    m=max(m,t)
    tn = i
print(m)

Submission status: Runtime Error

Solution code:
import collections
import math
import re
import numpy


def ai():return input()#s
def a():return int(input())#a,n or k
def ma():return map(int, input().split())#a,b,c,d or n,k
def ms():return map(str, input().split())#,a,b,c,d
def lma():return list(map(int, input().split()))#x or y
def lms():return list(map(str, input().split()))#x or y
k= []
l = []
f = 0
s=0
c=0
e =1
def say(i):return print("Yes" if i == 0 else "No")
def Say(i):return print("YES" if i == 0 else "NO")
#def rep(i):return for i in range()
a = a()
x = lma()
l = set(x)
m=1
for i in l:
    if i- tn==1:
        ta = tb
        tb = tc
        tc = x.count(i+1)
    else:
        ta = x.count(i-1)
        tb = x.count(i)
        tc = x.count(i+1)
    t = ta+tb+tc
    m=max(m,t)
    tn = i
print(m)

Submission status: Runtime Error

Solution code:
N = int(input())
A = list(map(int, input().split()))
minA = min(A)
maxA = max(A)
list = [0]*max(A)
for i in range(minA+1,maxA):
    L = A
    L = [j for j in A if i-1 <= j <= i+1]
    list[i]= len(L)
print(max(list))

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
n=int(input())
a=list(map(int,input().split()))
b=list(set(a))
l=[];m=[]
for i in range(len(b)):
  l.append(a.count(b[i]))
if len(l)>2:
  for j in range(len(l)-3):
    m.append(l[j]+l[j+1]+l[j+2])
else:m.append(sum(l))
print(max(m))

Submission status: Runtime Error

Solution code:
import collections as cl
N = int(input())
a_li = [i for i in input().split()]
c = cl.Counter(a_li)

keys = list(c.keys())
max_sum = 0
if (len(c) == 1) :
    print(1)
elif (len(c) == 2) :
    if (abs(a_li[0] - a_li[1]) <= 1 ) :
        print(2)
    else :
        print(1)
else :
    for k in range(len(c) -2 ) :
        sum_v = c[keys[k]] + c[keys[k+1]] + c[keys[k+2]]
        if (max_sum < sum_v) :
            max_sum = sum_v
    
    print(max_sum)

Submission status: Runtime Error

Solution code:
import collections as cl
N = int(input())
a_li = [i for i in input().split()]
c = cl.Counter(a_li)
print(c)
# for k in list(c.keys())[:-2] :
keys = list(c.keys())
max_sum = 0
if (len(c) == 1) :
    print(1)
elif (len(c) == 2) :
    if (abs(a_li[0] - a_li[1]) <= 1 ) :
        print(2)
    else :
        print(1)
else :
    for k in range(len(c) -2 ) :
        sum_v = c[keys[k]] + c[keys[k+1]] + c[keys[k+2]]
        if (max_sum < sum_v) :
            max_sum = sum_v
    
    print(max_sum)

Submission status: Runtime Error

Solution code:
iimport collections

n=int(input())
l=list(map(int,input().split()))
s=collections.Counter(l)
s=list(s.items())
s=sorted(s,key=lambda x:x[0])
#print(s)
f=[0]*len(s)
for i in range(len(s)):
    f[i]=s[i][1]
    if i>=1:
        if s[i-1][0]==s[i][0]-1:
            f[i]+=s[i-1][1]
    if i<=len(s)-2:
        if s[i+1][0]==s[i][0]+1:
            f[i]+=s[i+1][1]
print(max(f))

Submission status: Runtime Error

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

if n == 1:
    print(1)
    exit()
elif n == 2:
    if abs(a[0] - a[1]) <= 2:
        print(2)
    else:
        print(1)
    exit()

d = {}
for x in a:
    if x in d:
        d[x] += 1
    else:
        d[x] = 1

d = sorted(d.items())

m = len(d)
ans = d[0][1]
if d[1][0] == d[0][0] + 1:
    ans += d[1][1]
r = d[m-1][1]
if d[m-2][0] == d[m-1][0] - 1:
    r += d[m-2][1]
ans = max(r, ans)

for i in range(1, m-1):
    r = d[i][1]
    if d[i-1][0] + 1 == d[i][0]:
        r += d[i-1][1]
    if d[i+1][0] - 1 == d[i][0]:
        r += d[i+1][1]
    ans = max(ans, r)


print(ans)

Submission status: Runtime Error

Solution code:
N = int(input())
A = list(map(int,input().split()))
count_list = []
if len(list(set(A))) == 1:
    print(1)
else:
    for i in range(min(A) + 1,max(A)):
        count = A.count(i-1) + A.count(i) + A.count(i+1)
        count_list.append(count)
    print(max(count_list))

Submission status: Runtime Error

Solution code:
N = int(input())
A = list(map(int,input().split()))
count_list = []
if len(list(set(A))) == 1:
    print("1")
else:
    for i in range(min(A) + 1,max(A)):
        count = A.count(i-1) + A.count(i) + A.count(i+1)
        count_list.append(count)
    print(max(count_list))

Submission status: Runtime Error

Solution code:
N = int(input())
a = list(map(int, input().split()))
cnt = []
for i in range(N):
    x = a[i]
    count = 0
    for j in range(N):
        if count + len(a[j:]) < max(cnt):
            break
        if x == a[j] or x == a[j]+1 or x == a[j]-1:
            count += 1
    cnt.append(count)

print(max(cnt))

Submission status: Runtime Error

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

Submission status: Runtime Error

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

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

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

for i in range(0,max(a)+1):
    c = a.count(i)
    d[i+1] += c
    d[i] += c
    d[i+2] += c

print(max(d))

Submission status: Runtime Error

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

listA.sort()
maxCount=0
index=0
if len(listA) == 1:
    print(1)
else:
    while index < len(listA):
        flag=0
        if index==0:
            base = listA[index]
            count=1
        else: 
            if listA[index] == base+1 and flag==0:
                nextSt = index
                count+=1
                flag =1
            elif listA[index] < base+3:
                count+=1
            else:
                if index == len(listA)-1:
                    break
                elif count > maxCount:
                    maxCount = count
                flag = 0
                base = listA[nextSt]
                index = nextSt-1
                count=0
        index+=1

    print(maxCount)