practivceAlgorithm/codeforce

[codeforce][Python] #719 D.Same Differences

findTheValue 2021. 10. 17. 18:57

ai - bj = i - j를 ai - i = bj - j 로 치환시켜 dictionary에 저장 후 조합의 수만큼 더해줌.

 

import sys
input = sys.stdin.readline
from collections import defaultdict

for test in range(int(input())):
    n = int(input())
    arr = list(map(int, input().split()))
    new_arr = defaultdict(int)
    for idx, num in enumerate(arr):
        new_arr[num - idx] += 1
    answer = 0
    for num in new_arr:
        k = new_arr[num] 
        answer += k * (k - 1) // 2
    print(answer)