qyliang/CodeNanoFix huggingface dataset:
Buggy code:
import bisect
n = int(input())
ln = list(map(int, input().split()))
ln.sort()
ans = 0

for i in range(n-2):
    for j in range(i+1, n-1):
        a = ln[i]
        b = ln[j]
        index = bisect.bisect_left(ln, a+b)-1
        print(i,j, index)
        print(a, b, ln[index-1])
        ans += index - j
print(ans)

Fixed code:
import bisect
n = int(input())
ln = list(map(int, input().split()))
ln.sort()
ans = 0

for i in range(n-2):
    for j in range(i+1, n-1):
        a = ln[i]
        b = ln[j]
        index = bisect.bisect_left(ln, a+b)-1
        ans += index - j
print(ans)
Buggy code:

def findnumberofTriangles(arr): 
	n = len(arr) 
	arr.sort() 
	count = 0
	for i in range(0, n-2):  
		k = i + 2 
		for j in range(i + 1, n):  
			while (k < n and arr[i] + arr[j] > arr[k]): 
				k += 1
			if(k>j): 
				count += k - j - 1

	return count 

n = int(input())
arr=[int(x) for x in input().split()]
print(findnumberoftriangles(arr))



Fixed code:

def findnumberofTriangles(arr): 
	n = len(arr) 
	arr.sort() 
	count = 0
	for i in range(0, n-2):  
		k = i + 2 
		for j in range(i + 1, n):  
			while (k < n and arr[i] + arr[j] > arr[k]): 
				k += 1
			if(k>j): 
				count += k - j - 1

	return count 

n = int(input())
arr=[int(x) for x in input().split()]
print(findnumberofTriangles(arr))
Buggy code:
from collections import defaultdict
import bisect
def main():
    _ = int(input())
    l = list(map(int,input().split()))
    d = defaultdict(int)
    for i in l:
        d[i] += 1
    dk = sorted(list(d.keys()))
    dk2 = [i*2 for i in dk]
    ans = 0
    while dk:
        x = dk[-1]
        _ = dk2.pop(-1)
        bs = bisect.bisect(dk2,x)
        tmp = dk[bs:]
        if not tmp:
            break
        for i in tmp:
            for j in dk:
                if i+j > x and i >= j:
                    if i == j and i == x:
                        ans += max([0,d[x]*(d[x]-1)*(d[x]-2)//6])
                    elif i == j:
                        ans += d[x]*d[i]*(d[i]-1)//2
                    else:
                        ans += d[x]*d[i]*d[j]
        del dk[-1]
        if len(dk) < 3:
            break
    print(ans)
if __name__ == '__main__':
    main()

Fixed code:
from collections import defaultdict
import bisect
def main():
    _ = int(input())
    l = list(map(int,input().split()))
    d = defaultdict(int)
    for i in l:
        d[i] += 1
    dk = sorted(list(d.keys()))
    dk2 = [i*2 for i in dk]
    ans = 0
    while dk:
        x = dk[-1]
        _ = dk2.pop(-1)
        bs = bisect.bisect(dk2,x)
        tmp = dk[bs:]
        if not tmp:
            break
        for i in tmp:
            for j in dk:
                if i+j > x and i >= j:
                    if i == j and i == x:
                        ans += d[x]*(d[x]-1)*(d[x]-2)//6
                    elif i == j:
                        ans += d[x]*d[i]*(d[i]-1)//2
                    elif i == x:
                        ans += d[x]*(d[x]-1)*d[j]//2
                    else:
                        ans += d[x]*d[i]*d[j]
        del dk[-1]
    print(ans)
if __name__ == '__main__':
    main()
Buggy code:
#16:18
n = int(input())
l = list(map(int,input().split()))
l.sort()
ans = 0
import bisect
for i in range(n):
  for j in range(i+1,n):
    ans += max(0,bisect.bisect_right(l,l[i]+l[j])-j-1)
    #print(i,j,n-bisect.bisect_right(l,l[i]+l[j]))
print(ans)

Fixed code:
#16:18
n = int(input())
l = list(map(int,input().split()))
l.sort()
ans = 0
import bisect
for i in range(n):
  for j in range(i+1,n):
    ans += max(0,bisect.bisect_left(l,l[i]+l[j])-j-1)
print(ans)
Buggy code:
N=int(input())
L=list(map(int,input().split()))
i=0
L=sorted(L)
for a in range(N):
  for b in range(a+1,N):
    c=bisect.bisect_left(L,L[a]+L[b])
    if c>b:
      i+=c-b
    else:
      i=i
print(i)

Fixed code:
import bisect
N=int(input())
L=list(map(int,input().split()))
i=0
L=sorted(L)
for a in range(N):
  for b in range(a+1,N):
    c=bisect.bisect_left(L,L[a]+L[b])
    if c>b:
      i+=c-b-1
    else:
      i=i
print(i)
Buggy code:
# D

import itertools
n = int(input())
l = list(map(int, input().split()))
l.sort()
 
from bisect import bisect_left
ans = 0

for i, j in itertools.combination(len(n),2):
    t = l[i]+l[j]
    idx = bisect_left(l, t)
    ans += max(0, idx-j-1)
print(ans)


Fixed code:
# D

import itertools
n = int(input())
l = list(map(int, input().split()))
l.sort()
 
from bisect import bisect_left
ans = 0

for i, j in itertools.combinations(range(len(l)),2):
    t = l[i]+l[j]
    idx = bisect_left(l, t)
    ans += max(0, idx-j-1)
print(ans)
Buggy code:
from bisect import bisect_left

N=int(input())
L=list(map(int,input().split()))
L=sorted(L)
ans=0
for i in range(N-2):
  a=L[i]
  for j in range(i+1,N-1):
    """
    b=L[j]
    lo=0
    hi=N
    x=a+b
    while lo<hi:
      mid=(lo+hi)//2
      if L[mid]<x: lo=mid+1
      else: hi=mid
    ans+=lo-(j+1) 
    """
    ans+=bisect_left(L,a+b)-(j+1)
print(ans)


Fixed code:
from bisect import bisect_left

N=int(input())
L=list(map(int,input().split()))
L=sorted(L)
ans=0
for i in range(N-2):
  a=L[i]
  for j in range(i+1,N-1):
    b=L[j]
    """
    lo=0
    hi=N
    x=a+b
    while lo<hi:
      mid=(lo+hi)//2
      if L[mid]<x: lo=mid+1
      else: hi=mid
    ans+=lo-(j+1) 
    """
    ans+=bisect_left(L,a+b)-(j+1)
print(ans)
Buggy code:
import bisect


N = int(input())
str_l = input()
num = 0
L = []
for v in str_l.split(' '):
    L.append(int(v))

L.sort()
print(L)
for i in range(N - 1):
    a = L[i]
    for j in range(i, N - 2):
        b = L[j + 1]
        index = bisect.bisect_left(L[j + 2:], a + b)
        for k in range(0, index):
            
            c = (L[j + 2:])[k]
            if b + c > a and a + c > b:
                num += 1

print(num)

Fixed code:
import bisect


N = int(input())
str_l = input()
num = 0
L = []
for v in str_l.split(' '):
    L.append(int(v))

L.sort()
for i in range(N - 1):
    a = L[i]
    for j in range(i, N - 2):
        b = L[j + 1]
        index = bisect.bisect_left(L[j + 2:], a + b)
        num += index
        # for k in range(0, index):
        #     c = (L[j + 2:])[k]
        #     if b + c > a and a + c > b:
        #         num += 1

print(num)
Buggy code:
import bisect

N = int(input())
Ls = list(map(int, input().split(" ")))
Ls.sort()

ans = 0

for i in range(2,N):
  for j in range(1,i):
    ans += (len(Ls[:j]) - bisect.bisect_left(Ls[:j],Ls[i]-Ls[j]))
       
print(ans)

Fixed code:
import bisect
 
N = int(input())
Ls = list(map(int, input().split(" ")))
Ls.sort()
 
ans = 0
 
for i in range(0,N-2):
  for j in range(i+1,N-1):
    k = bisect.bisect_left(Ls,Ls[i]+Ls[j])
    ans += k - (j + 1)
       
print(ans)
Buggy code:
from bisect import bisect, bisect_left
 
N= int(input())
L= list(map(int,input().split()))
L.sort()
 
ans = 0
for i in range(N-2):
  a=L[i]
  for k in range(i+1,n-1):
    b=s[k]
    t=bisect_left(L,a+b)
    ans+=t-k-1
print(ans)

Fixed code:
from bisect import bisect, bisect_left
 
N=int(input())
L=list(map(int,input().split()))
L.sort()
 
ans = 0
for i in range(N-2):
  a=L[i]
  for k in range(i+1,N-1):
    b=L[k]
    t=bisect_left(L,a+b)
    ans+=t-k-1
print(ans)
Buggy code:
import bisect
n = int(input())
l = sorted(list(map(int, input().split())))
ans=0
for i in range(n-2):
    for j in range(i+1,n-1):
        print(bisect.bisect_left(l[j+1:], l[i] + l[j]) )
        ans+=bisect.bisect_left(l[j+1:], l[i] + l[j])
print(ans)  

Fixed code:
import bisect
n = int(input())
l = sorted(list(map(int, input().split())))
ans=0
for i in range(n-2):
    for j in range(i+1,n-1):
        ans+=bisect.bisect_left(l, l[i] + l[j]) - j - 1
print(ans)
Buggy code:
import bisect

n = int(input())
l = list(map(int,input().split()))
l.sort()
count = 0
for i in range(n-2):
    for j in range(i+1, n-1):
        short_2hen = l[i] + l[j]
        l_long = l[j+1:n]
        bis = bisect.bisect_right(l_long, short_2hen-1)
        count += bis
        print(l_long)
        print(bis)
print(count)


Fixed code:
import bisect

n = int(input())
l = list(map(int,input().split()))
l.sort()
count = 0
for i in range(n-2):
    for j in range(i+1, n-1):
        short_2hen = l[i] + l[j]
        bis = bisect.bisect_left(l, short_2hen)
        count += bis - j - 1
print(count)
Buggy code:
inf = 1001001001
n = int(input())
lis = list(map(int, input().split()))
lis.sort()
lis.append(inf)
ans = 0
for i in range(n-2):
    a = lis[i]
    for j in range(i + 1, n-1):
        b = lis[j]
        l = j
        r = n
        #while r > l+1:
        #    mid = (l+r) // 2
        #    if lis[mid] < a + b:
        #        l = mid
        #    else:
        #        r = mid
        #ans += l - j
print(ans)

Fixed code:
inf = 1001001001
n = int(input())
lis = list(map(int, input().split()))
lis.sort()
lis.append(inf)
ans = 0
for i in range(n-2):
    a = lis[i]
    for j in range(i + 1, n-1):
        b = lis[j]
        l = j
        r = n
        while r > l+1:
            mid = (l+r) // 2
            if lis[mid] < a + b:
                l = mid
            else:
                r = mid
        ans += l - j
print(ans)
Buggy code:
n = int(input())
nums = map(int, input().split())

nums.sort()
counts = 0

for i in range(n):
  start, end = 0, i - 1
  while start < end:
    if nums[start] + nums[end] > nums[i]:
      counts += end - start
      end -= 1
    else:
      start += 1
print(counts)


Fixed code:
n = int(input())
nums = list(map(int, input().split()))
nums.sort()

counts = 0

for i in range(n):
  start, end = 0, i - 1
  while start < end:
    if nums[start] + nums[end] > nums[i]:
      counts += end - start
      end -= 1
    else:
      start += 1
print(counts)
Buggy code:
from bisect import bisect_left

n = int(input())
l = sorted(list(map(int,input().split())))
ans = 0

for i in range(n):
    for j in range(i+1,n):
        ans += bisect_left(l.,l[i]+l[j])-(j+1)
print(ans)

Fixed code:
from bisect import bisect_left
n = int(input())
l = sorted(list(map(int,input().split())))

ans = 0
for i in range(n):
    for j in range(i+1,n):
        ans += bisect_left(l,l[i]+l[j])-(j+1)
print(ans)
Buggy code:
from bisect import bisect_left
n=int(input())
l=list(map(int,input().split()))
l.sort()

ans=0
for a in range(n-2):
    for b in range(a+1,n-1):
        r=bisect_left(l, l[a] + l[b], b + 1, n)
        l=b+1
        ans+=r-l
        
print(ans)


Fixed code:
from bisect import bisect_left
n=int(input())
l=list(map(int,input().split()))
l.sort()

ans=0
for a in range(n-2):
    for b in range(a+1,n-1):
        ans+=max(0,bisect_left(l, l[a] + l[b])-b-1)
        
        
print(ans)
Buggy code:
import bisect

n = int(input())
l = sorted([int(li) for li in input().split()])

sum = 0
for i in range(n-1,-1,-1):
    for j in range(i-1,-1,-1):
        k = bisect.bisect_right(l[j],l[i]-l[j])
        sum += j-k if j>k else 0

print(sum)


Fixed code:
import bisect

n = int(input())
l = sorted([int(li) for li in input().split()])

sum = 0
for i in range(n-1,-1,-1):
    for j in range(i-1,-1,-1):
        k = bisect.bisect_right(l,l[i]-l[j])
        sum += j-k if j>k else 0

print(sum)
Buggy code:
import bisect
n = int(input())
l = [int(x) for x in input().split()]

l.sort()

ans = 0
for a in range(n - 1):
    for b in range(a + 1, n):
        c = bisect.bisect(l, l[a] + l[b])
        if b < c:
            ans += c - (b + 1)

print(ans)

Fixed code:
import bisect
n = int(input())
l = [int(x) for x in input().split()]

l.sort()

ans = 0
for a in range(n):
    for b in range(a + 1, n):
        c = bisect.bisect_left(l, l[a] + l[b])
        if b < c:
            ans += c - (b + 1)

print(ans)
Buggy code:
from bisect import*
n, *l = map(int, open(0).read().split())
print(sum(max(0, bisect_left(l, l[i] + l[j]) - j - 1) for i in range(n) for j in range(i+1, n)))

Fixed code:
from bisect import*
n, *l = map(int, open(0).read().split())
l.sort()
print(sum(max(0, bisect_left(l, l[i] + l[j]) - j - 1) for i in range(n) for j in range(i+1, n)))
Buggy code:
# -*- coding: utf-8 -*-
import numpy as np
from collections import deque
import collections
import itertools
import bisect
mod = 10 ** 9 + 7
N = int(input())
L = list(map(int, input().split()))
L = sorted(L)
print(L)
count = 0
for i in range(len(L)):
    for j in range(i+1, len(L)):
        a = L[i]
        b = L[j]
        idx = insert_index = bisect.bisect_left(L, a+b)
        count += max(0, idx-j-1)


print(count)


Fixed code:
# -*- coding: utf-8 -*-
import numpy as np
from collections import deque
import collections
import itertools
import bisect
mod = 10 ** 9 + 7
N = int(input())
L = list(map(int, input().split()))
L = sorted(L)
count = 0
for i in range(len(L)):
    for j in range(i+1, len(L)):
        a = L[i]
        b = L[j]
        idx = insert_index = bisect.bisect_left(L, a+b)
        count += max(0, idx-j-1)


print(count)
Buggy code:
import bisect

N = int(input())
L = list(map(int, input().split()))
L = sorted(L)

count = 0
for n in range(N-2):
    for m in range(n+1,N-1):
        print(n,m)
        print(bisect.bisect_left(L,L[m]+L[n]))
        count += bisect.bisect_left(L,L[m]+L[n]) -m-1

print(count)



Fixed code:
import bisect

N = int(input())
L = list(map(int, input().split()))
L = sorted(L)

count = 0
for n in range(N-2):
    for m in range(n+1,N-1):
        count += bisect.bisect_left(L,L[m]+L[n]) - (m + 1)

print(count)
Buggy code:
import bisect

n = int(input())
L = list(map(int,input().split()))
L = sorted(L)
ans = 0

for i in range(n-1):
    for j in range(i+1,n):
        limit = L[i]+L[j]
        ans += bisect.bisect_right(L,limit) - (j+1)

print(ans)


Fixed code:
import bisect

n = int(input())
L = list(map(int,input().split()))
L = sorted(L)
ans = 0

for i in range(n-1):
    for j in range(i+1,n):
        limit = L[i]+L[j]
        ans += bisect.bisect_left(L,limit) - (j+1)

print(ans)
Buggy code:
import sys
import bisect
N = int(input())
L = [i for i in map(int, input().split())]
ans = 0

L.sort()
#print(L)
if N == 3:
    if L[0] + L[1] > L[2]:
        print("1")
        sys.exit()

for i in range(len(L)-2):
    for j in range(i+1,len(L)-1):
        index = bisect.bisect_right(L,L[i]+L[j],j+1,len(L))
        ans += (index - 1) - (j + 1) + 1
print(ans)

Fixed code:
import sys
import bisect
N = int(input())
L = [i for i in map(int, input().split())]
ans = 0

L.sort()
#print(L)
if N == 3:
    if L[0] + L[1] > L[2]:
        print("1")
        sys.exit()

for i in range(len(L)-2):
    for j in range(i+1,len(L)-1):
        index = bisect.bisect_left(L,L[i]+L[j],j+1)
        #print(index)
        ans += (index - 1) - (j + 1) + 1
print(ans)
Buggy code:
N = int(input())
L = list(map(int, input().split()))
L.sort()
def find(arr, x):
  if arr[0] > x:
    return 0
  if arr[-1] < x:
    return len(arr)
  
  l = 0
  h = len(arr)
  
  while h - l > 1:
    m = (l+h) // 2
    if arr[m] < x:
      l = m
    else:
      h = m
  return h

ans = 0

for i in range(N-2):
  for j in range(i+1, N-1):
    x = L[i] + L[j]
    ans += find(L[j+1:], x)
print(ans)

Fixed code:
N = int(input())
L = list(map(int, input().split()))
L.sort()
def find(arr, x):
  if arr[0] >= x:
    return 0
  if arr[-1] < x:
    return len(arr)
  
  l = 0
  h = len(arr)
  
  while h - l > 1:
    m = (l+h) // 2
    if arr[m] < x:
      l = m
    else:
      h = m
  return h

ans = 0

for i in range(N-2):
  for j in range(i+1, N-1):
    x = L[i] + L[j]
    temp = find(L[j+1:], x)
    ans += temp
print(ans)
Buggy code:
N = int(input())
L = list(map(int, input().split()))
C = sorted(L)

def X(n,j):
    L1 = j+1
    L2 = N-1
    while L1 != L2:
        a = (L1+L2)//2
        if C[a] == n:
            return len(C[j+1:a])
        elif L2 - L1 <= 1:
            if C[L1] > n:
                return len(C[j+1:L1])
            elif C[L1] < n < C[L2]:
                return len(C[j+1:L2])
            else:
                return len(C[j+1:L2+1])
        elif C[a] < n:
            L1 = a
        else:
            L2 = a
    if L1 < n:
        return 1
    else:
        return 0
ans = 0
for i in range(N-2):
    for j in range(i+1,N-1):
        ans += X((C[i]+C[j]),j)
print(ans)

Fixed code:
N = int(input())
L = list(map(int, input().split()))
C = sorted(L)
def X(n,j):
    L1 = j+1
    L2 = N-1
    while L1 != L2:
        a = (L1+L2)//2
        if C[a] == n:
            while C[a] == C[a-1]:
                a = a-1
            return len(C[j+1:a])
        elif L2 - L1 <= 1:
            if C[L2] < n:
                return len(C[j+1:L2+1])
            elif C[L1] < n <= C[L2]:
                return len(C[j+1:L2])
            while C[L1] == C[L1-1]:
                if j+1 <= L1-1:
                    L1 = L1-1
                else:
                    break
            if C[L1] > n:
                return len(C[j+1:L1])
            elif C[L1] == n:
                return len(C[j+1:L1])
        elif C[a] < n:
            L1 = a
        else:
            L2 = a
    if C[L1] < n:
        return 1
    else:
        return 0
ans = 0
# print(C)
for i in range(N-2):
    for j in range(i+1,N-1):
        # print(X((C[i]+C[j]),j))
        ans += X((C[i]+C[j]),j)
print(ans)
Buggy code:
n = int(input())
l = list(map(int,input().split()))
cnt = 0
l.sort()

for i in range(n):
    for j in range(i+1,n):
        k = bisect.bisect_left(l,l[i]+l[j])
        if k > j:
            cnt += k - j - 1
        else:
            continue
            
print(cnt)

Fixed code:
import bisect
n = int(input())
l = list(map(int,input().split()))
cnt = 0
l.sort()

for i in range(n):
    for j in range(i+1,n):
        k = bisect.bisect_left(l,l[i]+l[j])
        if k > j:
            cnt += k - j - 1
        else:
            pass
            
print(cnt)
Buggy code:
import bisect

n = int(input())
l = sorted(list(map(int,input().split())))
print(l)
count = 0
for i in range(2,n):
	for j in range(1,i):
		count += max(j - bisect.bisect_right(l, l[i] - l[j]) , 0)

		print(l[j],l[i],bisect.bisect_right(l,l[i]-l[j]),count)


Fixed code:
import bisect

n = int(input())
l = sorted(list(map(int,input().split())))
count = 0
for i in range(2,n):
	for j in range(1,i):
		count += max(j - bisect.bisect_right(l, l[i] - l[j]) , 0)

print(count)
Buggy code:
n = int(input())
L = list(map(int, input().split()))

L.sort()
import bisect
ans = 0
print(L)
for i in range(n-2):
    b = L[i]
    for j in range(i+1, n-1):
        c = L[j]
        p = bisect.bisect_left(L, b+c)
        q = bisect.bisect_right(L, c)
        ans += max(p-q, 0)
print(ans)

Fixed code:
n = int(input())
L = list(map(int, input().split()))

L.sort()
import bisect
ans = 0
for i in range(n-2):
    b = L[i]
    for j in range(i+1, n-1):
        c = L[j]
        p = bisect.bisect_left(L, b+c)
        #print(b, c, j, p, p-j-1)
        ans += max(p-j-1, 0)
print(ans)
Buggy code:
from bisect import bisect_left

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

for b in range(n):
  for a in range(b):
    ab = L[a] + L[b]
    r = bisect_left(L, ab)
    l = b + 1
    
    ans += r-l

print(ans)

Fixed code:
from bisect import bisect_left

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

for b in range(N):
  for a in range(b):
    ab = L[a] + L[b]
    r = bisect_left(L, ab)
    l = b + 1
    
    ans += r-l

print(ans)
Buggy code:
# coding: utf-8
import bisect

N = int(input())
L = sorted(list(map(int, input().split())))
ans = 0
for i in range(N):
    for j in range(i + 1, N - 1):
        ans += bisect.bisect_right(L, L[i] + L[j]) - j - 1
print(ans)

Fixed code:
# coding: utf-8
import bisect

N = int(input())
L = sorted(list(map(int, input().split())))
ans = 0
for i in range(N):
    for j in range(i + 1, N - 1):
        ans += max(bisect.bisect_right(L, L[i] + L[j] - 1) - j - 1, 0)
print(ans)
Buggy code:
import bisect

N = int(input())
L = list(map(int,input().split()))
L.sort()

ans = 0

for i in range(N-2):
    for j in range(i+1,N-1):
        ans += bisect.bisect_left(L,L[i]+L[j]-1) - L.index(L[j+1])
print(ans)
        

Fixed code:
import bisect

N = int(input())
L = list(map(int,input().split()))
L.sort()

ans = 0

for i in range(N):
    for j in range(i+1,N):
        ans += bisect.bisect_left(L,L[i]+L[j]) - (j+1)
print(ans)
Buggy code:
import bisect

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

L.sort()
print(L)

ans=0
for i in range(N-1,-1,-1):
  for j in range(i-1,-1,-1):
    if L[i]>=2*L[j]:
      break
    #print(L[i],L[j],bisect.bisect(L,L[i]-L[j]),j-bisect.bisect(L,L[i]-L[j]))
    ans+=j-bisect.bisect(L,L[i]-L[j])
print(ans)

Fixed code:
import bisect

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

L.sort()
#print(L)

ans=0
for i in range(N-1,-1,-1):
  for j in range(i-1,-1,-1):
    if L[i]>=2*L[j]:
      break
    #print(L[i],L[j],bisect.bisect(L,L[i]-L[j]),j-bisect.bisect(L,L[i]-L[j]))
    ans+=j-bisect.bisect(L,L[i]-L[j])
print(ans)
Buggy code:
import bisect
N = int(input())
L = [int(i) for i in input().split()]

SL = sorted(L)
ans = 0
for i in range(N - 2):
    for j in range(i + 1, N - 1):
        right = bisect.bisect_left(SL, SL[i] + SL[j])
        left = j + 1
        ans += max(right - left)

print(ans)


Fixed code:
import bisect
N = int(input())
L = [int(i) for i in input().split()]

SL = sorted(L)
ans = 0
for i in range(N - 2):
    for j in range(i + 1, N - 1):
        right = bisect.bisect_left(SL, SL[i] + SL[j])
        left = j + 1
        ans += right - left

print(ans)
Buggy code:
n = int(input())
l = list(map(int, input().split()))
l.sort()
cnt = 0
for i in range(n - 2):
    for j in range(i + 1, n - 1):
        if l[i] + l[j] > l[n - 1]:
            cnt += n - j - 1
            continue
        le = j + 1
        ri = n - 1
        while ri - le > 0:
            m = (le + ri) // 2
            if l[m] < l[i] + l[j]:
                le += m + 1
            else:
                ri = m
            cnt += le - j - 1
print(cnt)

Fixed code:
n = int(input())
l = list(map(int, input().split()))
l.sort()
cnt = 0
for i in range(n - 2):
    for j in range(i + 1, n - 1):
        if l[i] + l[j] > l[n - 1]:
            cnt += n - j - 1
            continue
        le = j + 1
        ri = n - 1
        while ri - le > 0:
            m = (le + ri) // 2
            if l[m] < l[i] + l[j]:
                le = m + 1
            else:
                ri = m
        cnt += le - j - 1
print(cnt)
Buggy code:
import sys
from bisect import bisect_left
N=int(input())
L=list(map(int,input().split()))
L.sort()
total = 0
for i in range(N):
  for j in range(i + 1, N):
    a = L[i] - L[j]
    total += max(bisect_left(L, a) - j - 1, 0)

print(total)

Fixed code:
import sys
from bisect import bisect_left
N=int(input())
L=list(map(int,input().split()))
L.sort()
total = 0
for i in range(N):
  for j in range(i + 1, N):
    a = L[i] + L[j]
    total += max(bisect_left(L, a) - j - 1, 0)

print(total)
Buggy code:
import bisect

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

l.sort()
a = sorted(list(set(l)))
ans = 0

for b in range(n - 2):
    for c in range(b + 1,n - 1):
        left = bisect.bisect_left(a,l[c] + l[b])
        ans+=left - c - 1

print(ans)

Fixed code:
import bisect

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

l.sort()
ans = 0

for b in range(n - 2):
    for c in range(b + 1,n - 1):
        left = bisect.bisect_left(l,l[c] + l[b])
        ans+=left - c - 1

print(ans)
Buggy code:
import bisect

N = int(input())
L = [int(l) for l in input().split()]
L.sort()
S = 0
n = 0
print(L)
S = 0
for i in range(N - 2):
    # print("i=", i)
    for j in range(i + 1, N - 1):
        # print("j=",j)
        l = L[i] + L[j]
        s = bisect.bisect_left(L, l)
        # print(s)
        S += bisect.bisect_left(L, L[i] + L[j]) - j - 1

print(S)

Fixed code:
import bisect

N = int(input())
L = [int(l) for l in input().split()]
L.sort()
S = 0
n = 0
S = 0
for i in range(N - 2):
    for j in range(i + 1, N - 1):
        l = L[i] + L[j]
        s = bisect.bisect_left(L, l)
        S += bisect.bisect_left(L, L[i] + L[j]) - j - 1

print(S)