qyliang/CodeNanoFix huggingface dataset:
Buggy code:
N,K = map(int,input().split())
treelist = []
difflist = []
append = treelist.append
#print(treelist)
append_diff = difflist.append
for i in range(N):
    tree = int(input())
    #print(tree)
    append(tree)
treelist.sort()
print(treelist)
for i in range(K-1,N):
    diff = treelist[i] - treelist[i-K+1]
    #print(diff)
    append_diff(diff)
print(min(difflist))

Fixed code:
N,K = map(int,input().split())
treelist = []
difflist = []
append = treelist.append
#print(treelist)
append_diff = difflist.append
for i in range(N):
    tree = int(input())
    #print(tree)
    append(tree)
treelist.sort()
#print(treelist)
for i in range(K-1,N):
    diff = treelist[i] - treelist[i-K+1]
    #print(diff)
    append_diff(diff)
print(min(difflist))
Buggy code:
N, K = map(int, input().split())
h = []
for _ in range(N):
    h.append(int(input()))

h.sort()

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

print(ans)

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

h.sort()

ans = 10 ** 9
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 =[int(input()) for i in range(n)]
h.sort()
ans = 100000000

for i in range(len(h)-k+1):
    if h[i+k-1] - h[i] < ans:
        ans = h[i+k-1] - h[i]
        if ans = 0:
            break

print(ans)

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

for i in range(len(h)-k+1):
    if h[i+k-1] - h[i] < ans:
        ans = h[i+k-1] - h[i]
        if ans == 0:
            break

print(ans)
Buggy code:
N, K = map(int, input().split())

tree = []
for _ in range(n):
    tree.append(int(input()))

tree.sort()
ans = 10**9
for i in range(N-K+1):
    ans = min(ans, tree[i+K-1]-tree[i])
print(ans)


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

tree = []
for _ in range(N):
    tree.append(int(input()))

tree.sort()
ans = 10**9
for i in range(N-K+1):
    ans = min(ans, tree[i+K-1]-tree[i])
print(ans)
Buggy code:
NK = input().split(' ')
N, K = int(NK[0]), int(NK[1])
trees = []
for i in range(N):
  trees.append(int(input()))
trees.sort()
min = 0
for i in range(N-K):
  h = trees[i+K]-trees[i]
  if h < min:
    min = h
print(min)

Fixed code:
NK = input().split(' ')
N, K = int(NK[0]), int(NK[1])
trees = []
for i in range(N):
  trees.append(int(input()))
trees.sort()
min = float("inf")
for i in range(N-K+1):
  h = trees[i+K-1]-trees[i]
  if h < min:
    min = h
print(min)
Buggy code:
N, K = map(int, input().split())
H = [int(input()) for _ in range(N)]
H.sort()
df = 0
min_h = H[K-1]-H[0]

for i in range(1, N-K-1):
    new_h = H[i+K-1]-H[i]
    if new_h < min_h:
        min_h = new_h

print(min_h)


Fixed code:
N, K = map(int, input().split())
H = [int(input()) for _ in range(N)]
H.sort()
df = 0
min_h = H[K-1]-H[0]

for i in range(1, N-K+1):
    new_h = H[i+K-1]-H[i]
    if new_h < min_h:
        min_h = new_h

print(min_h)
Buggy code:
n,k=map(int,input().split())
h=[int(input()) for i in range(n)]
h.sort()
lis=[]
for i in range(n-k):
  lis.append(h[i+k-1]-h[i])
print(min(lis))

Fixed code:
n,k=map(int,input().split())
h=[int(input()) for i in range(n)]
h.sort()
lis=[]
for i in range(n-k+1):
  lis.append(h[i+k-1]-h[i])
print(min(lis))
Buggy code:
n, k = map(int,input().split())
h = list(int(input()) for i in range(n))    
h=sorted(h)
x=[]
for i in range(n-k+1):
  b = h[i+k-1]-h[i]
x.append(b)
print(min(x))

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

print(result)

Fixed code:
N, K = map(int, input().split())
h = [int(input()) for _ in range(N)]
h.sort()
result = max(h) - min(h)
for i in range(N-K+1):
  tmp = h[i+K-1]-h[i]
  result = min(result, tmp)

print(result)
Buggy code:
import sys
def main():
  if 'get_ipython' not in globals():  
    # For Subsession
    N,K = map(int, input().split())
    h = []
    for _ in range(N):
      h.append(int(input()))
  else:
    N = 5
    K = 3
    h = [10,15,11,14,12]
    # N = 5
    # K = 3
    # h = [5,7,5,7,7]

  h.sort()
  ans = 0
  
  for n in range(0,K-1):
    ans += (h[n+1] - h[n])
  
  max_i = len(h)-K+1
  for i in range(1,max_i):
    tmp = 0
    max_j =  range(i, i+K-1)
    for n in max_j:
      tmp += (h[n+1] - h[n])
    if ans > tmp:
      ans = tmp
    if ans == 0:
      sys.exit()
  
  print(ans)

main()

Fixed code:
import sys
def main():
  if 'get_ipython' not in globals():  
    # For Subsession
    N,K = map(int, input().split())
    h = []
    for _ in range(N):
      h.append(int(input()))
  else:
    N = 5
    K = 3
    h = [10,15,11,14,12]
    # N = 5
    # K = 3
    # h = [5,7,5,7,7]

  h.sort()  

  ans = (h[K-1] - h[0])
  
  max_i = len(h)-K+1
  for i in range(1,max_i):
    tmp = (h[i+K-1] - h[i])
    if ans > tmp:
      ans = tmp
    if ans == 0:
      print(ans)
      sys.exit()
  
  print(ans)

main()
Buggy code:
N, K = map(int, input().split())
h = [int(input()) for _ in range(N)]

h.sort()

ans = float("inf")

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

print(ans)

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

h.sort()

ans = float("inf")

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

print(ans)
Buggy code:
N,K = map(int,input().split())
L = sorted([int(input()) for i in range(N)])
ans = float("inf")
for i in range(N-K) :
    ans = min(ans, L[i+K-1]-L[i])
print(ans)


Fixed code:
N,K = map(int,input().split())
L = sorted([int(input()) for i in range(N)])
ans = float("inf")
for i in range(N-K+1) :
    ans = min(ans, L[i+K-1]-L[i])
print(ans)
Buggy code:
m = input()
n = int(m.split()[0])
k = int(m.split()[1])
t = []
for i in range(n):
    t.append(int(input()))
t.sort()
a = None
for i in range(len(t)-k+1):
    w = abs(-1*t[i] + t[i+k])
    if i == 0:
        a = w
    elif a > w:
        a = w
print(a)

Fixed code:
m = input()
n = int(m.split()[0])
k = int(m.split()[1])
t = []
for i in range(n):
    t.append(int(input()))
t.sort()
a = None
for i in range(len(t)-k+1):
    w = abs(-1*t[i] + t[i+k-1])
    if i == 0:
        a = w
    elif a > w:
        a = w
print(a)
Buggy code:
def cChristmas(n, k, hl):


    hl.sort()
    ans = 10**10

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

    return ans

def main():
    n, k = map(int, input().split())
    hl = [int(input()) for i in range(n)]
    print(cChristmas(n, k, hl))

if __name__ == '__main__':
    main()


Fixed code:
def cChristmas(n, k, hl):


    hl.sort()
    ans = 10**10

    for i in range(n-k+1):
        ans = min(ans, hl[i+k-1]-hl[i])

    return ans

def main():
    n, k = map(int, input().split())
    hl = [int(input()) for i in range(n)]
    print(cChristmas(n, k, hl))

if __name__ == '__main__':
    main()
Buggy code:
n, k = map(int, input().split())
h = sorted([int(input()) for _ in range(n)])

ans = float('inf')
for i in range(n-k):
    ans = min(h[i+k-1] - h[i], ans)
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[i+k-1]-h[i])
print(ans)
Buggy code:
N, K = map(int, input().split())
h_list = []
for i in range(N):
    h = int(input())
    h_list.append(h)
h_list = sorted(h_list, reverse = True)
for i in range(N-K+1):
    cnt = h_list[i]-h_list[i+K-1]
    min1 = min(min1, cnt)
print(min1)

Fixed code:
N, K = map(int, input().split())
h_list = []
min1 = 10**10
for i in range(N):
    h = int(input())
    h_list.append(h)
h_list = sorted(h_list, reverse = True)
for i in range(N-K+1):
    cnt = h_list[i]-h_list[i+K-1]
    min1 = min(min1, cnt)
print(min1)
Buggy code:
n, k = map(int, input().split())
list_original = []
for i in range(n)
    list_original.append(int(input))

list = sorted(list_original)
sub = 10000000000

for j in range(n-k+1):
    if list[j+k-1]-list[j] <= sub:
        sub =list[j+k-1]-list[j]
    else:
        pass

print(sub)

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

list = sorted(list_original)
sub = 10000000000

for j in range(n-k+1):
    if list[j+k-1]-list[j] <= sub:
        sub =list[j+k-1]-list[j]
    else:
        pass

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

min = 1000000000

h.sort()

for i in range(0, len(h)-k):
    diff = h[i+k-1] - h[i]
    if diff < min:
        min = diff

print(min)


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

min = 1000000000

h.sort()

for i in range(0, len(h)-k+1):
    diff = h[i+k-1] - h[i]
    if diff < min:
        min = diff

print(min)
Buggy code:
from functools import reduce
n, k = map(int, input().split())

heights = []
for _ in range(n):
	heights.append(int(input()))

heights.sort()

diff = [0]*n

for i in range(k,len(heights)):
	diff[i] = heights[i]-heights[i-k]
	
print(reduce(min, diff[k:], 1000000002))

Fixed code:
from functools import reduce
n, k = map(int, input().split())

heights = []
for _ in range(n):
	heights.append(int(input()))

heights.sort()

diff = [0]*n

for i in range(k-1,len(heights)):
	diff[i] = heights[i]-heights[i-k+1]
	
print(reduce(min, diff[k-1:], 1000000002))
Buggy code:
def main():
    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)]))
        
    
if __name__ == '__main__':
    main()

Fixed code:
def main():
    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)]))
        
    
if __name__ == '__main__':
    main()
Buggy code:
N, K = (int(x) for x in input().split())
h = [int(input()) for i in range(K)]
h.sort()
print(min(h[i+K-1] - h[i] for i in range(N-K+1)))

Fixed code:
N, K = (int(x) for x in 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)].sort()
# print(h)

MIN = 10**10
for i in range(N-K):
	if h[i+K-1] - h[i] <= MIN:
		MIN = h[i+K-1] - h[i]
#	print(i,h[i],h[i+K-1])
print(MIN)



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

MIN = 10**10
for i in range(N-K+1):
	if h[i+K-1] - h[i] <= MIN:
		MIN = h[i+K-1] - h[i]
# print(i,h[i],h[i+K-1])
print(MIN)
Buggy code:
N, K = map(int, input().split())
lst = []
for i in range(N):
    lst.append(int(input()))
lst.sort()
dif = 10**9
for i in range(N):
    if lst[i+K-1] - lst[i] < dif:
        dif = lst[i+K-1] - lst[i]
print(dif)

Fixed code:
N, K = map(int, input().split())
lst = []
for i in range(N):
    lst.append(int(input()))
lst.sort()
dif = 10**9
for i in range(N-K+1):
    if lst[i+K-1] - lst[i] < dif:
        dif = lst[i+K-1] - lst[i]
print(dif)
Buggy code:
N, K = map(int, input().split())
lst = []
for i in range(N):
    x = int(input())
    lst.append(x)
lst.sort()
L = []
for i in range(N - K + 1):
    L.append(lst[i + K -1] - lst[i])
L.sort()
print(L)
print(L[0])

Fixed code:
N, K = map(int, input().split())
lst = []
for i in range(N):
    x = int(input())
    lst.append(x)
lst.sort()
L = []
for i in range(N - K + 1):
    L.append(lst[i + K -1] - lst[i])
L.sort()
print(L[0])
Buggy code:
N,K = map(int, input().split())
hs = []
for _ in range(N):
    h = int(input())
    hs.append(h)
hs.sort()
print(hs)
ans = 1e9
for i in range(N-K+1):
    ans = min(ans, abs(hs[i]-hs[i+K-1]))
print(ans)

Fixed code:
N,K = map(int, input().split())
hs = []
for _ in range(N):
    h = int(input())
    hs.append(h)
hs.sort()
# print(hs)
ans = 1e9
for i in range(N-K+1):
    ans = min(ans, abs(hs[i]-hs[i+K-1]))
print(ans)
Buggy code:
n,k =map(int,input().split())
h = [int(input()) for _ in range(n)]
l = []
h.sort()
for i in range(n-1-k):
        x = h[i+k-1] - h[i]
        l.append(x)
print(min(l))

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

Fixed code:
n,k=map(int,input().split())
h=sorted(list(map(int,[input() for i in range(n)])))
d=h[k-1]-h[0]
for i in range(n-k):
    d=min(d,h[k+i]-h[i+1])
print(d)
Buggy code:
# -*- coding: utf-8 -*-
import sys
from collections import deque, defaultdict
from math import sqrt, factorial
# def input(): return sys.stdin.readline()[:-1] # warning not \n
# def input(): return sys.stdin.buffer.readline().strip() # warning bytes
# def input(): return sys.stdin.buffer.readline().decode('utf-8')


def solve():
    n, k = [int(x) for x in input().split()]
    a = []
    for _ in range(n):
        a.append(int(input()))
    a.sort()
    ans = float('inf')
    for i in range(n-k):
        ans = min(ans, a[i+k-1]-a[i])
    print(ans)
t = 1
# t = int(input())
for case in range(1,t+1):
    ans = solve()


"""

a aa c ca c a b a ab a b c

"""


Fixed code:
# -*- coding: utf-8 -*-
import sys
from collections import deque, defaultdict
from math import sqrt, factorial
# def input(): return sys.stdin.readline()[:-1] # warning not \n
# def input(): return sys.stdin.buffer.readline().strip() # warning bytes
# def input(): return sys.stdin.buffer.readline().decode('utf-8')


def solve():
    n, k = [int(x) for x in input().split()]
    a = []
    for _ in range(n):
        a.append(int(input()))
    a.sort()
    ans = float('inf')
    for i in range(n-k+1):
        ans = min(ans, a[i+k-1]-a[i])
    print(ans)
t = 1
# t = int(input())
for case in range(1,t+1):
    ans = solve()


"""

a aa c ca c a b a ab a b c

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

h.sort()
ans = 1e9

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

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

h.sort()
ans = 1e9


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

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

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

cand = [0]*(n-k+1)
cand[0] = h[k-1] - h[0]


for j in range(1,n-k+1):
    cand[i] = cand[i-1] + h[k+i-1] - h[i]

print(min(cand))

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

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

cand = [0]*(n-k+1)
cand[0] = h[k-1] - h[0]


for j in range(1,n-k+1):
    cand[j] = h[k+j-1] - h[j]

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

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

trees = []

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

trees.sort()

min_diff = 10**10
for j in range(n-k):
    diff = abs(trees[j] - trees[j+k-1])
    if diff < min_diff:
        min_diff = diff

print(min_diff)

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

trees = []

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

trees.sort()
min_diff = 10**10
for j in range(n-k+1):
    diff = abs(trees[j] - trees[j+k-1])
    if diff < min_diff:
        min_diff = diff

print(min_diff)
Buggy code:
N, K = map(int, input().split())
A=[]
for i in range(N):
    A.append(int(input()))

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

print(mini)

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

A.sort()
#print(A)
mini=10000000000
for i in range(N-K+1):
    mini=min(mini,A[i+K-1]-A[i])

print(mini)
Buggy code:
import sys
input = sys.stdin.readline
N, K = map(int, input().split())
heights = sorted([int(input() for _ in range(N)])
ans = float('inf')
for i in range(N - K + 1):
    if abs(heights[i] - heights[i + K - 1]) < ans:
        ans = abs(heights[i] - heights[i + K - 1])
print(ans)

Fixed code:
import sys
input = sys.stdin.readline
N, K = map(int, input().rstrip().split())
heights = sorted([int(input().rstrip()) for _ in range(N)])
ans = float('inf')
for i in range(N - K + 1):
    if abs(heights[i] - heights[i + K - 1]) < ans:
        ans = abs(heights[i] - heights[i + K - 1])
print(ans)
Buggy code:
N,K = [int(x) for x in input().split()]
h = [0 for x in range(N)]
for i in range(N):
    h[i] = int(input())
h = sorted(h)
dis = []
for i in range(N-K+1):
    dis.append(h[i+K-1]-h[i])


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


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

Fixed code:
import sys
input = sys.stdin.readline
n,k = map(int,input().split())
l = sorted([int(input()) for _ in range(n)])
print(min(l[i+k-1]-l[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()
ans = 10**10
for i in range(N-K+1):
    ans = min(ans, H[i-1+K]-H[i])
    print(i,ans)

print(ans)

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

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

print(ans)
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)))

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:
N, K = map(int, input().split())
H = []
for i in range(N):
    h = int(input())
    H.append(h)
H = H.sorted()
mi = 10**9
for i in range(N-K+1):
    _ = H[i+K] - H[i]
    if mi > _:
        mi = _
print(mi)

Fixed code:
N, K = map(int, input().split())
H = []
for i in range(N):
    h = int(input())
    H.append(h)
H = sorted(H)
mi = 10**9
for i in range(N-K+1):
    _ = H[i+(K-1)] - H[i]
    if mi > _:
        mi = _
print(mi)
Buggy code:
n,k = (int(i) for i in input().split())
l = list([])
for i in range(n):
    h = int(input())
    l.append(h)
l2 = sorted(l, reverse=True)

d = k-1
d_min = 1000000000
for i in range(n-d):
    tmp = abs(l2[i] - l2[i+d])
    if (d_min > tmp ):
        d_min = tmp
print(tmp)

Fixed code:
n,k = (int(i) for i in input().split())
l = list([])
for i in range(n):
    h = int(input())
    l.append(h)
l2 = sorted(l, reverse=True)

d = k-1
d_min = 1000000000
for i in range(n-d):
    tmp = l2[i] - l2[i+d]
    if (d_min > tmp ):
        d_min = tmp
print(d_min)
Buggy code:
n,k=map(int,input().split())


h=list(int(input()) for in range(n))
h.sort()
result=10**9

i=0
while i+k-1<n:
  result=min(result,h[i+k-1]-h[i])
  
  i+=1
  
print(result)
    

Fixed code:
n,k=map(int,input().split())
 
 
h=list(int(input()) for _ in range(n))
h.sort()
result=10**9
 
i=0
while i+k-1<n:
  result=min(result,h[i+k-1]-h[i])
  
  i+=1
  
print(result)
Buggy code:
a=list(map(int,input().split()))
b=list(sorted([int(input()) for i in range(a[0])]))
ans=[]
for i in range(a[0]-a[1]+1):
  ans+=[abs(b[i-a[1]+1]-b[i])]
print(min(ans))

Fixed code:
a=list(map(int,input().split()))
b=list(sorted([int(input()) for i in range(a[0])]))
ans=[]
for i in range(a[0]-a[1]+1):
  ans+=[abs(b[a[1]-1+i]-b[i])]
print(min(ans))
Buggy code:
n,k=map(int,input().split())
p=[int(input()) for i in range(n)]
p.sort()
q=9999999999999
for i in range (n-k+1):
    print(p[k+i-1])
    print(p[i])
    if p[k+i-1]-p[i]<q:
        q=p[k+i-1]-p[i]
print(q)

Fixed code:
n,k=map(int,input().split())
p=[int(input()) for i in range(n)]
p.sort()
q=9999999999999
for i in range (n-k+1):
    if p[k+i-1]-p[i]<q:
        q=p[k+i-1]-p[i]
print(q)
Buggy code:
n,k=map(int,input().split())

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

x.sort()

y=[]

for j in range(n):
  p=x[j+k-1]-x[j]
  y.append(p)

y.sort()

print(y[0])

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

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

x.sort()

y=[]

for j in range(n-k+1):
  p=x[j+k-1]-x[j]
  y.append(p)

y.sort()

print(y[0])
Buggy code:
N, K = map(int, input().split())
H = [int(input()) for i in range(N)]
H.sort()

D = [(H[i+K-1] - H[i]) for i in range(N-K)]
print(min(D))


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

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

an.sort()
dn = [-1 for _ in range(n - k + 1)]
for i in range(n - k + 1):
  dn[i] = an[i+k] - an[i]

print(min(dn))

  

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

an.sort()
dn = [-1 for _ in range(n - k + 1)]
for i in range(n - k + 1):
  dn[i] = an[i+k-1] - an[i]

print(min(dn))
Buggy code:
N,K = (int(i) for i in input().split())  
h = [int(input()) for i in range(N)]
 
h.sort()
 
ans=[]
 
for i in range(N):
	if i+K==N:
		break
	else:
		ans.append(h[i+K-1] - h[i])
print(min(ans))

Fixed code:
N,K = (int(i) for i in input().split())  
h = [int(input()) for i in range(N)]
 
h.sort()
 
ans=[]
 
for i in range(N):
	if i+K==N+1:
		break
	else:
		ans.append(h[i+K-1] - h[i])
print(min(ans))
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 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 = []
ans = 10**10
for i in range(N):
    tmp = int(input())
    h.append(tmp)
h = sorted(h)[::-1]
for i in range(N-K):
    ans = min(h[i]-h[i+K-1],ans)
print(str(ans))

Fixed code:
N,K = map(int,input().split())
h = []
ans = 10**10
for i in range(N):
  tmp = int(input())
  h.append(tmp)
h = sorted(h)
for i in range(N-K+1):
  h2 = h[i]
  h3 = h[i+K-1]
  ans = min(h3 - h2,ans)
print(str(ans))
Buggy code:
n,k=map(int,(input().split()))
h=[int(input()) for i in range(n)]

q=sorted(h)
ansList=[]
for c,d in zip(q,q[k-1:]):
  print(c,d)
  ansList.append(d-c)

print(min(ansList))

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

q=sorted(h)
ansList=[]
for c,d in zip(q,q[k-1:]):
  ansList.append(d-c)

print(min(ansList))
Buggy code:
N, K = map(int, input().split())
h = [int(input()) for i in range(N)].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 = sorted([int(input()) for i in range(N)])
print(min(h[i+K-1] - h[i] for i in range(N-K+1)))
Buggy code:
N,K = [int(i) for i in input().split()]
H = sorted([int(input()) for i in range(N)])
print(H)
ans = H[K - 1] - H[0]
for i in range(N - K + 1):
    t = H[K + i - 1] - H[i]
    if ans > t:
        ans = t
print(ans)

Fixed code:
N,K = [int(i) for i in input().split()]
H = sorted([int(input()) for i in range(N)])
ans = H[K - 1] - H[0]
for i in range(N - K + 1):
    t = H[K + i - 1] - H[i]
    if ans > t:
        ans = t
print(ans)