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 collections import Counter

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

array = []
for li in [-1,0,1]:
  for ai in aa:
    array.append(ai+li)

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

Submission status: Accepted

Solution code:
from collections import defaultdict as dd
n = int(input())
A = list(map(int, input().split()))
dic = dd(lambda: 0)
for a in A:
  dic[a-1] += 1
  dic[a] += 1
  dic[a+1] += 1
print(max(dic.values()))

Submission status: Accepted

Solution code:
from sys import stdin
import bisect

n = int(stdin.readline().rstrip())
li = list(map(int,stdin.readline().rstrip().split()))

li.sort()

ma = 0

if len(li) == 1:
    print(1)
elif len(li) == 2:
    if abs(li[0] - li[1]) <= 1:
        print(2)
    else:
        print(1)
else:
    for i in range(1,n-1):
        ma = max(ma,bisect.bisect(li,i+1)-bisect.bisect_left(li,i-1))

    print(ma)

Submission status: Accepted

Solution code:
N =input()
As = [int(x) for x in input().split()]
count = {}
for a in As:
    for a_ in [a-1, a, a+1]:
        if count.get(a_):
            count[a_] += 1
        else:
            count[a_] = 1
max_count = 0
for key, value in count.items():
    if value > max_count:
        max_count = value
        
print(max_count)

Submission status: Accepted

Solution code:
from collections import defaultdict

n = int(input())
a = list(map(int, input().split()))
ans = 0
nom = defaultdict(int)
for i in range(n):
    nom[a[i]]+=1
ans = 0
a = set(a)
a = list(a)
m = len(a)
for i in range(m):
    b = nom[a[i]] + nom[a[i]-1]+ nom[a[i]+1]
    ans = max(ans,b)
print(ans)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
input=lambda :sys.stdin.readline().rstrip()
def resolve():
    M=10**5
    n=int(input())
    C=[0]*M
    for a in map(int,input().split()):
        C[a]+=1
    print(max(*(C[i-1]+C[i]+C[i+1] for i in range(1,M-1))))
resolve()

Submission status: Accepted

Solution code:
import sys
from collections import Counter

input = sys.stdin.readline

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

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import collections as col
n = int(input())
li = list(map(int,input().split()))

li1 = []
for i in range(n):
  li1.append(li[i])
  li1.append(li[i]+1)
  li1.append(li[i]-1)
c = col.Counter(li1)
print(max(list(c.values())))

Submission status: Accepted

Solution code:
n = int(input())
A = map(int, input().split())
D = [0]*10**6
for i in A:
  for j in range(3):
    D[i+j] += 1
print(max(D))

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
import collections
c=collections.Counter(a)
d=sorted(c)
maxi=0
if n>=3 and len(d)>=3:
    for i in range(len(d)-2):
        total=c[d[i]]+c[d[i+1]]+c[d[i+2]]
        maxi=max(maxi,total)
    print(maxi)
if n>=3 and len(d)==2:
    print(n if abs(d[0]-d[1])<=2 else 2)
if n>=3 and len(d)==1:
    print(n)
if n==2:
    print(2 if abs(a[0]-a[1])<=2 else 1)
if n==1:
    print(1)

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
b=[]
for i in range(n):
    b.append(a[i])
    b.append(a[i]+1)
    b.append(a[i]-1)
import heapq
cnt=0
c=1000000
d=0
ans=[]
heapq.heapify(b)
#print(b)
while len(b)>0:
    d=heapq.heappop(b)
    if c==1000000:
        c=d
        cnt+=1
    elif c!=d:
        ans.append(cnt)
        cnt=1
        c=d
    else:
        cnt+=1
print(max(ans))

Submission status: Accepted

Solution code:
N = int(input())
A = [int(a) for a in input().split()]
X = [0] * 101010
for a in A:
    X[a] += 1

ma = 0
for i in range(100001):
    ma = max(ma, X[i-1] + X[i] + X[i+1])
print(ma)

Submission status: Accepted

Solution code:
import collections

n = int(input())
a = list(map(str, input().split()))
x1 = []
x2 = []
x3 = []
for i in range(n):
    tmp = str(int(a[i]) + 1)
    x1.append(tmp)
    tmp = a[i]
    x2.append(tmp)
    tmp = str(int(a[i]) - 1)
    x3.append(tmp)

x1.extend(x2)
x1.extend(x3)
x1.sort()
set_x1 = set(x1)
c = collections.Counter(x1)
Max = 0
for i in set_x1:
    if int(c[str(i)]) > Max:
        Max = int(c[str(i)])
print(Max)

Submission status: Accepted

Solution code:
N = int(input())
arr = list(map(int, input().split()))
arr2 = [0]*(max(arr)+1)
if max(arr) < 4:
    print(len(arr))
    exit()
for i in range(N):
    arr2[arr[i]] += 1
ans = arr2[1] + arr2[2] + arr2[3]
tmp = ans
for i in range(4,len(arr2)):
    tmp = tmp - arr2[i-3] + arr2[i]
    if ans < tmp:
        ans = tmp
print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict

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

a_li.sort()
a_count = defaultdict(int)
for x in a_li:
    a_count[x] += 1

ans = 0

a_set = set(a_li)

for i in a_set:
    s = a_count[i] + a_count[i-1] + a_count[i+1]
    ans = max(ans,s)

print(ans)

Submission status: Accepted

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

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

ans = 0
X_count = 0

if N == 1:
    print(1)
elif N == 2:
    if abs(A[1] - A[0]) <= 1:
        print(2)
    else:
        print(1)
else:
    for i in range(N-2):
        X_count = X[i] + X[i+1] + X[i+2]
        ans = max(ans,X_count)
        X_count = 0
    print(ans)

Submission status: Accepted

Solution code:
def examC():
    N = I()
    A = LI()
    d = defaultdict(int)
    for i in A:
        d[i] +=1;d[i-1] +=1; d[i+1] +=1
    ans = max(d.values())
    print(ans)

import sys
import copy
import bisect
import heapq
from collections import Counter,defaultdict,deque
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def S(): return sys.stdin.readline().strip()
mod = 10**9 + 7
inf = float('inf')

examC()

Submission status: Accepted

Solution code:
import collections
n = int(input())
a = [int(x) for x in input().split()]

c = collections.Counter(a)
max_counts = 0

for i in c.keys():
    #count = sum([v for k, v in c.items() if abs(k-i) <= 1])
    count = c[i] + c[i-1] + c[i+1]
    max_counts = count if count > max_counts else max_counts

print(max_counts)

Submission status: Accepted

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

Submission status: Accepted

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

d = dict(Counter(a) + Counter(b) + Counter(c))
print (max(d.values()))

Submission status: Accepted

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

Submission status: Accepted

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

import collections
counter=collections.Counter(a)

ans=0
for key in counter.keys():
  s=0
  s+=counter[key]
  if key+1 in counter:
    s+=counter[key+1]
  if key-1 in counter:
    s+=counter[key-1]
  ans=max(ans,s)
  
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
dic = {}
List = list(map(int,input().split()))

for item in List:
  mini_list = [item-1,item,item+1]
  for value in mini_list:
    if value not in dic:
      dic[value] = 1
    else:
      dic[value] +=1

print(max(dic.values()))

Submission status: Accepted

Solution code:
import collections

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

b = list()
c = list()

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

d = collections.Counter(a + b + c)

print(max(d.values()))

Submission status: Accepted

Solution code:
from collections import Counter

N = int(input())
A = Counter(list(map(int,input().split())))
keysA =  list(A.keys())
len_keysA = len(keysA)

if len_keysA == 1:
  print(N)
  exit()
ans = -float('inf')
for i in range(len_keysA):
  tmp_ans = A[keysA[i]]
  if i != 0 and abs(keysA[i]-keysA[i-1]) <= 1: tmp_ans += A[keysA[i-1]]
  if i != len_keysA-1 and abs(keysA[i]-keysA[i+1]) <= 1: tmp_ans += A[keysA[i+1]]  
  ans = max(ans, tmp_ans)
print(ans)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**6)

N = int(input())
A = list(map(int, input().split()))
B = [0] * 100003
for i in range(N):
    B[A[i]] += 1

max = 0
for i in range(100002):
    if max < B[i - 1] + B[i] + B[i + 1]:
        max = B[i - 1] + B[i] + B[i + 1]
print(max)

Submission status: Accepted

Solution code:
#72c
import collections

N = int(input())
a_list = [int(e) for e in input().split()]

c = collections.Counter(a_list)

ans = 1

for number in sorted(c.items(), reverse=False):
    #print("######################")
    #print(number[0],":",number[1])
    #print(number[0]+1,":",c[number[0]+1])
    #print(number[0]-1,":",c[number[0]-1])
    if ans < c[number[0]-1] + number[1] + c[number[0]+1]:
        ans = c[number[0]-1] + number[1] + c[number[0]+1]
        
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from sys import stdin

n, *a = (int(x) for x in stdin.read().split())

count = {i: 0 for i in set(a)}
for i in a: count[i] += 1 

suite_count = count.copy()
for i in count:
    if i - 1 in count: suite_count[i] += count[i-1]
    if i + 1 in count: suite_count[i] += count[i+1]

print(max(suite_count.values()))

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    A = [int(i) for i in input().split()]
    max_A = max(A)
    counts = [0] * (max_A + 2)
    for a in A:
        if a > 0:
            counts[a-1] += 1
        counts[a] += 1
        counts[a+1] += 1
    print(max(counts))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def main():
    _ = int(input())
    A = [int(i) for i in input().split()]
    from collections import Counter, defaultdict
    c = defaultdict(int)
    for k, v in Counter(A).items():
        c[k] = v
    ans = 0
    for i in range(max(A) + 1):
        if i != 0:
            cur = c[i] + c[i-1] + c[i+1]
        else:
            cur = c[i] + c[i+1]
        ans = max(ans, cur)
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def main():
    _ = int(input())
    A = [int(i) for i in input().split()]
    A.sort()
    from collections import Counter, defaultdict
    c = defaultdict(int)
    for k, v in Counter(A).items():
        c[k] = v
    ans = 0
    for i in range(10**5+1):
        if i != 0:
            cur = c[i] + c[i-1] + c[i+1]
        else:
            cur = c[i] + c[i+1]
        ans = max(ans, cur)
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
A = [int(i) for i in input().split(' ')]
A.sort()
B = [0]*(A[N-1]+3)

for a in A:
    B[a] += 1
    B[a+1] += 1
    B[a+2] += 1

print(max(B))

Submission status: Accepted

Solution code:
from collections import defaultdict

def solve():
    N = int(input())
    A = map(int, input().split())
    d = defaultdict(lambda:0)
    for a in A:
        d[a]+=1
        d[a-1]+=1
        d[a+1]+=1
    # print(d)
    print(max(d.values()))
    
solve()

Submission status: Accepted

Solution code:
from collections import Counter

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

count_max = 0
C = Counter(A)
for i in range(min(C.keys()), max(C.keys()) + 1):
    count = C.get(i-1, 0) + C.get(i, 0) + C.get(i+1, 0)
    count_max = max(count_max, count)
print(count_max)

Submission status: Accepted

Solution code:
input()
list_ = sorted(map(int, input().rstrip("\n").split()))

dict_ = dict()
for key in list_:
    dict_[key] = dict_.get(key, 0) + 1

max_ = 0
for key in set(list_):
    num = dict_[key] + dict_.get(key - 1, 0) + dict_.get(key + 1, 0)
    max_ = num if num > max_ else max_
print(max_)

Submission status: Accepted

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

    c = [0] * (100000 + 2)
    for ai in A:
        c[ai+1] += 1
        c[ai] += 1
        c[ai+2] += 1

    print(max(c))

if __name__ == "__main__":
    main()

Submission status: Accepted

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

    c = [0] * (100000 + 1)
    for ai in A:
        c[ai] += 1

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

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import sys

N=int(input())

a=[int(t.strip()) for t in input().split()]

num=[]
num= [0] * (max(a)+3)
for i in range(N):
	num[a[i]] += 1
	
maximum=0
for i in range(len(num)-2):
	tmp=num[i] + num[i+1] +num[i+2]
	if maximum < tmp:
		maximum = tmp

print(maximum)		
#print(num)

Submission status: Accepted

Solution code:
N=int(input())
a=list(map(int,input().split()))
m=max(a)
c=[0]*(m+3)

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

if len(c)==1:
    T=1
else:
    T=c[0]+c[1]+c[2]
    for i in range(m):
        T=max(T,c[i-1]+c[i]+c[i+1])
print(T)

Submission status: Accepted

Solution code:
N = int(input())

import collections
a = list(map(int,input().split()))
A = [] * 3 * N
for i in range(len(a)):
    A.append(a[i] - 1)
    A.append(a[i])
    A.append(a[i] + 1)

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

Submission status: Accepted

Solution code:
N = int(input())

import collections
a = list(map(int,input().split()))
b = [[0] * 3 for i in range(N)]
A = [] * 3 * N
for i in range(len(a)):
    b[i][0] = a[i] - 1
    b[i][1] = a[i]
    b[i][2] = a[i] + 1
    A.append(a[i] - 1)
    A.append(a[i])
    A.append(a[i] + 1)

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

Submission status: Accepted

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

mx = max(A)

score = [0]*(mx+3)

for i in A:
    score[i] += 1
    score[i+1] += 1
    score[i+2] += 1

print(max(score))

Submission status: Accepted

Solution code:
import collections

n = int(input())
a = list(map(int, input().split()))
counter = collections.Counter(a)
max_count = 0
for i in range(0, max(a) + 1):
    max_count = max(max_count, counter[i - 1] + counter[i] + counter[i + 1])
print(max_count)

Submission status: Accepted

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

# bucket
count = []
for i in range(10 ** 5):
    count.append(0)

for i in a:
    if i >= 1:
        count[i - 1] += 1
    if i < 10 ** 5 - 1:
        count[i + 1] += 1
    count[i] += 1

print(sorted(count, reverse = True)[0])

Submission status: Accepted

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

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

if len(C)==1:
    print(C[0])
else:
    T=max(C[0]+C[1],C[-1]+C[-2])
    for i in range(1,max(A)):
        T=max(T,C[i-1]+C[i]+C[i+1])
    print(T)

Submission status: Accepted

Solution code:
import sys
from functools import reduce
import copy
import math
from pprint import pprint
import collections
import bisect


sys.setrecursionlimit(4100000)


def inputs(num_of_input):
    ins = [input() for i in range(num_of_input)]
    return ins


def int_inputs(num_of_input):
    ins = [int(input()) for i in range(num_of_input)]
    return ins


def solve(inputs):
    numbers = string_to_int(inputs[0])
    counter = collections.Counter()

    for n in numbers:
        counter[n-1] += 1
        counter[n] += 1
        counter[n+1] += 1

    return counter.most_common(1)[0][1]


def string_to_int(string):
    return list(map(int, string.split()))


if __name__ == "__main__":
    input()
    ret = solve(inputs(1))
    print(ret)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
import bisect

def solve(N: int, a: "List[int]"):
    a.sort()
    answer = 0
    for i in range(N):
        count = bisect.bisect_left(a,a[i]+3)
        answer = max(answer,count-i)
    print(answer)
    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:
import collections
table=collections.defaultdict(int)
N=int(input())
a=list(map(int,input().split()))

for x in a:
  table[x]+=1
  table[x+1]+=1
  table[x-1]+=1
table=list(table.values())
print(max(table))

Submission status: Accepted

Solution code:
N=int(input())
a=list(map(int,input().split()))
d={}
for ai in a:
    if ai not in d:
        d[ai]=1
    else:
        d[ai]+=1

s={}
for ai in d:
    k=d[ai]
    if ai-1 in d:
        k+=d[ai-1]
    if ai+1 in d:
        k+=d[ai+1]
    s[ai]=k

print(max(s.values()))

Submission status: Accepted

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

d={}
for a in A:
	for i in [a-1,a,a+1]:
		if i not in d:
			d[i]=1
		else:
			d[i]+=1

print(max(d.values()))

Submission status: Accepted

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

def addToMap(v):
    if v in m:
        m[v] += 1
    else:
        m[v] = 1

for i in range(n):
    v1, v2, v3 = a[i]-1, a[i], a[i]+1
    addToMap(v1)
    addToMap(v2)
    addToMap(v3)

maxK = 0
maxV = 0

for key, val in m.items():
    if val > maxV:
        maxK = key
        maxV = val

print(maxV)

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(lambda x: int(x), input().split()))
a.sort()
cnt=[]
val=[]
last=-1
for i in a:
    if i == last:
        cnt[-1]+=1
    else:
        cnt.append(1)
        val.append(i)
        last = i

low_val=-10
low_cnt=0
mid_val=0
mid_cnt=0
ans=0
for i, v in enumerate(val):
    if v>=low_val+3:
        ans=max(ans, low_cnt+mid_cnt)
        low_val=v
        low_cnt=cnt[i]
        mid_val=0
        mid_cnt=0
    elif v==low_val+2:
        ans=max(ans, low_cnt+mid_cnt+cnt[i])
        if mid_val>0:
            low_val=mid_val
            low_cnt=mid_cnt
            mid_val=v
            mid_cnt=cnt[i]
        else:
            low_val=v
            low_cnt=cnt[i]
            mid_val=0
            mid_cnt=0
    if v==low_val+1:
        mid_val=v
        mid_cnt=cnt[i]
ans=max(ans, low_cnt+mid_cnt)
print(ans)

Submission status: Accepted

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

for i in range(len(d)):
    if i == 0:
        ans = max(ans, d[i] + d[i + 1])
        continue
    elif i == len(d) - 1:
        ans = max(ans, d[i - 1] + d[i])
        continue
    ans = max(ans, d[i-1] + d[i] + d[i + 1])

print(ans)

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(lambda x: int(x), input().split()))
dic={}
for i in a:
    dic[i]=dic.get(i,0)+1
tmp=dic.get(0,0)+dic.get(1,0)
ans=0
for i in range(2,100000):
    tmp+=dic.get(i,0)
    if tmp>ans:
        ans=tmp
    tmp-=dic.get(i-2,0)
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
dic={}
for i in a:
	if i-1 in dic:
		dic[i-1]+=1
	else:
		dic[i-1]=1
	if i in dic:
		dic[i]+=1
	else:
		dic[i]=1
	if i+1 in dic:
		dic[i+1]+=1
	else:
		dic[i+1]=1
print(max(dic.values()))

Submission status: Accepted

Solution code:
N = int(input())
A = [int(_) for _ in input().split()]
spam = [0 for _ in range(10 ** 5 + 2)]
for a in A:
    spam[a-1] +=1
    spam[a] +=1
    spam[a+1] +=1
print(max(spam))

Submission status: Accepted

Solution code:
n=int(input())
l=[int(i) for i in input().split()]
from collections import defaultdict 
d=defaultdict(int)
for i in l: 
    d[i]+=1 
    d[i+1]+=1 
    d[i-1]+=1 
print(max(d.values()))

Submission status: Accepted

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

for x in a:
   cnt[x-1]+=1
   cnt[x]+=1
   cnt[x+1]+=1
ans = -1
for i in range(len(cnt)):
   if cnt[i] > ans:
      ans = cnt[i]
print(ans)

Submission status: Accepted

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

b = list()
for i in range(100000):
    b.append(0)

for i in range(n):
    b[a[i]] += 1
    
c = list()    
for i in range(100000):
    if(i==0):
        if(b[i]):
            c.append(b[i+1]+b[i])
    elif(i==99999):
        if(b[i]):
            c.append(b[i-1]+b[i])
    else:
        if(b[i]):
            c.append(b[i-1]+b[i+1]+b[i])
        
print(max(c))

Submission status: Accepted

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

cnt = [0 for _ in range(10**5+2)]

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

Submission status: Accepted

Solution code:
import collections

def test():
    n = int(input())
    a = [int(x) for x in input().split()]
    b = collections.Counter(a)
    ans = 0
    for x in range(max(a)+1):
        ex = b[x]
        if x - 1 in b:
            ex += b[x-1]
        if x + 1 in b:
            ex += b[x+1]
        ans = max(ans,ex)

    print(ans)


if __name__ == "__main__":
    test()

Submission status: Accepted

Solution code:
import collections
n = int(input())
list_a = list(map(int, input().split()))
count = collections.Counter(list_a)
max_x = [-1, -1]
for k, v in count.items():
  temp_num = v
  if k-1 in count.keys():
    temp_num += count[k-1]
  if k+1 in count.keys():
    temp_num += count[k+1]
  if temp_num > max_x[1]:
    max_x[0] = k
    max_x[1] = temp_num
print(max_x[1])

Submission status: Accepted

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

Submission status: Accepted

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

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

for i in range(N):
  res[a[i]-1]+=1
  res[a[i]]+=1
  res[a[i]+1]+=1
print(max(res))

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int,input().split()))
cnt={}
for i in range(min(a)-1,max(a)+2):
    cnt[i]=0
for i in a:
    if i in cnt:
        cnt[i]+=1
M=0
for i in a:
    if M < cnt[i-1]+cnt[i]+cnt[i+1]:
        M = cnt[i-1]+cnt[i]+cnt[i+1]
print(M)

Submission status: Accepted

Solution code:
import collections

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

c = collections.Counter(a)
ans = 0

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import defaultdict

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

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

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

Submission status: Accepted

Solution code:
N = int(input())
a = list(map(int,input().split()))
X = [0] * (max(a) + 3)

for i in a:
    X[i] += 1
    X[i + 1] += 1
    X[i + 2] += 1
print(max(X))

Submission status: Accepted

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

for n in range(N):
  plus=li[n]+1
  minus=li[n]-1
  li.append(plus)
  li.append(minus)

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

Submission status: Accepted

Solution code:
import collections

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

c = collections.Counter(a)

max_value = -1
for ci in c.most_common():
    cnt = ci[1]

    index = ci[0]
    if index-1 in c:
        cnt += c[index-1]
    if index +1 in c:
        cnt += c[index+1]
    max_value = max(max_value, cnt)

print(max_value)

Submission status: Accepted

Solution code:
n = int(input())
a = [0 for _ in range(10**5 +2)]
b = [0 for _ in range(10**5 +2)]
c = [0 for _ in range(10**5 +2)]

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

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

ans = [x+y+z for x,y,z in zip(a,b,c)]
print(max(ans))

Submission status: Accepted

Solution code:
import collections

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

a_count = collections.Counter(a)

ans_list = []
for x in range(min(a), max(a)+1):
    ans_list.append(a_count[x-1] + a_count[x] + a_count[x+1])
        
print(max(ans_list))

Submission status: Accepted

Solution code:
from collections import Counter
n,*a=map(int,open(0).read().split())
c=Counter(a)
ans=0
for i in c.keys():
    ans=max(ans,c[i-1]+c[i]+c[i+1])
print(ans)

Submission status: Accepted

Solution code:
def input():
    import sys
    return sys.stdin.readline().rstrip()


def main():
    import math
    import collections
    import itertools

    n = int(input())
    as_ = list(map(int, input().split()))
    counter_a_tmp = collections.Counter(as_)
    counter_a = collections.defaultdict(int)
    for key, count in counter_a_tmp.items():
        counter_a[key] = count

    ans = 0
    if max(as_) == 0:
        print(n)
    else:
        for i in range(0, max(as_) - 1):
            ans = max(ans, counter_a[i] + counter_a[i + 1] + counter_a[i + 2])

        print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
# ABC072 C - Together
from collections import Counter
N = int(input())
A = list(map(str,input().split()))
L = [0]*100001
C = Counter(A)
C = C.items()

if N == 1:
    ans = 1
else:
    for c in C:
        idx,v = c
        idx = int(idx)
        L[idx] = v

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

print(ans)

Submission status: Accepted

Solution code:
def main():
  n = int(input())
  ans = 0
  a_lis = list(map(int,input().split()))
  A_dict = [0]*(10**5+1)
  for i in a_lis:
    A_dict[i] += 1
  for j in range(1,10**5):
    tmp = A_dict[j-1]+A_dict[j]+A_dict[j+1]
    if tmp > ans:
      ans = tmp
  print(ans)

if __name__=='__main__':
  main()

Submission status: Accepted

Solution code:
from collections import defaultdict

n = int(input())
ans = 0

a_lis = list(map(int,input().split()))
A_dict = [0]*(10**5+1)

for i in a_lis:
  A_dict[i] += 1

for j in range(1,10**5):
  tmp = A_dict[j-1]+A_dict[j]+A_dict[j+1]
  if tmp > ans:
    ans = tmp
print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict

n = int(input())

a_lis = list(map(int,input().split()))
A_dict = defaultdict(int)

for i in a_lis:
  A_dict[i] += 1
  
max_cnt = 0

for j in range(1, 10**5):
  tmp = A_dict[j-1]+A_dict[j]+A_dict[j+1]
  max_cnt = max(max_cnt, tmp)
print(max_cnt)

Submission status: Accepted

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

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

for i in range(n):
    Counter[A[i]] += 1

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

print(ans)

Submission status: Accepted

Solution code:
def slove():
    import sys
    import collections
    input = sys.stdin.readline
    n = int(input().rstrip('\n'))
    a = list(map(int, input().rstrip('\n').split()))
    b = []
    for i in range(len(a)):
        b.append(a[i])
        b.append(a[i]+1)
        b.append(a[i]-1)
    b = collections.Counter(b).most_common()
    print(b[0][1])


if __name__ == '__main__':
    slove()

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split()))
z = 100002*[0]

for a in A:
  z[a]+=1
  z[a+1]+=1
  z[a+2]+=1

print(max(z))

Submission status: Accepted

Solution code:
import collections
N = int(input())
A = [int(i) for i in input().split()]
Dict = dict(collections.Counter(A))
Cnt = []
for key in Dict.keys():
    cnt = Dict[key]
    Cnt.append([key,cnt])
Cnt.sort()
dp = [0 for i in range(len(Cnt)+1)]
Cnt.append([float('inf'),0])
if len(Cnt)<3:
    print(Cnt[0][1])
    exit()
else:
    if Cnt[1][0]-1==Cnt[0][0]:
        dp[0] = Cnt[0][1]+Cnt[1][1]
for i in range(1,len(Cnt)-1):
    if Cnt[i][0]==Cnt[i-1][0]+1:
        dp[i] += Cnt[i-1][1]
    if Cnt[i+1][0]-1==Cnt[i][0]:
        dp[i]+=Cnt[i+1][1]
    dp[i] += Cnt[i][1]
print(max(dp))

Submission status: Accepted

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

cnt = [0] * 1000010
for i in a:
    cnt[i] += 1
    cnt[i+1] += 1
    cnt[i-1] += 1

print(max(cnt))

Submission status: Accepted

Solution code:
Dic = {}

def adddic(n):
    if n in Dic:
        Dic[n] += 1
    else:
        Dic[n] = 1

N = int(input())
D = list(map(int,input().split()))
for n in range(N):
    adddic(D[n]-1)
    adddic(D[n])
    adddic(D[n]+1)
ans = 0
for key,value in Dic.items():
    ans = max(ans,value)
print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict as dd
N = int(input())
D = dd(int)
A = list(map(int, input().split()))

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

Submission status: Accepted

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

Submission status: Accepted

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