map으로 나보다 2큰수의 위치를 저장해둔 후 거기까지 사이의 갯수에서 나포함 3개를 뽑으면 된다.
import sys
input = sys.stdin.readline
from bisect import bisect_left
for test in range(int(input())):
n = int(input())
arr = list(map(int, input().split()))
arr.sort()
done = {}
answer = 0
for i in range(n - 2):
if arr[i] not in done:
right = bisect_left(arr, arr[i] + 3)
done[arr[i]] = right
length = done[arr[i]] - i
if length > 2:
length -= 2
answer += length * (length + 1) // 2
print(answer)
'practivceAlgorithm > codeforce' 카테고리의 다른 글
[codeforce][Python] #690 C. Unique Number (0) | 2021.12.07 |
---|---|
[codeforce][Python] #690 B. Last Year's Substring (0) | 2021.12.07 |
[codeforce][Python] #690 A. Favorite Sequence (0) | 2021.12.07 |
[Codeforce][Python] #702 F. Equalize the Array (0) | 2021.11.07 |
[Codeforce][Python] #702 D. Permutation Transformation (0) | 2021.11.07 |