Once more, I’m convinced that there is some really small bug that’s giving me wrong answer. Could you give me some text example that fails when running program.
program eqidlis;
var
t,n,i,j,k,middle,ceil,temp,res:integer;
equal:boolean;
idli:array[1..3000] of integer;
begin
readln(t);
for k:=1 to t do
begin
readln(n);
for i:=1 to n do read(idli[i]);
for i:=2 to n do
begin
j:=i;
while (j>1) and (idli[j-1]>idli[j]) do
begin
temp:=idli[j-1];
idli[j-1]:=idli[j];
idli[j]:=temp;
Dec(j);
end;
end;
middle:= n div 2; j:=n; res:=0;
for i:=1 to middle do
begin
ceil:=idli[j]-idli[i];
if ceil=0 then continue
else
begin
Inc(res);
if ceil mod 2=0 then ceil:=ceil div 2
else ceil:=(ceil div 2)+1;
idli[i]:=idli[i]+ceil;
idli[j]:=idli[j]-ceil;
end;
Dec(j);
end;
temp:=idli[1]; equal:=true;
for i:=2 to n do
if idli[i]<>temp then begin equal:=false; break; end;
if equal then writeln(res)
else writeln('-1');
end;
end.
First, I sort out the array. Then starting from counter1=1 to middle (length of the array div 2) and back from counter2=length of the array to the middle I calculate the ciel difference of each element ciel(array[counter1],array[counter2]) the way it is described. Then, I check whether all the members of array are the same. If not print -1 else print res. That’s it.