alexjercan/bugnet huggingface dataset:
Problem p02689 (Python) — buggy submission, verdict: Time Limit Exceeded

n, m = map(int, input().split())
h = list(map(int, input().split()))
ab = [tuple(map(int, input().split())) for _ in range(m)]
dic = {}
ban = {}
for i in range(m):
    a = ab[i][0]
    b = ab[i][1]

    if dic.get(a):
        dic[a] = max(dic[a], h[b - 1])
    else:
        dic[a] = h[b - 1]

    if dic.get(b):
        dic[b] = max(dic[b], h[a - 1])
    else:
        dic[b] = h[a - 1]
ans = 0
for i in range(n):
    if dic.get(i + 1):
        if dic[i + 1] < h[i]:
            ans += 1
    else:
        ans += 1
print(ans)


Error: TLE

Fixed submission:

n, m = map(int, input().split())
h = list(map(int, input().split()))
ab = [tuple(map(int, input().split())) for _ in range(m)]
dic = {}
ban = {}
for i in range(m):
    a = ab[i][0]
    b = ab[i][1]
    if h[a - 1] < h[b - 1]:
        ban[a] = True
    elif h[a - 1] == h[b - 1]:
        ban[a] = True
        ban[b] = True
    else:
        ban[b] = True

    if dic.get(a):
        dic[a].append(b)
    else:
        dic[a] = [b]

    if dic.get(b):
        dic[b].append(a)
    else:
        dic[b] = [a]
    dic[a] = list(set(dic[a]))
    dic[b] = list(set(dic[b]))
ans = 0
for i in range(n):
    if dic.get(i + 1):
        if ban.get(i + 1):
            if ban[i + 1]:
                continue
        flg = True
        for j in range(len(dic[i + 1])):
            if h[i] <= h[dic[i + 1][j] - 1]:
                flg = False
                break
        if flg:
            ans += 1
    else:
        ans += 1
print(ans)
Problem p02690 (Python) — buggy submission, verdict: Runtime Error

from itertools import combinations

x = int(input())
num = [i**5 for i in range(1000)]

for a, b in combinations(num, 2):
    if a - b == x:
        print(int(a**0.2), int(b**0.2))
    elif a + b == x:
        print(int(a**0.2), -int(b**0.2))
    elif b - a == x:
        print(int(b**0.2), int(a**0.2))
    else:
        continue
    break


Error: TypeError: int() argument must be a string, a bytes-like object or a real number, not 'complex'
Traceback (most recent call last):
  File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02690/Python/s068916445.py", line 8, in <module>
    print(int(a**0.2), int(b**0.2))
TypeError: int() argument must be a string, a bytes-like object or a real number, not 'complex'


Fixed submission:

from itertools import product

x = int(input())
num = [i**5 for i in range(-1000, 1000)]

for a, b in product(num, repeat=2):
    if a - b == x:
        print(int(a**0.2), int(b**0.2))
        break
Problem p02690 (Python) — buggy submission, verdict: Time Limit Exceeded

x = int(input())

l5 = [i**5 for i in range(0, 10000)]
for a5 in l5:
    for b5 in l5:
        if x == a5 - b5:
            print(int(a5 ** (1 / 5)), int(b5 ** (1 / 5)))
            exit()
        elif x == a5 + b5:
            print(int(a5 ** (1 / 5)), int((-1) * b5 ** (1 / 5)))
            exit()


Error: TLE

Fixed submission:

x = int(input())

for a in range(9999):
    for b in range(9999):
        if a**5 - b**5 == x:
            print(a, b)
            exit()
Problem p02690 (Python) — buggy submission, verdict: Runtime Error

import numpy as np


def main():
    x = int(input())
    A = np.arange(-120, 120)
    a = A**5
    for i, a_1 in enumerate(a):
        for j, a_2 in enumerate(a):
            if a_1 - a_2 == x:
                print(A[i], A[j])
                return


if __name__ == "__main__":
    main()


Error: WA

Fixed submission:

x = int(input())
for i in range(x // 2 + 1):
    if pow(x - pow(i, 5), 1 / 5).is_integer():
        print(i, int(-pow(x - pow(i, 5), 1 / 5)))
        exit()
Problem p02690 (Python) — buggy submission, verdict: Runtime Error

x = int(input())


i = 0
a, b = 0, 0
while i**5 < x:
    i += 1
mx = i
for k in range(2 * (mx + 1)):
    for j in range(-k, k + 1):
        if k**5 - j**5 == x:
            a, b = k, j
print(a, b)


Error: 0

Fixed submission:

x = int(input())


i = 0

while i**5 < x:
    i += 1
for k in range(i):
    for j in range(-i, i + 1):
        if k**5 - j**5 == x:
            a, b = k, j
print(a, b)
Problem p02690 (Python) — buggy submission, verdict: Runtime Error

# 暴力即可
# 範囲指定 A=100 B=-100で20000000000とかになるしどっちも|100|程度でいいのではないか
# なんか足りんみたい（なんで？）なので4X^2<=10^9なる最大のXを求めよう
# 2X = 1000ちょい だから500くらいあればいいのか
# 1000^2なので時間的には間に合いそう
x = int(input())
for a in range(-500, 501):
    for b in range(-500, 501):
        if a**5 - b**5 == x:
            print(a, b)
            exit()
print(3 / 0)  # はい


Error: WA

Fixed submission:

# 暴力即可
# 範囲指定 A=100 B=-100で20000000000とかになるしどっちも|100|程度でいいのではないか
x = int(input())
for a in range(-100, 101):
    for b in range(-100, 101):
        if a**5 - b**5 == x:
            print(a, b)
            exit()
print(3 / 0)  # はい
Problem p02690 (Python) — buggy submission, verdict: Runtime Error

x = int(input())

for i in range(1000):
    for j in range(1000):
        if i**5 - j**5 == x:
            print(i, j)
            exit()
        elif i**5 - (-j) ** 5 == x:
            print(i, -j)
            exit()


Error: 0

Fixed submission:

x = int(input())
det = 1001001001
for i in range(1000000):
    if abs(i**5 - x) < abs(det):
        det = i**5 - x
        a = i
    else:
        break

if det < 0:
    for i in range(-1, -1000000, -1):
        if i**5 == det:
            b = i
            break
elif det == 0:
    b = 0
else:
    for i in range(1000000):
        if i**5 == det:
            b = i
            break


print(a, b)
Problem p02690 (Python) — buggy submission, verdict: Time Limit Exceeded

def get_divisor(n: int) -> list:
    divisor = []
    for i in range(1, n + 1):
        if i * i > n:
            break
        if n % i == 0:
            divisor.append(i)
            if n // i != i:
                divisor.append(n // i)
    # divisor.sort() # if you want sorted divisors
    return divisor


x = int(input())
cand = get_divisor(x)
# print(cand)
for d in cand:
    if d**5 == x:
        print(d, 0)
    elif d**5 < x:
        a = d
        b = 0
        while a**5 - b**5 < x:
            a += 1
            b += 1
        if a**5 - b**5 == x:
            print(a, b)
            break

        a = 0
        b = -d
        while a**5 - b**5 < x:
            a -= 1
            b -= 1
        if a**5 - b**5 == x:
            print(a, b)
            break
    else:
        if d % 2 == 0 and (d // 2) ** 5 + (d // 2) ** 5 > x:
            continue
        if d % 2 == 1 and ((d + 1) // 2) ** 5 + (d // 2) ** 5 > x:
            continue
        a = d
        b = 0
        while a**5 - b**5 > x:
            a -= 1
            b -= 1
            # print(a, b, a**5 - b**5)
        if a**5 - b**5 == x:
            print(a, b)
            break

        a = 0
        b = -d
        while a**5 - b**5 > x:
            a += 1
            b += 1
        if a**5 - b**5 == x:
            print(a, b)
            break


Error: TLE

Fixed submission:

def get_divisor(n: int) -> list:
    divisor = []
    for i in range(1, n + 1):
        if i * i > n:
            break
        if n % i == 0:
            divisor.append(i)
            if n // i != i:
                divisor.append(n // i)
    # divisor.sort() # if you want sorted divisors
    return divisor


x = int(input())
cand = get_divisor(x)
# print(cand)
for d in cand:
    a = d
    b = 0
    while a**5 - b**5 < x:
        a += 1
        b += 1
    if a**5 - b**5 == x:
        print(a, b)
        break

    is_found = False
    for i in range(1, d):
        a = i
        b = i - d
        if a**5 - b**5 == x:
            print(a, b)
            is_found = True
            break
    if is_found:
        break

    a = 0
    b = -d
    while a**5 - b**5 < x:
        a -= 1
        b -= 1
    if a**5 - b**5 == x:
        print(a, b)
        break
Problem p02690 (Python) — buggy submission, verdict: Runtime Error

X = int(input())

R = 3000
go = [i**5 for i in range(R)]


def foo():
    for i in range(R):
        for j in range(R):
            if go[i] - go[j] == X:
                return i, j
        for j in range(i, R):
            if go[i] + go[j] == X:
                return i, -j

    return 0, 0


A, B = foo()
print(A, B)


Error: WA

Fixed submission:

X = int(input())

go = [i**5 for i in range(1000)]


def foo():
    for i in range(1000):
        for j in range(i, 1000):
            if go[i] - go[j] == X:
                return i, j
        for j in range(1000):
            if go[i] + go[j] == X:
                return i, -j


A, B = foo()
print(A, B)
Problem p02690 (Python) — buggy submission, verdict: Time Limit Exceeded

def f(X):
    for i in range(1, 120):
        for j in range(-63, 119):
            if i**5 - j**5 == X:
                return i, j


X = int(input())
print(*f(X))


Error: TLE

Fixed submission:

X = int(input())
a, b = 0, -24
while a**5 - b**5 != X:
    a += 1
    b = -24
    while a**5 - b**5 != X:
        b += 1
print(a, b)
Problem p02690 (Python) — buggy submission, verdict: Time Limit Exceeded

x = int(input())
for a in range(-1000, 1000):
    for b in range(-1000, 1000):
        if a**5 - b**5 == x:
            print(a, b)
            quit()


Error: TLE

Fixed submission:

x = int(input())
a = int(x**0.2)
b = 0
while a**5 - b**5 != x:
    b -= 1
print(a, b)
Problem p02690 (Python) — buggy submission, verdict: Runtime Error

X = int(input())

numbers = dict()

for i in range(-200, 200):
    numbers.setdefault(i, i**5)

for i in range(-200, 200):
    for j in range(-200, 200):
        if numbers[i] - numbers[j] == X:
            output = [i, j]
            break

print(output[0], output[1])


Error: 0

Fixed submission:

X = int(input())

numbers = dict()

for i in range(-101, 101):
    numbers.setdefault(i, i**5)


for i in range(-101, 101):
    for j in range(-101, 101):
        if numbers[i] - numbers[j] == X:
            output = [i, j]
            break
print(output[0], output[1])
Problem p02690 (Python) — buggy submission, verdict: Time Limit Exceeded

def f(B):
    if isinstance(B, float):
        if abs(B - int(B)) < 10**-6:
            return True
        else:
            return False
    return False


X = int(input())

for a in range(0, 10**5):
    bb = X + a**5
    B = bb**0.2
    if f(B):
        print(int(B), a)
        break

    a *= -1
    bb = X + a**5
    B = bb**0.2
    if f(B):
        print(int(B), a)
        break


Error: TLE

Fixed submission:

X = int(input())

for a in range(0, 10**9):
    a *= a
    bb = X + a**5
    B = bb**0.2

    if isinstance(B, float):
        if B.is_integer():
            print(int(B), a)
            break

    a *= -1
    bb = X + a**5
    B = bb**0.2
    if isinstance(B, float):
        if B.is_integer():
            print(int(B), a)
            break
Problem p02691 (Python) — buggy submission, verdict: Time Limit Exceeded

n = int(input())
A = list(map(int, input().split()))
A_j = {}
for i, a in enumerate(A):
    if A_j.get(i - a):
        A_j[i - a] += 1
    else:
        A_j[i - a] = 1

r = 0
for i, a in enumerate(A):
    t = A_j.get(a + i)
    if t:
        r += t

print(r)


Error: TLE

Fixed submission:

n = int(input())
A = list(map(int, input().split()))
A_i = {}
A_j = {}
for i, a in enumerate(A):
    A_i[i] = a + i
    A_j[i] = i - a

r = 0
for i in range(n):
    for j in range(i + 1, n):
        if A_i[i] == A_j[j]:
            r += 1

print(r)
Problem p02691 (Python) — buggy submission, verdict: Time Limit Exceeded

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

cnt1 = {}
cnt2 = {}

for i in range(N):
    tmp1 = A[i] + i + 1
    tmp2 = i + 1 - A[i]
    if tmp1 not in cnt1:
        cnt1[tmp1] = 1
    else:
        cnt1[tmp1] += 1
    if tmp2 not in cnt2:
        cnt2[tmp2] = 1
    else:
        cnt2[tmp2] += 1

diff = set(list(cnt1.keys())) & set(list(cnt2.keys()))

ans = 0
for d in diff:
    ans += cnt1[d] * cnt2[d]
print(ans)


Error: TLE

Fixed submission:

N = int(input())
A = list(map(int, input().split()))
diff_i = [A[i] + i + 1 for i in range(N)]
diff_j = [i + 1 - A[i] for i in range(N)]
diff = set(diff_i) & set(diff_j)
ans = 0
for d in diff:
    ans += diff_i.count(d) * diff_j.count(d)
print(ans)
Problem p02691 (Python) — buggy submission, verdict: Time Limit Exceeded

from collections import defaultdict

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

d = defaultdict(int)
ans = 0
for i in range(n):
    ans += d[i - a[i]]
    d[a[i] + i] += 1

print(ans)


Error: TLE

Fixed submission:

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

# i < j
# j - i = a[i] + a[j]
# a[i] + i = j - a[j]

count = 0
candidate = []
for i in range(n):
    sa = i - a[i]
    count += candidate.count(sa)
    wa = i + a[i]
    candidate.append(wa)

print(count)
Problem p02691 (Python) — buggy submission, verdict: Time Limit Exceeded

from collections import defaultdict

N = int(input())
A = [0] + list(map(int, input().split()))
d = defaultdict(int)

ans = 0
for i in range(1, N + 1):
    d[A[i] + i] += 1  # Ai + iを数える
    ans += d[i - A[i]]

print(ans)


Error: TLE

Fixed submission:

N = int(input())
tmp_A = list(map(int, input().split()))
A = [0] + tmp_A

tmp_A = sorted(tmp_A)
min_h = sum(tmp_A[0:2])
max_h = sum(tmp_A[-2:])

ans = 0
for n in range(1, N + 1):
    now_h = A[n]
    min_i = max(n + 1, min(min_h, N))
    max_i = min(max_h + n + 1, N + 1)
    for i in range(min_i, max_i):
        if A[n] + A[i] is abs(n - i):
            ans += 1

print(ans)
Problem p02691 (Python) — buggy submission, verdict: Time Limit Exceeded

n = int(input())
l = {}
r = {}
xc = 0
for x in input().split():
    xi = int(x)
    lx = xc + xi
    if lx in l:
        l[lx] += 1
    else:
        l[lx] = 1
    rx = xc - xi
    if rx in r:
        r[rx] += 1
    else:
        r[rx] = 1
    xc += 1
c = 0
for x in l:
    if x in r:
        c += l[x] * r[x]
print(c)


Error: TLE

Fixed submission:

n = int(input())
a = [int(x) for x in input().split()]
c = 0
for i in range(n):
    for j in range(i - 1):
        if i - j == a[i] + a[j]:
            c += 1
print(c)
Problem p02691 (Python) — buggy submission, verdict: Time Limit Exceeded

from collections import Counter


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

a_plus_idx = Counter([a[i] + i for i in range(n)])
a_minus_idx = Counter([i - a[i] for i in range(n)])

total = 0
for k, v in a_plus_idx.items():
    total += v * a_minus_idx[k]

print(total)


Error: TLE

Fixed submission:

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

cnt = 0
for i in range(n - 1):
    for j in range(i + a[i], n):
        if j - i == a[i] + a[j]:
            cnt += 1
print(cnt)
Problem p02691 (Python) — buggy submission, verdict: Time Limit Exceeded

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

c1 = {}
c2 = {}

for i in range(N):
    c1.setdefault(i + A[i], 0)
    c1[i + A[i]] += 1
    c2.setdefault(i - A[i], 0)
    c2[i - A[i]] += 1

result = 0
for k in set(c1).intersection(c2):
    result += c1[k] * c2[k]
print(result)


Error: TLE

Fixed submission:

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

result = 0
for i in range(N):
    a = A[i]
    b = a + i
    for j in range(1, N - a - i):
        if j == A[j + b]:
            result += 1
print(result)
Problem p02691 (Python) — buggy submission, verdict: Runtime Error

def main():
    n = int(input())
    a = list(map(int, input().split()))
    d = [0] * n
    ans = 0
    for i in range(n):
        ai = a[i]
        l, r = i + ai, i - ai
        if 0 <= l < n:
            d[l] += 1
        if 0 <= r < n:
            ans += d[r]
    print(ans)


if __name__ == "__main__":
    main()


Error: WA

Fixed submission:

def main():
    n = int(input())
    a = list(map(int, input().split()))
    d = [0] * n
    ans = 0
    for i in range(n):
        ai = a[i]
        l, r = i + ai, i - ai
        if 0 <= l < n:
            d[l] += 1
        if 0 <= r < n:
            ans += d[r]
        print(d)
    print(ans)


if __name__ == "__main__":
    main()
Problem p02691 (Python) — buggy submission, verdict: Time Limit Exceeded

from collections import Counter

N = int(input())
A = list(map(int, input().split()))
count = 0
l_array = []
r_array = []
for i, X in enumerate(A):
    l_array.append(i + X)
    r_array.append(i - X)

counter = Counter(r_array)

for x in l_array:
    count += counter.get(x, 0)
print(count)


Error: TLE

Fixed submission:

N = int(input())
A = list(map(int, input().split()))
count = 0
l_array = []
r_array = []
for i, X in enumerate(A):
    l_array.append(i + X)
    r_array.append(i - X)

for x in l_array:
    count += r_array.count(x)
print(count)
Problem p02691 (Python) — buggy submission, verdict: Time Limit Exceeded

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

d = {}

for i, a in enumerate(A):
    tmp = i + 1 - a
    if tmp in d.keys():
        d[tmp] += 1
    else:
        d[tmp] = 1

# print(d)

for i, a in enumerate(A):
    tmp = a + i + 1
    tmptmp = i + 1 - a
    if tmp in d.keys():
        ans += d[tmp]
    if tmptmp in d.keys():
        d[tmptmp] = max(0, d[tmptmp] - 1)

print(ans)


Error: TLE

Fixed submission:

N = int(input())
A = list(map(int, input().split()))
ans = 0
for i in range(N):
    a = A[i]
    for j in range(min(A[i] + i, N), N):
        if a + A[j] == j - i:
            ans += 1

print(ans)
Problem p02691 (Python) — buggy submission, verdict: Time Limit Exceeded

n = int(input().strip())
a = list(map(int, input().split()))
dic = {}
ans = 0
for i in range(len(a)):
    if dic.get(i - a[i]):
        ans += dic[i - a[i]]
    if dic.get(a[i] + i):
        dic[a[i] + i] += 1
    else:
        dic[a[i] + i] = 1
print(ans)


Error: TLE

Fixed submission:

n = int(input().strip())
a = list(map(int, input().split()))
left = {}
for i in range(len(a)):
    if left.get(a[i] + i):
        left[a[i] + i].append(i)
    else:
        left[a[i] + i] = [i]
ans = 0
for i in range(len(a)):
    if left.get(i - a[i]):
        ans += len(list(filter(lambda x: x < i, left[i - a[i]])))
print(ans)
Problem p02691 (Python) — buggy submission, verdict: Time Limit Exceeded

# -*- coding: utf-8 -*-
# 整数の入力
n = int(input())
h_list = input().split()
h_list = [int(h) for h in h_list]

count = 0
result_dict = {}

for i in range(n):
    key = (i + 1) + h_list[i]
    value = result_dict.get(key, 0)
    result_dict[key] = value + 1

for j in range(n):
    key = (j + 1) - h_list[j]
    count += result_dict.get(key, 0)

print(count)


Error: TLE

Fixed submission:

# -*- coding: utf-8 -*-
# 整数の入力
n = int(input())
h_list = input().split()
h_list = [int(h) for h in h_list]

count = 0

for i in range(n):
    a_height = h_list[i]
    n_aa = i + a_height + 1000000000 + 1
    if n_aa > n:
        n_aa = n
    for k in range(i + 1 + a_height, n_aa):
        b_height = h_list[k]
        if k - i == a_height + b_height:
            count += 1

print(count)
Problem p02691 (Python) — buggy submission, verdict: Time Limit Exceeded

n = int(input())
A = [int(x) for x in input().split()]
backward = [0] * n
forward = [0] * n
for i, a in enumerate(A):
    if i + a < n:
        forward[i + a] += 1
    if i - a >= 0:
        backward[i - a] += 1
ans = 0
for x, y in zip(forward, backward):
    ans += x * y
print(ans)


Error: TLE

Fixed submission:

n = int(input())
A = list(map(int, input().split()))
ans = 0
for i in range(n - 1):
    for j in range(i + 1, n):
        if j - i == A[i] + A[j]:
            ans += 1
print(ans)
Problem p02691 (Python) — buggy submission, verdict: Time Limit Exceeded

import sys
from collections import defaultdict


def input():
    return sys.stdin.readline().strip()


def main():
    N = int(input())
    A = list(map(int, input().split()))
    L = defaultdict(list)
    R = defaultdict(list)
    for i in range(N):
        L[A[i] + i].append(i)
        R[i - A[i]].append(i)
    K = list(L.keys())

    answer = 0
    for k in K:
        l = len(L[k])
        r = len(R[k])
        answer += l * r
    print(answer)


if __name__ == "__main__":
    main()


Error: TLE

Fixed submission:

import sys


def input():
    return sys.stdin.readline().strip()


def main():
    N = int(input())
    A = list(map(int, input().split()))
    answer = 0
    for i in range(N):
        for j in range(i + A[i] + 1, N):
            if A[i] + A[j] == j - i:
                answer += 1
    print(answer)


if __name__ == "__main__":
    main()
Problem p02691 (Python) — buggy submission, verdict: Time Limit Exceeded

from _collections import defaultdict
import sys

read = sys.stdin.buffer.read
N = int(input())
A = [int(i) for i in input().split()]
D = defaultdict(int)

for i, a in enumerate(A, 1):
    D[i - a] += 1

ans = 0
for i, a in enumerate(A, 1):
    ans += D[i + a]
print(ans)


Error: TLE

Fixed submission:

import numpy as np

N = int(input())
A = np.array(input().split(), np.int32)
S = np.array([A[i] - i for i in range(N)])
T = np.array([-A[i] - i for i in range(N)])
# j > i
# S[j] == T[i]
ans = 0
for j in range(N):
    for i in range(j - 1, -1, -1):
        if S[j] == T[i]:
            ans += 1
print(ans)
Problem p02691 (Python) — buggy submission, verdict: Time Limit Exceeded

from bisect import bisect_left, bisect_right

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

li = []
for i in range(N):
    li.append(A[i] + i)

li.sort()

cnt = 0
for j in range(N):
    target = j - A[j]
    cnt += bisect_right(li, target) - bisect_left(li, target)

print(cnt)


Error: TLE

Fixed submission:

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

li = []
for i in range(N):
    li.append(A[i] + i)

li.sort()

cnt = 0
for j in range(N):
    cnt += li.count(j - A[j])

print(cnt)
Problem p02691 (Python) — buggy submission, verdict: Time Limit Exceeded

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

ans = 0
dic1 = {}
dic2 = {}
for i in range(n):
    tmp1 = a[i] + (i + 1)
    tmp2 = -a[i] + (i + 1)
    if tmp1 in dic1:
        dic1[tmp1] += 1
    else:
        dic1[tmp1] = 1
    if tmp2 in dic2:
        dic2[tmp2] += 1
    else:
        dic2[tmp2] = 1
for key in dic1.keys():
    if key in dic2:
        ans += dic1[key] * dic2[key]
print(ans)


Error: TLE

Fixed submission:

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

ans = 0
for i in range(n - 1):
    for j in range(i + 1, n):
        if j - i == a[i] + a[j]:
            ans += 1
print(ans)
Problem p02691 (Python) — buggy submission, verdict: Time Limit Exceeded

from collections import defaultdict

n = int(input())
height = list(map(int, input().split()))
add_set = defaultdict(int)
ans = 0
for i in range(n):
    add_set[i + height[i] + 1] += 1
    if i + 1 - height[i] in add_set:
        ans += add_set[i + 1 - height[i]]

print(ans)


Error: TLE

Fixed submission:

n = int(input())
height = list(map(int, input().split()))
ans = 0
for i in range(n - 1):
    for j in range(i + 1 + height[i], n):
        if abs(j - i) == (height[i] + height[j]):
            ans += 1
print(ans)
Problem p02691 (Python) — buggy submission, verdict: Time Limit Exceeded

from collections import Counter


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

# R = [None] * (N + 1)
# R[N] = Counter()

R = Counter()
cnt = 0
for n in range(N - 1, 0, -1):
    R[(n + 1) - A[n]] += 1
    c = A[n - 1] + ((n - 1) + 1)
    cnt += R[c]

# ans = 0
# for i, Ai in enumerate(A):
#     # n < a
#     # c0 = i - a
#     # l = L[i][c0]

#     # n >= a
#     c1 = Ai + (i + 1)
#     r = R[i + 1][c1]

#     ans += r

print(cnt)


Error: TLE

Fixed submission:

from collections import Counter


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

R = [None] * (N + 1)
R[N] = Counter()
L = [None] * (N + 1)
L[0] = Counter()

# n < i
# for i in range(N):
#     c = Counter()
#     c[A[i] + i] = 1
#     L[i + 1] = L[i] | c

# n >= i
c = Counter()
for n in range(N - 1, -1, -1):
    R[n] = R[n + 1] | c
    R[n][(n + 1) - A[n]] += 1

ans = 0
for i, Ai in enumerate(A):
    # n < a
    # c0 = i - a
    # l = L[i][c0]

    # n >= a
    c1 = Ai + (i + 1)
    r = R[i + 1][c1]

    ans += r

print(ans)