题解 | 相差不超过k的最多数
from re import L
import sys
n, k = map(int, input().split())
ar = list(map(int, input().split()))
ar.sort()
L, R = 0, 0
res = 0
while R < len(ar):
while R < len(ar) and ar[L] + k >= ar[R]:
R += 1
res = max(res, R - L)
L += 1
print(res)
酷炫双指针

查看14道真题和解析