qyliang/CodeNanoFix huggingface dataset:
Buggy code:
N, K = map(int, input().split())
H = [int(input()) for _ in range(N)]
H.sort()
minim = 1e10
for i in range(N-K):
    print(H[i], H[i+K-1])
    minim = min(minim, H[i+K-1] - H[i])
print(minim)

Fixed code:
N, K = map(int, input().split())
H = [int(input()) for _ in range(N)]
H.sort()
minim = 1e10
for i in range(N-K+1):
    minim = min(minim, H[i+K-1] - H[i])
print(minim)
Buggy code:
N, K =map(int, input().split())
lst = []
for _ in range(N):
    lst.append(int(input()))
sor = sorted(lst)

mindiff = float('inf')
for i in range(N-K+1):
    diff = selected[i] - selected[i+K-1]
    if diff <= mindiff:
        mindiff = diff
print(mindiff)

Fixed code:
N, K =map(int, input().split())
lst = []
for _ in range(N):
    lst.append(int(input()))
sor = sorted(lst)

mindiff = float('inf')
for i in range(N-K+1):
    diff = sor[i+K-1] - sor[i] 
    if diff <= mindiff:
        mindiff = diff
print(mindiff)
Buggy code:
n, k = (int(x) for x in input().split())
h = []
for _ in range(n):
    h.append(int(input()))
h.sort()
ans = []
for i in range(n-k-1):
    ans.append(h[i+k-1]-h[i])
print(min(ans))

Fixed code:
n, k = (int(x) for x in input().split())
h = []
for _ in range(n):
    h.append(int(input()))
h.sort()
ans = []
for i in range(n-k+1):
    ans.append(h[i+k-1]-h[i])
print(min(ans))
Buggy code:
import sys
stdin = sys.stdin
read_int = lambda : list(map(int,stdin.readline().split()))
read_str = lambda : stdin.readline().rstrip()

N,K = read_int()
h = [read_int()[0] for _ in range(N)]
h.sort()

def solve():
    ans = float('INF')
    for i in range(N - K):
        ans = min(ans, h[i + K - 1] - h[i])
    return ans

if __name__ == "__main__":
    print(solve())


Fixed code:
import sys
stdin = sys.stdin
read_int = lambda : list(map(int,stdin.readline().split()))
read_str = lambda : stdin.readline().rstrip()

N,K = read_int()
h = [read_int()[0] for _ in range(N)]
h.sort()

def solve():
    ans = float('INF')
    for i in range(N - K + 1):
        ans = min(ans, h[i + K - 1] - h[i])
    return ans

if __name__ == "__main__":
    print(solve())
Buggy code:
N, K = map(int, input().split())
tree = []
for i in range(N):
  tree.append(int(input()))
tree.sort()
mini = 1000000000
for i in range(N-K+1):
  mini = min(mini, tree[i+K-1]-[i])
print(mini)
  

Fixed code:
N, K = map(int, input().split())
tree = []
for i in range(N):
  tree.append(int(input()))
tree.sort()
mini = 1000000000
for i in range(N-K+1):
  mini = min(mini, tree[i+K-1]-tree[i])
print(mini)
Buggy code:
n,k=map(int,input().split())
l=[]
sa=[]
ans=[]
for i in range(n):
    h=int(input())
    l.append(h)
l.sort()
for i in range(1,n):
    sa.append(abs(l[i]-l[i-1]))
for i in range(k-1):
    ans.append(sa[i+k-2]+sa[i])
print(min(ans))

Fixed code:
n,k=map(int,input().split())
l=[]
sa=[]
for i in range(n):
    h=int(input())
    l.append(h)
l.sort()
for i in range(n-k+1):
    sa.append(l[i+k-1]-l[i])
print(min(sa))
Buggy code:
N, K = map(int, input().split())
h = [int(input()) for i in range(N)]
h.sort()
res=[]
for i in range(N-K-1):
  res.append(h[K+i-1]-h[i])
print(min(res))

Fixed code:
N, K = map(int, input().split())
h = [int(input()) for i in range(N)]
h.sort()
res=[]
for i in range(N-K+1):
  res.append(h[K+i-1]-h[i])
print(min(res))
Buggy code:
N, K = map(int, input().split())
H = [int(input()) for _ in range(N)]

H.sort()
res = 10000000000
for i in range(N-K+1):
    print(H[i:i+K])
    res = min(res, max(H[i:i+K]) - min(H[i:i+K]))
print(res)

Fixed code:
N, K = map(int, input().split())
H = [int(input()) for _ in range(N)]

H.sort()
res = 10000000000
for i in range(0, N-K+1):
    res = min(res, H[i+K-1] - H[i])
print(res)
Buggy code:
n,k = map(int,input().split())
h = [int(input()) for _ in range(n)]
h.sort()

ans = 10**9
for i in range(n-k):
    ans = min(ans,h[i+k-1]-h[i])
print(ans)

Fixed code:
n,k = map(int,input().split())
h = [int(input()) for _ in range(n)]
h.sort()

ans = 10**9
for i in range(n-k+1):
    #print(i,i+k-1)
    ans = min(ans,h[i+k-1]-h[i])
print(ans)
Buggy code:
#115-C
n,k = (int(i) for i in input().split())
t = [int(input()) for i in range(n)] 
#t = [10,15,11,14,12]
t = sorted(t)
min = t[k-1]-t[0]
for i in range(n-k):
    s = t[i+k-1]-t[i]
    if s < min:
        min = s

print(min)
#print(t)
    

Fixed code:
#115-C
n,k = (int(i) for i in input().split())
t = [int(input()) for i in range(n)] 
#t = [10,15,11,14,12]
t = sorted(t)
min = t[k-1]-t[0]
for i in range(n-k+1):
    s = t[i+k-1]-t[i]
    if s < min:
        min = s

print(min)
#print(t)
Buggy code:
=input().split()
n=[int(i) for i in n]
h=[n[1]]
for i in range(n[0]):
    h[i]=int(input())
h.sort()

q=-1
for i in range(len(h)):
    a=h[i+n[1]-1]-h[i]
    if q==-1 or q>a:
        q=a
    if q==0:
        break
print(q)

Fixed code:
n=input().split()
n=[int(i) for i in n]
h=[0]*n[0]
for i in range(n[0]):
    h[i]=int(input())
h.sort()
q=-1
for i in range(0,len(h)-n[1]+1):
    a=h[i+n[1]-1]-h[i]
    if q==-1 or q>a:
        q=a
print(q)
Buggy code:
N,K = map(int,input().split(" "))


H = sorted([int(input()) for i in range(N)],reverse=True)


#print(H)

L = []

for k in range(N-K+1):
    #print(k,k+1,k+2)
    
    L.append(H[k]-H[k+K])

print(min(L))


Fixed code:
N,K = map(int,input().split(" "))


H = sorted([int(input()) for i in range(N)],reverse=True)


#print(H)

L = []

for k in range(N-K+1):
    #print(k,k+1,k+2,k+K)
    
    L.append(H[k]-H[k+K-1])

print(min(L))
Buggy code:
N,K = map(int,input().split())
tree_list = [int(input()) for _ in range(N)]
tree_list = sorted(tree_list)
ans = 10**9
for i in range(N-K-1):
    compare = tree_list[i+K]-tree_list[i]
    if ans > compare:
        ans = compare
print(ans)
    

Fixed code:
N,K = map(int,input().split())
tree_list = [int(input()) for _ in range(N)]
tree_list = sorted(tree_list)
ans = 10**9
for i in range(N-K+1):
    compare = tree_list[i+K-1]-tree_list[i]
    if ans > compare:
        ans = compare
print(ans)
Buggy code:
#115_C
n, k = map(int, input().split())
h = [int(input()) for i in range(n)]
h.sort()

ans = 1000000000
for i in range(n-k):
    diff = h[i+k-1] - h[i]
    ans = min(ans, diff)
    
print(ans)

Fixed code:
#115_C
n, k = map(int, input().split())
h = [int(input()) for i in range(n)]
h.sort()

ans = 1000000000
for i in range(n-k+1):
    diff = h[i+k-1] - h[i]
    ans = min(ans, diff)
    
print(ans)
Buggy code:
n, k=map(int,input().split())
a=[input() for i in range(n)]
a.sort()
b=[]
for p in range(n-k):
  c=a[p+k]-a[p]
  b.append(c)
  
print(min(b))
  

Fixed code:
n, k=map(int,input().split())
a=[int(input()) for i in range(n)]
a.sort()
b=[]
for p in range(n-k+1):
  c=a[p+k-1]-a[p]
  b.append(c)
  
print(min(b))
Buggy code:
N, K = map(int,input().split())
h = sorted(int(input()) for _ in range(N))

ans = 1e9+7
for i in range(N-K+1):
  ans = min(ans, h[i+K]-h[i])
print(ans)

Fixed code:
N, K = map(int,input().split())
h = sorted(int(input()) for _ in range(N))

ans = 1e9+7
for i in range(N-K+1):
  ans = min(ans, h[i+K-1]-h[i])
print(ans)
Buggy code:
# -*- coding: utf-8 -*-
N,K = map(int,input().split())
c = [int(input()) for i in range(N)]
c.sort()
output = c[K-1] - c[0]
for i in range(N-K):
    output = c[K+i] - c[i+1] if output > c[K+i] - c[i+1]
print(output)


Fixed code:
# -*- coding: utf-8 -*-
N,K = map(int,input().split())
c = [int(input()) for i in range(N)]
c.sort()
output = c[K-1] - c[0]
for i in range(N-K):
    if output > c[K+i] - c[i+1]:output = c[K+i] - c[i+1]
print(output)
Buggy code:
n, k = map(int, input().split())
h = [int(input()) for i in range(n)]
h.sort()
ans = 10 ** 10
for i in range(n-k+1):
  ans = min(ans, (max(a[i:i+k])-min(a[i:i+k])))
print(ans)

Fixed code:
n, k = map(int, input().split())
h = [int(input()) for i in range(n)]
h.sort()
ans = 10 ** 10
for i in range(n-k+1):
  ans = min(ans, h[i+k-1] - h[i])
print(ans)
Buggy code:
## C - Christmas Eve
N, K = map(int, input().split())
H = sorted([int(input()) for i in range(N)])
rt = 10**9
for i in range(0,N-K):
    rt = H[K - 1 + i] - H[i] if rt > H[K - 1 + i] - H[i] else rt
print(rt)

Fixed code:
## C - Christmas Eve
N, K = map(int, input().split())
H = sorted([int(input()) for i in range(N)])
rt = 10**9
for i in range(0,N-K+1):
    rt = H[K - 1 + i] - H[i] if rt > H[K - 1 + i] - H[i] else rt
print(rt)
Buggy code:
n,k=map(int,input().split())
h=[input() for _ in range(n)]
h=sorted(h)
l=[]
for i in range(n-k+1):
  l.append(h[i+k-1]-h[i])
print(min(l))

Fixed code:
n,k=map(int,input().split())
h=[int(input()) for _ in range(n)]
h=sorted(h)
l=[]
for i in range(n-k+1):
  l.append(h[i+k-1]-h[i])
print(min(l))
Buggy code:
import heapq

def main():
    n,k = map(int,input().split())
    tree_heights = []

    for i in range(n):
        tree_heights.append(int(input()))
    tree_heights.sort()
    print(tree_heights)

    mn = 1000000000000000000000000000000
    for i in range(n-k+1):
        print(i,abs(tree_heights[i]-tree_heights[i+k-1]))
        mn = min(abs(tree_heights[i]-tree_heights[i+k-1]),mn)
    print(mn)

    #print(tree_heights)
    #print(tree_diffs)
    #print(sum(tree_diffs[0:k]))





if __name__ == '__main__':
    main()


Fixed code:
import heapq

def main():
    n,k = map(int,input().split())
    tree_heights = []

    for i in range(n):
        tree_heights.append(int(input()))
    tree_heights.sort()
    #print(tree_heights)

    mn = 1000000000000000000000000000000
    for i in range(n-k+1):
        #print(i,abs(tree_heights[i]-tree_heights[i+k-1]))
        mn = min(abs(tree_heights[i]-tree_heights[i+k-1]),mn)
    print(mn)

    #print(tree_heights)
    #print(tree_diffs)
    #print(sum(tree_diffs[0:k]))





if __name__ == '__main__':
    main()
Buggy code:
n, k = map(int,(input().split()))
h = []
for i in range(n):
    h.append(int(input()))
h.sort()
min=h[-1]-h[0]
for i in range(n-k+1):
    if h[i+k-1]-h[i] < min:
        min = h[i+k-1]-h[i]

Fixed code:
n, k = map(int,(input().split()))
h = []
for i in range(n):
    h.append(int(input()))
h.sort()
min=h[-1]-h[0]
for i in range(n-k+1):
    if h[i+k-1]-h[i] < min:
        min = h[i+k-1]-h[i]
print(min)
Buggy code:
n, k = map(int,input().split(" "))
trees = []
for i in range(n):
    trees.append(int(input()))

min_val = 0
for i in range(len(trees) - 1):
    min_val = min(abs(trees[i+1] - trees[i]), min_val)
    
print(min_val)

Fixed code:
n, k = map(int,input().split(" "))
trees = []
for i in range(n):
    trees.append(int(input()))

trees.sort()
min_val = trees[k-1] - trees[0]
for i in range(1, n - k + 1):
    min_val = min(abs(trees[i+k-1] - trees[i]), min_val)
print(min_val)
Buggy code:
n, k = map(int, input().split())
hh = []
minn = 10**9
for i in range(n):
    h = int(input())
    hh.append(h)
h = sorted(hh)
for i in range(n - k - 1):
    if h[i + k - 1] - h[i] < minn:
        minn = h[i + k - 1] - h[i]
print(minn)


Fixed code:
n, k = map(int, input().split())
hh = []
minn = 10**12
for i in range(n):
    h = int(input())
    hh.append(h)
h = sorted(hh)
for i in range(n - k + 1):
    if h[i + k - 1] - h[i] < minn:
        minn = h[i + k - 1] - h[i]
print(minn)
Buggy code:
n,k=map(int,input().split())

h=[int(input()) for _ in range(n)]
h.sort()

min_h_diff=float('inf')
for i in range(n-k):
    min_h_diff=min(min_h_diff,h[i+k-1]-h[i])
print(min_h_diff)    

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

h=[int(input()) for _ in range(n)]
h.sort()

min_h_diff=float('inf')
for i in range(n-k+1):
    min_h_diff=min(min_h_diff,h[i+k-1]-h[i])
print(min_h_diff)
Buggy code:
N, K = map(int, input().split())
h = [int(input()) for i in range(N)]
h.sort()
print(min(h[i+K-1] - h[i]) for i in range(N-K+1))

Fixed code:
N,K=map(int,input().split())
h=[int(input()) for _ in range(N)]
h.sort()
print(min([h[i+K-1]-h[i] for i in range(N-K+1)]))
Buggy code:
N, M = list(map(int, input().split()))
K = [int(input()) for i in range(N)]
K = sorted(K)
print(min([K[i+M-1]-K[i] for i in range(N-M+1)])

Fixed code:
N, M = list(map(int, input().split()))
K = [int(input()) for i in range(N)]
K = sorted(K)
print(min([K[i+M-1]-K[i] for i in range(N-M+1)]))
Buggy code:
n,k = map(int,input().split())
s = [int(input()) for i in range(n)]
s.sort()
c = s[n-k-1] - s[0]
for i in range(1,n-k+1):
    if s[i+n-k] - s[i] < c:
        c = s[i+n-k] - s[i]

print(c)

Fixed code:
n,k = map(int,input().split())
s = [int(input()) for i in range(n)]
s.sort()
c = s[k-1] - s[0]
for i in range(1,n-k+1):
    if s[i+k-1] - s[i] < c:
        c = s[i+k-1] - s[i]

print(c)
Buggy code:
ef solve(string):
    n, k = map(int, string.split("\n")[0].split(" "))
    ins = list(map(int, string.split("\n")[1:]))
    ins.sort()
    return str(min([ins[i + k - 1] - ins[i] for i in range(len(ins) - k + 1)]))


if __name__ == '__main__':
    n, k = map(int, input().split(" "))
    ins = [input() for _ in range(n)]
    print(solve("{} {}\n{}".format(n, k, "\n".join(ins))))



Fixed code:
def solve(string):
    n, k = map(int, string.split("\n")[0].split(" "))
    ins = list(map(int, string.split("\n")[1:]))
    ins.sort()
    return str(min([ins[i + k - 1] - ins[i] for i in range(len(ins) - k + 1)]))


if __name__ == '__main__':
    n, k = map(int, input().split(" "))
    ins = [input() for _ in range(n)]
    print(solve("{} {}\n{}".format(n, k, "\n".join(ins))))
Buggy code:
N,K = map(int,input().split())

H = []
for i in range(N):
    h = 100000#int(input())
    H.append(h)

H = sorted(H)

tmp = 0
ans = H[-1]
for i in range(N-K+1):
    tmp = H[i+K-1] - H[i]
#    print(H[i:i+K])

    ans = min(ans, tmp)

print(ans)


Fixed code:
N, K = map(int,input().split())
H=[]
for i in range (N):
    h=int(input())
    H.append(h)

H = sorted(H)
ans = H[K-1] - H[0]
for i in range (N-K+1):
    ans = min (ans, H[i+K-1]-H[i])

print(ans)
Buggy code:
N, K = map(int, input().split())
h = sorted([int(input()) for i in range(N)])
print(h)

inf = 10**9
min_height = inf
for i in range(N-K+1):
    print(i+(K-1), h[i+(K-1)], i, h[i])
    min_height = min(min_height, h[i+(K-1)] - h[i])

print(min_height)


Fixed code:
N, K = map(int, input().split())
h = sorted([int(input()) for i in range(N)])
# print(h)

inf = 10**9
min_height = inf
for i in range(N-K+1):
    # print(i+(K-1), h[i+(K-1)], i, h[i])
    min_height = min(min_height, h[i+(K-1)] - h[i])

print(min_height)
Buggy code:
n,k = map(int,input().split(" "))

trees = []
for i in range(n):
    trees.append(int(input()))

trees = sorted(trees,reverse=True)
print(trees)
average = sum(trees)/len(trees)
min_dist = 1e+10
if abs(max(trees) - average) > abs(min(trees) - average):
    for i in range(n)[k-1:]:
        print(i)
        if min_dist > abs(trees[i] - trees[i-k+1]):

            min_dist = abs(trees[i] - trees[i-k+1])
else:
    for i in range(n-k+1):
        if min_dist > abs(trees[i+k-1] - trees[i]):
            min_dist = abs(trees[i+k-1] - trees[i])
print(min_dist)


Fixed code:
n,k = map(int,input().split(" "))

trees = []
for i in range(n):
    trees.append(int(input()))

trees = sorted(trees,reverse=True)
average = sum(trees)/len(trees)
min_dist = 1e+10
if abs(max(trees) - average) > abs(min(trees) - average):
    for i in range(n)[k-1:]:
        if min_dist > abs(trees[i] - trees[i-k+1]):

            min_dist = abs(trees[i] - trees[i-k+1])
else:
    for i in range(n-k+1):
        if min_dist > abs(trees[i+k-1] - trees[i]):
            min_dist = abs(trees[i+k-1] - trees[i])
print(min_dist)
Buggy code:
N,K=map(int,input().split())
H=[int(input()) for _ in range(N)]
H.sort(reverse=True)
ans=10**6

for i in range(N-K):
    ans=min(ans,H[i]-H[i+K-1])

print(ans)    

Fixed code:
N,K=map(int,input().split())
H=[int(input()) for _ in range(N)]
H.sort(reverse=True)
ans=10**9

for i in range(N-K+1):
    ans=min(ans,H[i]-H[i+K-1])

print(ans)
Buggy code:
import sys

N,K=(int(x) for x in input().split())

h=[]
for i in range(N):
	h.append(int(input()))	
h.sort()

minimum=10**10
tmp=N-K
for i in range(N-K+1):
	#print("i/minimun",i,minimum)
	if h[i+K-1]-h[i] < minimum:
		minimum=h[i+K-1]-h[i]

#print(minimum)
#print(h)

Fixed code:
import sys

N,K=(int(x) for x in input().split())

h=[]
for i in range(N):
	h.append(int(input()))	
h.sort()

minimum=10**10
tmp=N-K
for i in range(N-K+1):
	#print("i/minimun,h[i+K-1],h[i]",i,minimum,h[i+K-1],h[i])
	if h[i+K-1]-h[i] < minimum:
		minimum=h[i+K-1]-h[i]

print(minimum)
#print(h)
Buggy code:
N,K = map(int, input().split())
h = [int(input()) for i in range(N)]
h.sort()
ans = min(h[i+K-1] - h[i] for i in range(N-k+1))
print(ans)


Fixed code:
N,K = map(int, input().split())
h = [int(input()) for i in range(N)]
h.sort()
ans = min(h[i+K-1] - h[i] for i in range(N-K+1))
print(ans)
Buggy code:
n,k=map(int,input().split())
h=[int(input()) for i in range(n)]
h.sort()
ans=[h[k+i]-h[i] for i in range(n-k+1)]
print(min(ans))

Fixed code:
n,k=map(int,input().split())
h=[int(input()) for i in range(n)]
h.sort()

print(min(h[i+k-1] - h[i] for i in range(n-k+1)))
Buggy code:
N,K = map(int,input().split())
h =[]
dif = []
for n in range(0,N):
    h.append(int(input()))

h.sort()

m = 10**10

for i in range(0,N-K):
    m = min(m,abs(h[i] - h[i+K-1]))

print(m)


Fixed code:
N,K = map(int,input().split())
h =[]
dif = []
for n in range(0,N):
    h.append(int(input()))

h.sort()

m = 10**10

for i in range(0,N-K+1):
    m = min(m,abs(h[i] - h[i+K-1]))

print(m)
Buggy code:
n,k = map(int,input().split())
A = [int(input()) for i in range(n)]
A.sort()
print(A[-1]-A[-k])

Fixed code:
n,k = map(int,input().split())
A = [int(input()) for i in range(n)]
A.sort()
print(min(A[i+k-1] - A[i] for i in range(n-k+1)))
Buggy code:
n, k = map(int, input().split())
h = [int(input()) for _ in range(N)]

h.sort()
m = sum(h)

for i in range(n - k):
    m = min(h[i + k] - h[i], m)
print(m)


Fixed code:
n, k = map(int, input().split())
h = [int(input()) for _ in range(n)]

h.sort()
m = sum(h)

for i in range(n - k + 1):
    m = min(h[i + k - 1] - h[i], m)
print(m)
Buggy code:
N, K = map(int, input().split())
H = sorted([int(input()) for _ in range(N)])
print(min(H[i+K-1]-H[i]) for i in range(N-K+1))


Fixed code:
N, K = map(int, input().split())
H = sorted([int(input()) for _ in range(N)])
print(min(H[i+K-1]-H[i] for i in range(N-K+1)))
Buggy code:
a,b=input().split()
c,d=int(a),int(b)
l=[int(input()) for i in range(c)]
L=sorted(l)
x=L[d]-L[0]
for i in range(c-d+1):
    if L[d+i]-L[i]<x:
        x=L[d+i]-L[i]
print(x)

Fixed code:
a,b=input().split()
c,d=int(a),int(b)
l=[int(input()) for i in range(c)]
L=sorted(l)
x=L[d-1]-L[0]
for i in range(c-d+1):
    if L[d+i-1]-L[i]<x:
        x=L[d+i-1]-L[i]
print(x)
Buggy code:
N, K = map(int, input().split())
H=[int(input()) for i in range(N)]

H.sort()

ans = float("inf")
for i in range(N-K+1):
    ans = min(ans, H[i+N-K-1]-H[i])

print(ans)

Fixed code:
N, K = map(int, input().split())
H=[int(input()) for i in range(N)]

H.sort()

ans = float("inf")
for i in range(N-K+1):
    ans = min(ans, H[K-N+i-1]-H[i])

print(ans)
Buggy code:
n, k = map(int, input().split())
tree = [int(input()) for _ in range(n)]
tree.sort()
result = float('INF')
cumulative = [0]
for i in range(n - 1):
    cumulative.append(tree[i + 1] - tree[i])
for i in range(n - k):
    result = min(result, cumulative[i + k - 1] - cumulative[i])
print(result)


Fixed code:
n, k = map(int, input().split())
tree = [int(input()) for _ in range(n)]
tree.sort()
result = float('INF')
cumulative = [0]
for i in range(n - 1):
    cumulative.append(tree[i + 1] - tree[i] + cumulative[i])
for i in range(n - k + 1):
    result = min(result, cumulative[i + k - 1] - cumulative[i])
print(result)
Buggy code:
n,k=map(int,input().split())
h=sorted([int(input()) for _ in range(n)])
gap=[h[i]-h[i-1] for i in range(1,n)]
tmp=0
lst=[gap[0]]
for i in range(1,len(gap)):
    lst.append(gap[i]+lst[i-1])
print(gap,lst)
ans=[lst[k-2]]
for i in range(k,n):
    ans.append(lst[i-1]-lst[i-k])
print(ans)
print(min(ans))

Fixed code:
n,k=map(int,input().split())
h=sorted([int(input()) for _ in range(n)])
gap=[h[i]-h[i-1] for i in range(1,n)]
tmp=0
lst=[gap[0]]
for i in range(1,len(gap)):
    lst.append(gap[i]+lst[i-1])
#print(gap,lst)
ans=[lst[k-2]]
for i in range(k,n):
    ans.append(lst[i-1]-lst[i-k])
#print(ans)
print(min(ans))
Buggy code:
N, K = map(int, input().split())
h = [int(input()) for i in range(N)]

h.sort()

ans = float("inf")

for n in range(N-K):
    ans = min(ans, h[n+K-1] - h[n])

print(ans)

Fixed code:
N, K = map(int, input().split())
h = [int(input()) for i in range(N)]

h.sort()
ans = float("inf")

for n in range(N-K+1):
    ans = min(ans, (h[n+K-1] - h[n]))

print(ans)
Buggy code:
# coding: utf-8
# Your code here!
N,K = [int(i) for i in input().split()]

l = [int(input()) for j in range(N)]
l = sorted(l)
dif = 1000000000
for a in range(N-K+1):
    d = l[a+K-1] - l[a]
    print(a)
    print(d)
    if dif >= d:
        dif = d

print(dif)


Fixed code:
# coding: utf-8
# Your code here!
N,K = [int(i) for i in input().split()]

l = [int(input()) for j in range(N)]
l = sorted(l)
dif = 1000000000
for a in range(N-K+1):
    d = l[a+K-1] - l[a]
    #print(a)
    #print(d)
    if dif >= d:
        dif = d

print(dif)
Buggy code:
n,k = map(int,input().split())
h = sorted([int(input()) for _ in range(n)])
print(min(ans[j+k-1]-ans[j] for j in range(n-k+1)))

Fixed code:
n,k = map(int,input().split())
h = sorted([int(input()) for _ in range(n)])
print(min(h[j+k-1]-h[j] for j in range(n-k+1)))
Buggy code:
import collections
def c():
    n, k = map(int, input().split())
    h = []
    for i in range(n):
        h.append(int(input()))
    h.sort()
    d = collections.deque(maxlen = k)
    mins = []
    while len(d) < k:
        d.append(h.pop())
    while h:
        mins.append(d[k-1] - d[0])
        d.append(h.pop())
    mins.append(d[k-1] - d[0])
    print(min(mins))
c()



Fixed code:
import collections
def c():
    n, k = map(int, input().split())
    h = []
    for i in range(n):
        h.append(int(input()))
    h.sort()
    d = collections.deque(maxlen = k)
    mins = []
    while len(d) < k:
        d.append(h.pop())
    while h:
        mins.append(d[0] - d[k-1])
        d.append(h.pop())
    mins.append(d[0] - d[k-1])
    print(min(mins))
c()
Buggy code:
N, K = map(int,input().split())
a = 1000000000
h = list(map(int, [input() for i in range(N)]))
h.sort()
print(h)
for i in range(N-K+1):
    a = min(a, h[i+K-1]-h[i])
print(a)


Fixed code:
N, K = map(int,input().split())
a = 1000000000
h = list(map(int, [input() for i in range(N)]))
h.sort()
for i in range(N-K+1):
    a = min(a, h[i+K-1]-h[i])
print(a)
Buggy code:
N,K = map(int,input().split())

H = []
for i in range(N):
  h = int(input())
  H.append(h)
H.sort()

hmax_hmin = []
for i in range(N-K+1):
  print(H[i+K-1],H[i])
  hmax_hmin.append(H[i+K-1] - H[i]) 
  
print(min(hmax_hmin))

Fixed code:
N,K = map(int,input().split())

H = []
for i in range(N):
  h = int(input())
  H.append(h)
H.sort()

hmax_hmin = []
for i in range(N-K+1):
  hmax_hmin.append(H[i+K-1] - H[i]) 
print(min(hmax_hmin))
Buggy code:
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
N, K = [int(x) for x in input().split()]
height = []
for _ in range(N):
    height.append(int(input()))

height.sort()
height.reverse()
sublist = []
for i in range(N-K+1):
    hmax-hmin = height[i]-heght[i+K]
    sublist.append(hmax-hmin)

print(min(sublist))


Fixed code:
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
N, K = [int(x) for x in input().split()]
height = []
for _ in range(N):
    height.append(int(input()))

height.sort()
height.reverse()
sublist = []
for i in range(N-K+1):
    hmaxhmin = height[i]-height[i+K-1]
    sublist.append(hmaxhmin)

print(min(sublist))
Buggy code:
#coding:utf-8
n,k=int(input().split())
h=[int(input()) for i in range(n)]
h.sort()
print(min(h[i+k-1]-h[i] for i in range(n-k+1)))

Fixed code:
#coding:utf-8
n,k=map(int, input().split())
h=[int(input()) for i in range(n)]
h.sort()
print(min(h[i+k-1] - h[i] for i in range(n-k+1)))
Buggy code:
n,k = map(int,input().split())
h=[int(input()) for i in range(n)]
h=sort(h)
mi=float('inf')
 
for i in range(n-k+1):
    mi = min(mi, h[i+k-1]-h[i])
 
print(mi)

Fixed code:
n,k = map(int,input().split())
h=list(int(input()) for i in range(n))
h.sort()
mi=float('inf')

for i in range(n-k+1):
    mi = min(mi, h[i+k-1]-h[i])
 
print(mi)