practivceAlgorithm/codeforce

[codeforce][Python] #690 E1. Close Tuples (easy version)

findTheValue 2021. 12. 7. 22:16

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)