

Hãy luôn nhớ cảm ơn và vote 5*
nếu câu trả lời hữu ích nhé!
uses crt;
var f:text; i,j,n,m,kq,t:longint; a:array[1..100000]of longint;
begin
clrscr;
assign(f,'BUYGIFTS.INP');reset(f);
readln(f,n,m);
for i:=1 to n do read(f,a[i]);
close(f);
assign(f,'BUYGIFTS.OUT');rewrite(f);
for i:=1 to n-1 do
for j:=i+1 to n do
if a[i]>a[j] then
begin t:=a[i]; a[i]:=a[j]; a[j]:=t; end;
kq:=high(longint);
for i:=1 to n-m+1 do
if a[i+m-1]-a[i]<kq then kq:=a[i+m-1]-a[i];
write(f,kq);
close(f);
end.
Hãy giúp mọi người biết câu trả lời này thế nào?

uses math;
var a:array[0..10000007] of int64;
n,m,mi:int64;
i:longint;
procedure sort(l,r:int64);
var i,j,x,tmp:int64;
begin
i := l; j := r; x := a[(i+j) div 2];
repeat
while a[i] < x do inc(i);
while a[j] > x do dec(j);
if (i <= j) then
begin
tmp := a[i];
a[i] := a[j];
a[j] := tmp;
inc(i);dec(j);
end;
until (i > j);
if (l<j) then sort(l,j);
if (i<r) then sort(i,r);
end;
begin
assign(input,'buygifts.inp');reset(input);
assign(output,'buygifts.out');rewrite(output);
readln(m,n);
mi := trunc(1e18);
for i := 1 to n do read(a[i]);
sort(1,n);
for i := 1 to n-m+1 do
mi := min(mi,a[i+m-1]-a[i]);
writeln(mi);
close(input);close(output);
end.
Hãy giúp mọi người biết câu trả lời này thế nào?

Bảng tin
2376
42183
1806
Thế á, để đệ sửa 🤣
20
237
9
sao ko ddc banj
20
237
9
https://hoidap247.com/cau-hoi/3117473
364
8819
301
sắp xếp như này thì tle :V
2376
42183
1806
Đề đâu có giới hạn time đâu ạ uwu
20
237
9
bạn thử có đc ko
20
237
9
https://hoidap247.com/cau-hoi/3117473
20
237
9
https://hoidap247.com/cau-hoi/3130638