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: Time Limit Exceeded

Solution code:
n=int(input())
A=list(map(int,input().split()))
A.sort()
md=0
for i in range(n-1):
    for j in range(i+1,n):
        if A[j]-A[i]<=2:
            md = max(j-i,md)
print(md+1)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int,input().split()))
B = list(set(A))
x = int()
y = int()
count = 0 
for i in range(len(B)):
  x = B[i]
  y = 0
  y += A.count(x)
  y += A.count(x+1)
  y += A.count(x-1)
  if count <= y:
    count = y
print(count)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int,input().split()))
B = sorted(A)
x = int()
y = int()
count = 0 
for i in range(len(B)):
  x = B[i]
  y = 0
  y += A.count(x)
  y += A.count(x+1)
  y += A.count(x-1)
  if count <= y:
    count = y
print(count)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
A=int(input())
n_l=list(map(int,input().split()))
n_l.sort()
n_l_s=deque()
for i in range(A-1):
   n_l_s.append(n_l[i+1]-n_l[i])
ans=1
n_l_s=list(n_l_s)
for i in range(A-1):
   c=0
   n=0
   for j in range(i,A-1):
      c+=n_l_s[j]
      if c < 3:
         n+=1
      else:
         ans=max(ans,n+1)
         break
print(ans)

Submission status: Time Limit Exceeded

Solution code:
A=int(input())
n_l=list(map(int,input().split()))
n_l.sort()
n_l_s=[]
for i in range(A-1):
   n_l_s.append(n_l[i+1]-n_l[i])
ans=1
for i in range(A-1):
   c=0
   n=0
   for j in range(i,A-1):
      c+=n_l_s[j]
      if c < 3:
         n+=1
      else:
         ans=max(ans,n+1)
         break
print(ans)

Submission status: Time Limit Exceeded

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

cnt = []
tmp = a[0]
cnt1 = 1
for i in range(1, N):
    if(abs(tmp-a[i]) <= 2):
        cnt1 += 1
    else:
        cnt.append(cnt1)
        if(abs(a[i]-a[i-1]) <= 1):
            cnt1 = a.count(a[i-1]-1)+a.count(a[i-1])+1
            tmp = a[i-1]-1
        else:
            cnt1 = 1
            tmp = a[i]
if(len(cnt) > 0):
    print(max(cnt1, max(cnt)))
else:
    print(cnt1)

Submission status: Time Limit Exceeded

Solution code:
import collections

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

nums = [[0] * 3 for i in range(N)]

roll = [-1,0,1]

for i in range(N):
  for j in range(3):
    d = A[i] + roll[j]
    nums[i][j] = d

B = sum(nums,[])
B = collections.Counter(B).most_common()
print(B[0][1])

Submission status: Time Limit Exceeded

Solution code:
def f(mc):
    if mc-1 in a and mc+1 in a:
        ans = a.count(mc-1) + a.count(mc) + a.count(mc+1)
    elif (mc-1 not in a) and (mc+1 in a):
        ans = a.count(mc) + a.count(mc+1) + a.count(mc+2)
    elif (mc-1 in a) and (mc+1 not in a):
        ans = a.count(mc) + a.count(mc-1) + a.count(mc-2)
    else:
        ans = a.count(mc)
    return ans

from collections import Counter

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

# setA = list(set(a))

c = Counter(a)
mc = c.most_common()

ans = -1
for _mc in mc:
    if _mc[0]-1 in a and _mc[0]+1 in a:
        ans = max(ans, c[_mc[0]-1] + c[_mc[0]] + c[_mc[0]+1])
    elif (_mc[0]-1 not in a) and (_mc[0]+1 in a):
        ans = max(ans, c[_mc[0]] + c[_mc[0]+1] + c[_mc[0]+2])
    elif (_mc[0]-1 in a) and (_mc[0]+1 not in a):
        ans = max(ans, c[_mc[0]] + c[_mc[0]-1] + c[_mc[0]-2])
    else:
        ans = max(ans, c[_mc[0]])

# print(max(ans))
print(ans)

Submission status: Time Limit Exceeded

Solution code:
def f(mc):
    if mc-1 in a and mc+1 in a:
        ans = a.count(mc-1) + a.count(mc) + a.count(mc+1)
    elif (mc-1 not in a) and (mc+1 in a):
        ans = a.count(mc) + a.count(mc+1) + a.count(mc+2)
    elif (mc-1 in a) and (mc+1 not in a):
        ans = a.count(mc) + a.count(mc-1) + a.count(mc-2)
    else:
        ans = a.count(mc)
    return ans

# from collections import Counter

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

setA = list(set(a))

ans = -1
for _a in setA:
    ans = max(f(_a), ans)
    # ans.append(f(_a))

# print(max(ans))
print(ans)

Submission status: Time Limit Exceeded

Solution code:
def f(mc):
    if mc-1 in a and mc+1 in a:
        ans = a.count(mc-1) + a.count(mc) + a.count(mc+1)
    elif (mc-1 not in a) and (mc+1 in a):
        ans = a.count(mc) + a.count(mc+1) + a.count(mc+2)
    elif (mc-1 in a) and (mc+1 not in a):
        ans = a.count(mc) + a.count(mc-1) + a.count(mc-2)
    else:
        ans = a.count(mc)
    return ans

from collections import Counter

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

setA = list(set(a))

ans = -1
for _a in setA:
    ans = max(f(_a), ans)
    # ans.append(f(_a))

# print(max(ans))
print(ans)

Submission status: Time Limit Exceeded

Solution code:
def f(mc):
    if mc-1 in a and mc+1 in a:
        ans = a.count(mc-1) + a.count(mc) + a.count(mc+1)
    elif (mc-1 not in a) and (mc+1 in a):
        ans = a.count(mc) + a.count(mc+1) + a.count(mc+2)
    elif (mc-1 in a) and (mc+1 not in a):
        ans = a.count(mc) + a.count(mc-1) + a.count(mc-2)
    else:
        ans = a.count(mc)
    return ans

from collections import Counter

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

setA = list(set(a))

ans = []
for _a in setA:
    ans.append(f(_a))

print(max(ans))

Submission status: Time Limit Exceeded

Solution code:
def f(mc):
    if mc-1 in a and mc+1 in a:
        ans = a.count(mc-1) + a.count(mc) + a.count(mc+1)
    elif (mc-1 not in a) and (mc+1 in a):
        ans = a.count(mc) + a.count(mc+1) + a.count(mc+2)
    elif (mc-1 in a) and (mc+1 not in a):
        ans = a.count(mc) + a.count(mc-1) + a.count(mc-2)
    else:
        ans = a.count(mc)
    return ans

from collections import Counter

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

c = Counter(a)

# mc = c.most_common()[0][0]
# if mc == 0:
#     mc = c.most_common()[1][0]

# if 10**5 - 1 > mc > 1:
#     ans = c.most_common()[0][1] + a.count(mc-1) + a.count(mc+1)
# elif mc == 1:
#     ans = a.count(1) + a.count(2) + a.count(3)

# if mc-1 in a and mc+1 in a:
#     ans = a.count(mc-1) + a.count(mc) + a.count(mc+1)
# elif (mc-1 not in a) and (mc+1 in a):
#     ans = a.count(mc) + a.count(mc+1) + a.count(mc+2)
# elif (mc-1 in a) and (mc+1 not in a):
#     ans = a.count(mc) + a.count(mc-1) + a.count(mc-2)
# else:
#     ans = a.count(mc)

ans = []
for _a in a:
    ans.append(f(_a))

print(max(ans))

Submission status: Time Limit Exceeded

Solution code:
def f(mc):
    if mc-1 in a and mc+1 in a:
        ans = a.count(mc-1) + a.count(mc) + a.count(mc+1)
    elif (mc-1 not in a) and (mc+1 in a):
        ans = a.count(mc) + a.count(mc+1) + a.count(mc+2)
    elif (mc-1 in a) and (mc+1 not in a):
        ans = a.count(mc) + a.count(mc-1) + a.count(mc-2)
    else:
        ans = a.count(mc)
    return ans

from collections import Counter

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

c = Counter(a)

# mc = c.most_common()[0][0]
# if mc == 0:
#     mc = c.most_common()[1][0]

# if 10**5 - 1 > mc > 1:
#     ans = c.most_common()[0][1] + a.count(mc-1) + a.count(mc+1)
# elif mc == 1:
#     ans = a.count(1) + a.count(2) + a.count(3)

# if mc-1 in a and mc+1 in a:
#     ans = a.count(mc-1) + a.count(mc) + a.count(mc+1)
# elif (mc-1 not in a) and (mc+1 in a):
#     ans = a.count(mc) + a.count(mc+1) + a.count(mc+2)
# elif (mc-1 in a) and (mc+1 not in a):
#     ans = a.count(mc) + a.count(mc-1) + a.count(mc-2)
# else:
#     ans = a.count(mc)

ans = []
mc = c.most_common()

for i in range(len(mc)):
    ans.append(f(mc[i][0]))

print(max(ans))

Submission status: Time Limit Exceeded

Solution code:
def f(mc):
    if mc-1 in a and mc+1 in a:
        ans = a.count(mc-1) + a.count(mc) + a.count(mc+1)
    elif (mc-1 not in a) and (mc+1 in a):
        ans = a.count(mc) + a.count(mc+1) + a.count(mc+2)
    elif (mc-1 in a) and (mc+1 not in a):
        ans = a.count(mc) + a.count(mc-1) + a.count(mc-2)
    else:
        ans = a.count(mc)
    return ans

from collections import Counter

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

c = Counter(a)

# mc = c.most_common()[0][0]
# if mc == 0:
#     mc = c.most_common()[1][0]

# if 10**5 - 1 > mc > 1:
#     ans = c.most_common()[0][1] + a.count(mc-1) + a.count(mc+1)
# elif mc == 1:
#     ans = a.count(1) + a.count(2) + a.count(3)

# if mc-1 in a and mc+1 in a:
#     ans = a.count(mc-1) + a.count(mc) + a.count(mc+1)
# elif (mc-1 not in a) and (mc+1 in a):
#     ans = a.count(mc) + a.count(mc+1) + a.count(mc+2)
# elif (mc-1 in a) and (mc+1 not in a):
#     ans = a.count(mc) + a.count(mc-1) + a.count(mc-2)
# else:
#     ans = a.count(mc)

ans = []
# i = 1
mc = c.most_common()
for _mc in mc:
    ans.append(f(_mc[0]))
    # i += 1
    # if i == 100:
    #     break

print(max(ans))

Submission status: Time Limit Exceeded

Solution code:
def f(mc):
    if mc-1 in a and mc+1 in a:
        ans = a.count(mc-1) + a.count(mc) + a.count(mc+1)
    elif (mc-1 not in a) and (mc+1 in a):
        ans = a.count(mc) + a.count(mc+1) + a.count(mc+2)
    elif (mc-1 in a) and (mc+1 not in a):
        ans = a.count(mc) + a.count(mc-1) + a.count(mc-2)
    else:
        ans = a.count(mc)
    return ans

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

if len(set(a)) == 1:
    print(n)
    exit()

c = Counter(a)
mc = c.most_common()[0][0]
# if mc == 0:
#     mc = c.most_common()[1][0]

# if 10**5 - 1 > mc > 1:
#     ans = c.most_common()[0][1] + a.count(mc-1) + a.count(mc+1)
# elif mc == 1:
#     ans = a.count(1) + a.count(2) + a.count(3)

# if mc-1 in a and mc+1 in a:
#     ans = a.count(mc-1) + a.count(mc) + a.count(mc+1)
# elif (mc-1 not in a) and (mc+1 in a):
#     ans = a.count(mc) + a.count(mc+1) + a.count(mc+2)
# elif (mc-1 in a) and (mc+1 not in a):
#     ans = a.count(mc) + a.count(mc-1) + a.count(mc-2)
# else:
#     ans = a.count(mc)

ans1 = f(mc)
ans2 = f(c.most_common()[1][0])
ans = []
for _c in c.most_common():
    ans.append(f(_c[0]))

print(max(ans))

Submission status: Time Limit Exceeded

Solution code:
input()
L = list(map(int, input().split()))
M = [i-1 for i in L]
P = [i+1 for i in L]
a = list(set(L)|set(M)|set(P))
answer = 0
for i in a:
    x = L.count(i)+L.count(i-1)+L.count(i+1)
    if answer<x:
        answer = x
print(answer)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
As = sorted(list(map(int,input().split())))
mins = As[0]
maxs = As[-1]
ans = 0
for i in range(mins,maxs+1):
    ans = max(ans,As.count(i)+As.count(i+1)+As.count(i-1))
print(ans)

Submission status: Time Limit Exceeded

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

b = []
for x in a:
  b += [x-1,x,x+1]
b.sort()

res = 0
for x in set(b):
  res = max(res,b.count(x))
print(res)

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sun Feb 10 10:05:08 2019

@author: shinjisu
"""


# ARC 076 C
def getInt(): return int(input())


def getIntList(): return [int(x) for x in input().split()]


def zeros(n): return [0]*n


def dmp(x):
    global debug
    if debug:
        print(x)
    return x


def probC():
    N = getInt()
    A = getIntList()
    dmp((N, A))
    A.sort()
    dmp(A)
    mxCnt = 1
    for i in range(N):
        cnt = 0
        for j in range(i,N):
            if A[i]+2<A[j]:
                break
            cnt += 1
        mxCnt = max(mxCnt, cnt)
    return mxCnt


debug = False  # True False
print(probC())

def probCTLE2():
    N = getInt()
    A = getIntList()
    dmp((N, A))
    hist = zeros(10**5)
    for i in range(N):
        if A[i] in hist:
            hist[A[i]] += 1
        else:
            hist[A[i]] = 1
    #dmp(hist)
    mxCnt = 1
    #keylist = list(hist.keys())
    #mxKey = max(keylist)
    # dmp(mxKey)
    for k in range(10**5-2):
        cnt = hist[k] + hist[k+1] + hist[k+2]
        mxCnt = max(mxCnt, cnt)
    return mxCnt


def probCTLE():
    N = getInt()
    A = getIntList()
    dmp((N, A))
    hist = {}
    for i in range(N):
        if A[i] in hist:
            hist[A[i]] += 1
        else:
            hist[A[i]] = 1
    dmp(hist)
    mxCnt = 1
    keylist = list(hist.keys())
    mxKey = max(keylist)
    dmp(mxKey)
    for k in hist.keys():
        cnt = 0
        if k-1 in keylist:
            cnt += hist[k-1]
        if k in keylist:
            cnt += hist[k]
        if k+1 in keylist:
            cnt += hist[k+1]
        mxCnt = max(mxCnt, cnt)
        dmp(('max',mxCnt))
    return mxCnt


def probCWA():
    N = getInt()
    A = getIntList()
    dmp((N, A))
    hist = {}
    for i in range(N):
        if A[i] in hist:
            hist[A[i]] += 1
        else:
            hist[A[i]] = 1
    dmp(hist)
    mxCnt = 1
    keylist = list(hist.keys())
    mxKey = max(keylist)
    dmp(mxKey)
    for i in range(mxKey-1):
        cnt = 0
        if i in keylist:
            cnt += hist[i]
        if i+1 in keylist:
            cnt += hist[i+1]
        if i+2 in keylist:
            cnt += hist[i+2]
        mxCnt = max(mxCnt, cnt)
        dmp(('max',mxCnt))
    return mxCnt

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sun Feb 10 10:05:08 2019

@author: shinjisu
"""


# ARC 076 C
def getInt(): return int(input())


def getIntList(): return [int(x) for x in input().split()]


def zeros(n): return [0]*n


def dmp(x):
    global debug
    if debug:
        print(x)
    return x


def probC():
    N = getInt()
    A = getIntList()
    dmp((N, A))
    hist = zeros(10**5)
    for i in range(N):
        if A[i] in hist:
            hist[A[i]] += 1
        else:
            hist[A[i]] = 1
    #dmp(hist)
    mxCnt = 1
    #keylist = list(hist.keys())
    #mxKey = max(keylist)
    # dmp(mxKey)
    for k in range(10**5-2):
        cnt = hist[k] + hist[k+1] + hist[k+2]
        mxCnt = max(mxCnt, cnt)
    return mxCnt


debug = False  # True False
print(probC())

def probCTLE():
    N = getInt()
    A = getIntList()
    dmp((N, A))
    hist = {}
    for i in range(N):
        if A[i] in hist:
            hist[A[i]] += 1
        else:
            hist[A[i]] = 1
    dmp(hist)
    mxCnt = 1
    keylist = list(hist.keys())
    mxKey = max(keylist)
    dmp(mxKey)
    for k in hist.keys():
        cnt = 0
        if k-1 in keylist:
            cnt += hist[k-1]
        if k in keylist:
            cnt += hist[k]
        if k+1 in keylist:
            cnt += hist[k+1]
        mxCnt = max(mxCnt, cnt)
        dmp(('max',mxCnt))
    return mxCnt


def probCWA():
    N = getInt()
    A = getIntList()
    dmp((N, A))
    hist = {}
    for i in range(N):
        if A[i] in hist:
            hist[A[i]] += 1
        else:
            hist[A[i]] = 1
    dmp(hist)
    mxCnt = 1
    keylist = list(hist.keys())
    mxKey = max(keylist)
    dmp(mxKey)
    for i in range(mxKey-1):
        cnt = 0
        if i in keylist:
            cnt += hist[i]
        if i+1 in keylist:
            cnt += hist[i+1]
        if i+2 in keylist:
            cnt += hist[i+2]
        mxCnt = max(mxCnt, cnt)
        dmp(('max',mxCnt))
    return mxCnt

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sun Feb 10 10:05:08 2019

@author: shinjisu
"""


# ARC 076 C
def getInt(): return int(input())


def getIntList(): return [int(x) for x in input().split()]


def zeros(n): return [0]*n


def dmp(x):
    global debug
    if debug:
        print(x)
    return x


def probC():
    N = getInt()
    A = getIntList()
    dmp((N, A))
    hist = {}
    for i in range(N):
        if A[i] in hist:
            hist[A[i]] += 1
        else:
            hist[A[i]] = 1
    dmp(hist)
    mxCnt = 1
    keylist = list(hist.keys())
    mxKey = max(keylist)
    dmp(mxKey)
    for k in hist.keys():
        cnt = 0
        if k-1 in keylist:
            cnt += hist[k-1]
        if k in keylist:
            cnt += hist[k]
        if k+1 in keylist:
            cnt += hist[k+1]
        mxCnt = max(mxCnt, cnt)
        dmp(('max',mxCnt))
    return mxCnt


debug = False  # True False
print(probC())

"""
def probC():
    N = getInt()
    A = getIntList()
    dmp((N, A))
    hist = {}
    for i in range(N):
        if A[i] in hist:
            hist[A[i]] += 1
        else:
            hist[A[i]] = 1
    dmp(hist)
    mxCnt = 1
    keylist = list(hist.keys())
    mxKey = max(keylist)
    dmp(mxKey)
    for i in range(mxKey-1):
        cnt = 0
        if i in keylist:
            cnt += hist[i]
        if i+1 in keylist:
            cnt += hist[i+1]
        if i+2 in keylist:
            cnt += hist[i+2]
        mxCnt = max(mxCnt, cnt)
        dmp(('max',mxCnt))
    return mxCnt

"""

Submission status: Time Limit Exceeded

Solution code:
a = int(input())
ar = list(map(int,input().split(" ")))
br = []
for i in range(min(ar),max(ar)+1):
    count = 0
    count += ar.count(i)
    count += ar.count(i-1)
    count += ar.count(i+1)
    br.append(count)
print(max(br))

Submission status: Time Limit Exceeded

Solution code:
import collections

n = int(input())
a = list(map(int, input().split()))
ac = collections.Counter(a)
ans = 0
for i in range(min(a) - 1, max(a) + 2):
    total = 0
    for j in ac:
        if j == i or j + 1 == i or j - 1 == i:
            total += ac[j]
    ans = max(ans, total)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = list(map(int, input().split()))
ans = 0
for i in range(min(a) - 1, max(a) + 2):
    total = 0
    for j in a:
        if j == i or j + 1 == i or j - 1 == i:
            total += 1
    ans = max(ans, total)
print(ans)

Submission status: Time Limit Exceeded

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

for i in range(n-1):
    cnt=0
    for j in range(i,n-1):
        if abs(a[i]-a[j])<=2:
            cnt+=1

    ans=max(ans,cnt)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = list(map(int, input().split()))
b = [i + 1 for i in a]
c = [i - 1 for i in a]
use_set = set(a) | set(b) | set(c)
ans = 0
for i in use_set:
    ans = max(ans, a.count(i) + b.count(i) + c.count(i))
print(ans)

Submission status: Time Limit Exceeded

Solution code:
#C問題 together

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

minval = min(a)
maxval = max(a)

a2 = []

ans = []

for j in a:
	for i in [-1,0,1]:
		a2.append(i+j)

for i in range(minval-1,maxval+2):
	ans.append(a2.count(i)) 

print(max(ans))

Submission status: Time Limit Exceeded

Solution code:
#C問題 together

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

a.sort()

minval = a[0]
maxval = a[-1]
l = (maxval - minval +1)

num = [0]*l
num_ans = []


num[0] = a.count(minval)

for i in a:
	if num[i-minval] == 0:
		num[i-minval] = a.count(i)

if l == 1:
	print(num[0])
	exit(0)

num_ans.append(num[0]+num[1]) 

if l == 2:
	print(num_ans[0])
	exit(0)

for i in range(1,l-1):
	num_ans.append(num[i-1]+num[i]+num[i+1])
num_ans.append(num[-2]+num[-1])

print(max(num_ans))

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
N = int(input())
a = [int(i) for i in input().split(" ")]
a = np.array(a,dtype='int')
def count(a):
    return [1 if i==1 or i== 0 or i == -1 else 0 for i in a]
if N == 1:
    print(1)
else:
    min_ = min(a)-1
    max_ = max(a)+1
    ans = -1
    for K in range(min_,max_):
        b = a - K
        tmp = sum(count(b))
        if ans < tmp:
            ans = tmp
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
if __name__ == '__main__':
    N = int(input())
    a = input()
    a = a.split(' ')
    a = list(map(int, a))

    a.sort()

    min = a[0]
    max = a[-1]

    i = min
    j = 0
    k = 0
    num = 0
    max_num = 0

    while i <= max + 1:
        if i - 1 <= a[j] and a[j] <= i + 1:
            num += 1
        elif i + 1 < a[j]:
            if max_num < num:
                max_num = num
            num = 0
            # j = k
            i += 1
            j = 0
            continue
        j += 1
        if j == len(a):
            if max_num < num:
                max_num = num
            num = 0;
            # j = k
            i += 1
            j = 0
            continue
            
    print(max_num)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
a = list(map(int, input().split()))
ap = [e + 1 for e in a]
am = [e - 1 for e in a]

b = sorted(a + ap + am)

cmax = 0

pre = b.pop(0)

while True:
    smc = 1
    while True:
        if len(b) == 0:
            print(cmax)
            import sys
            sys.exit()

        cur = b.pop(0)
        if cur == pre:
            smc += 1
        else:
            cmax = max(smc, cmax)
            pre = cur
            break

Submission status: Time Limit Exceeded

Solution code:

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

B = []
for i in A:
    B.append(i - 1)
    B.append(i)
    B.append(i + 1)

Bset = set(B)
C = []
for i in Bset:
    C.append(len(list(filter(lambda x: x == i, B))))

print(max(C))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
a = list(map(int, input().split()))
ap = [e + 1 for e in a]
am = [e - 1 for e in a]

b = a + ap + am

cnt = [b.count(e) for e in set(b)]

print(max(cnt))

Submission status: Accepted

Solution code:
# author:  Taichicchi
# created: 27.09.2020 23:31:22

import sys

from collections import Counter

N = int(input())

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

ls = []

for A in a:
    ls.append(A - 1)
    ls.append(A)
    ls.append(A + 1)

c = Counter(ls)

ans = max(c.values())

print(ans)

Submission status: Accepted

Solution code:
from collections import Counter

n = int(input())
counter_a = Counter(map(int, input().split()))
maxCnt = 0
for key, value in counter_a.items():
    cnt = counter_a[key-1] + counter_a[key] + counter_a[key+1]
    maxCnt = max(maxCnt, cnt)
print(maxCnt)

Submission status: Accepted

Solution code:
import numpy as np
import scipy.stats as stats
N = int(input())
A = list(map(int,input().split()))
A = np.array(A)
A_plus = A + 1
A_minus = A - 1
A = np.append(A, A_plus)
A = np.append(A, A_minus)
print(stats.mode(A).count[0])

Submission status: Accepted

Solution code:
n=int(input())
A=list(map(int,input().split()))
import sys
mod=10**9+7 ; inf=float("inf")
from math import sqrt, ceil
from collections import deque, Counter, defaultdict #すべてのkeyが用意されてる defaultdict(int)で初期化
input=lambda: sys.stdin.readline().strip()
sys.setrecursionlimit(11451419)
from decimal import ROUND_HALF_UP,Decimal as D  #変換後の末尾桁を0や0.01で指定
  #Decimal((str(0.5)).quantize(Decimal('0'), rounding=ROUND_HALF_UP))
from functools import lru_cache
from bisect import bisect_left as bileft, bisect_right as biright
from fractions import Fraction as F  #F(a,b)で正確なa/b
#メモ化再帰defの冒頭に毎回 @lru_cache(maxsize=10**10)
#引数にlistはだめ
#######ここまでテンプレ#######
#ソート、"a"+"b"、再帰ならPython3の方がいい
#######ここから天ぷら########

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

Submission status: Accepted

Solution code:
import sys
from collections import Counter

input = sys.stdin.readline


def main():
    N = int(input())
    A = list(map(int, input().split()))
    C = Counter(A)
    ans = 0
    for i in range(10 ** 5 + 1):
        ans = max(ans, C[i - 1] + C[i] + C[i + 1])
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import collections

n=int(input())
A=list(map(int,input().split()))
A.sort()
md=0
j=0
for i in range(n-1):
    while j<n:
        if A[j]-A[i]<=2:
            md = max(j-i,md)
            j+=1
        else:
            break
print(md+1)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
ins = lambda: input().rstrip()
ini = lambda: int(input().rstrip())
inm = lambda: map(int, input().split())
inl = lambda: list(map(int, input().split()))
out = lambda x: print('\n'.join(map(str, x)))

n = ini()
a = inl()
ans = 0
b = [0] * (10**5 + 1)
for i in a:
    b[i] += 1
for i in range(10**5-1):
    ans = max(ans, b[i] + b[i+1] + b[i+2])
print(ans)

Submission status: Accepted

Solution code:
ma = lambda :map(int,input().split())
lma = lambda :list(map(int,input().split()))
ni = lambda:int(input())
yn = lambda fl:print("Yes") if fl else print("No")
import collections
import math
import itertools
import heapq as hq
n = ni()
A = lma()
l = max(A)
cnts = [0 for i in range(l+3)]
for a in A:
    cnts[a]+=1
    cnts[a-1] +=1
    cnts[a+1] +=1
print(max(cnts))

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split()))
B = [0]*(10**6)
c = int()
ans = int(0)
for i in range(len(A)):
  B[A[i]] += 1

for i in range(1,len(B)-2):
  c = 0
  c += B[i-1]
  c += B[i]
  c += B[i+1]
  if ans <= c:
    ans = c
print(ans)

Submission status: Accepted

Solution code:
import math
from itertools import groupby


def read_int():
    return int(input().strip())


def read_ints():
    return list(map(int, input().strip().split(' ')))


def ilen(it):
    return sum(1 for _ in it)


def solve():
    N = read_int()
    A = read_ints()
    A.sort()
    grouped = [(a, ilen(v)) for a, v in groupby(A)]
    max_count = -math.inf
    for i in range(len(grouped)):
        count = grouped[i][1]
        if i > 0 and grouped[i][0]-1 == grouped[i-1][0]:
            count += grouped[i-1][1]
        if i < len(grouped)-1 and grouped[i][0]+1 == grouped[i+1][0]:
            count += grouped[i+1][1]
        max_count = max(max_count, count)
    return max_count


if __name__ == '__main__':
    print(solve())

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!
import sys
read = sys.stdin.read
readline = sys.stdin.readline


n,*a = map(int,read().split())

from collections import Counter
d = Counter(a + [i+1 for i in a]+[i-1 for i in a])
print(max(d.values()))

Submission status: Accepted

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

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

d = Counter(A)

ans = 0
for k in d.keys():
    tmp = d[k-1] + d[k] + d[k+1]
    ans = max(ans, tmp)
print(ans)

Submission status: Accepted

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

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

d = Counter(A)

ans = 0
for i in range(1, 10**5+1):
    tmp = d[i-1] + d[i] + d[i+1]
    ans = max(tmp, ans)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
import collections
input = lambda: sys.stdin.readline()
exec("try:sys.stdin=open('in.txt','r');sys.stdout=open('out.txt','w')\nexcept:pass")

N = int(input())
A = list(map(int, input().split()))
d = collections.Counter()
for i in range(N):
    d[A[i] + 1] += 1
    d[A[i]] += 1
    d[A[i] - 1] += 1
print(max(list(d.values())))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
from itertools import accumulate


def main():
    n, *A = map(int, sys.stdin.read().split())
    minA = min(A) - 1
    lr = [[i - 1 - minA, i + 1 - minA] for i in A]
    lst = [0] * (max(A) - minA + 3)
    for i, j in lr:
        lst[i] += 1
        lst[j + 1] -= 1
    lst = list(accumulate(lst))
    print(max(lst))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
import bisect


def main():
    n, *a = map(int, sys.stdin.read().split())
    lr = [[i - 1,i + 1] for i in a]
    lr.sort(key=lambda x: x[0])
    l = [i[0] for i in lr]
    res = 0
    for i in range(n):
        right = bisect.bisect(l, lr[i][1])
        left = bisect.bisect_left(l, l[i])
        res = max(res, right - left)
    print(res)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
from collections import Counter, defaultdict


def main():
  _, *a = map(int, sys.stdin.read().split())
  c = defaultdict(int, Counter(a))
  res = max(c[i - 1] + c[i] + c[i + 1] for i in range(max(a) + 1))
  print(res)


if __name__ == '__main__':
  main()

Submission status: Accepted

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

ans = [0] * 100000

for i in range(len(a)):
  if a[i] != 0:
    ans[a[i]-1] += 1
  ans[a[i]] += 1
  if a[i] != 99999:
    ans[a[i]+1] += 1
  
print(max(ans))

Submission status: Accepted

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

c=Counter([j+i for i in (0,-1, 1) for j in a])
ans=c.most_common(1)
print(ans[0][1])

Submission status: Accepted

Solution code:
A=int(input())
n_l=list(map(int,input().split()))
import collections
ans=0
n_l=collections.Counter(n_l)
for i in range(0,100001):
   ans=max(ans,n_l[i]+n_l[i+1]+n_l[i+2])
print(ans)

Submission status: Accepted

Solution code:
#!python3

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


def main():
    d = {i: 0 for i in range(-1, 10 ** 5 + 2)}
    for a in A:
        d[a] += 1

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


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
from collections import Counter
N = int(input())
a = map(int, input().split())
c = Counter(a)
ans = 0
for i in c:
  t = c[i-1] + c[i] + c[i+1]
  if ans < t:
    ans = t
print(ans)

Submission status: Accepted

Solution code:
import collections
N = int(input())
a = map(int, input().split())
c = collections.Counter(a)
ans = 0
for i in c:
  t = c[i-1] + c[i] + c[i+1]
  if ans < t:
    ans = t
print(ans)

Submission status: Accepted

Solution code:
input()
A = list( map(int, input().split()))
d = {}
for i in A:
  a,b,c = i-1,i,i+1
  for j in [a,b,c]:
    if j in d:
      d[j] +=1
    else:
      d[j] = 1
print(sorted(d.items(), key = lambda x:x[1])[-1][1])

Submission status: Accepted

Solution code:
import collections

N = int(input())
a = list(map(int, input().split()))
a.sort()
c = collections.Counter(a)

cnt = []
tmp = a[0]
cnt1 = 1
for i in range(1, N):
    if(abs(tmp-a[i]) <= 2):
        cnt1 += 1
    else:
        cnt.append(cnt1)
        if(a[i]-a[i-1] <= 1):
            cnt1 = c.get(a[i-1]-1, 0)+c.get(a[i-1], 0)+1
            tmp = a[i-1]-1
        else:
            cnt1 = 1
            tmp = a[i]
if(len(cnt) > 0):
    print(max(cnt1, max(cnt)))
else:
    print(cnt1)

Submission status: Accepted

Solution code:
from collections import defaultdict
n=int(input())
a=list(map(int,input().split()))
cnt_dic=defaultdict(int)
for i in range(n):
  v=a[i]
  cnt_dic[v+1]+=1
  cnt_dic[v]+=1
  cnt_dic[v-1]+=1
ans=0
for key in cnt_dic.keys():
  ans=max(ans,cnt_dic[key])
print(ans)